Build the app and follow its runtime boundaries.
The Android client is a Java 21, Android Jetpack application with Gradle Kotlin DSL, ViewBinding, Credential Manager, Retrofit, Firebase, and SLF4J. This guide complements the Javadocs embedded in the source.
The two main functions are Brand Verification (product authentication) and Device Control.
Brand Verification and Device Control share secure NFC tag authentication: the Xibernetix app executes a cryptographic handshake with the embedded tag and backend.
The detailed operator-binding and provisioning examples describe Device Control. Brand Verification uses brand-scoped NFC tag authentication. Bluetooth is an alternative Device Control connection still in development.
Build and test
Install JDK 21 and Android SDK 37.2, then point local.properties to the SDK. The development
debug variant is the quickest local
target.
./gradlew :app:assembleDevDebug
./gradlew :app:installDevDebug
./gradlew :app:testDevDebugUnitTest
./gradlew :app:connectedDevDebugAndroidTest
./gradlew :app:lintDevDebug
The project defines dev, stg, and prod flavors and both debug and
release build types. Use the flavor
that
matches the backend and identity configuration under test.
Project map
| Area | Package or file | Responsibility |
|---|---|---|
| Application UI | com.xibernetix.device |
Activities and fragments for role workspaces, devices, authentication, passkeys, settings, and security events. |
| NFC | com.xibernetix.nfc |
Reader sessions, ISO-DEP commands, chip introspection, cryptography, provisioning, authentication, and tag presentation. |
| Bluetooth for Device Control | com.xibernetix.ble |
In development: BLE discovery, GATT profile interpretation, and session lifecycle. End-to-end integration between the mobile app and backend is incomplete. |
| Tokens | com.xibernetix.token |
Authentication-token storage, parsing, and role checks. |
| Network DTOs | com.xibernetix.device.credentials |
Retrofit API contracts, sessions, account DTOs, passkeys, devices, and security events. |
| Resources | app/src/main/res |
Layouts, strings, navigation resources, themes, icons, and ViewBinding inputs. |
NFC control flow
NfcDeviceControlActivityNfcSessionManagerNfcTagProcessorNfcDeviceScannedActivity
| Java type | Control-flow responsibility |
|---|---|
NfcDeviceControlActivity |
Requires the operator role, enables reader mode in onResume, disables it in
onPause, receives onTagDiscovered,
and posts the result-screen transition to the UI thread.
|
NfcSessionManager |
Guards authorization, routes NDEF versus ISO-DEP, resolves a model processor, supplies token and phone metadata, handles failures, and closes ISO-DEP. |
NfcTagType4ChipModel |
Maps the first ISO-DEP historical byte to a model enum. Duplicate first-byte mappings are replaced by the last enum declaration. |
NfcTagIntrospector |
Connects, selects the security application, reads GetVersion frames and SDM counter data, and probes factory-default key settings. |
NfcTagProcessor |
Implements the shared decision: try factory-key authentication and provision on success; otherwise authenticate with the diversified key and validate operator ownership. |
NfcTagOperatorIdHandler |
Writes, reads, and protects operator ID file 2, including migration from legacy clear communication to EV2 Full secure messaging. |
IsoDepTagTransceiver |
Builds and transmits the native or wrapped APDU commands used by introspection and secure messaging. |
NfcTagCrypto |
Performs AES operations, CMAC calculation, EV2 IV construction, session-key derivation, rotations, and hexadecimal conversion. |
XibernetixNfcBackendClient |
Adapts Retrofit callbacks to the ordered reader flow and waits up to the configured API timeout for key diversification. |
NfcTagAuthContext |
Carries verification, protocol, secure-session, operator, and display data from processing to the scanned-tag UI. |
Threading and lifetime
NfcAdapter.ReaderCallback.onTagDiscoveredis not the UI thread. Keep the ordered tag exchange there and marshal UI work throughrunOnUiThread.- The tag handle is useful only while the tag remains in the RF field.
NfcSessionManagerowns and closes the ISO-DEP channel. XibernetixNfcBackendClientblocks on a bounded latch because provisioning and authentication cannot be split into unordered callbacks. Never invoke it from the main thread.- Retrofit callbacks used by the legacy NDEF ship path are asynchronous and do not create a verified
NfcTagAuthContext.
Security invariants
- Treat
NfcTagAuthContext.identityVerified()as the final decision. Other populated fields are diagnostic, not proof. - For an existing Device Control tag, success requires mutual cryptographic authentication and the authorized operator/mobile-device binding. Brand Verification uses the tag's registered brand identity and does not require the scanning operator to own the tag.
- Master keys are cached through
EncryptedSharedPreferences; session encryption and MAC keys are derived per EV2 session. NfcTagAuthContextcontains master and session key bytes so it can render session diagnostics. Do not log, serialize to analytics, or persist the complete record.- Provisioning writes operator file 2, changes it to Full secure messaging, then changes key slot 0. Command counters must account for those preceding secure operations.
- Do not turn authentication failures into a factory reset or NDEF format attempt. Escalate an uncertain provisioning state.
Flow blind spots and operational boundaries
- Public-page access is enforced in more than one place: the application security chain, the OAuth
security chain, and the token filter.
When adding a public route, update every applicable allowlist and verify anonymous access to its
canonical, trailing-slash, and
.htmlforms. - The shared web header decodes the browser JWT only to choose an account or sign-in link. That display decision is not authorization; endpoint access must continue to be decided by the server-side security configuration and role checks.
- A successful backend record update does not prove that the phone completed the matching NFC operation. The tag can leave the RF field, the ordered exchange can time out, or a secure command can fail after an earlier command has changed tag state. Treat the verified tag result as the completion signal and investigate uncertain states rather than retrying destructively.
- Role-specific screens guide the user, but they do not replace server enforcement. Keep role checks on the Android entry point and on every backend endpoint that changes account, device, or NFC data.
Backend contracts used by NFC
| Endpoint | Client use |
|---|---|
POST /nfc-tag/{id}/diversify |
Requests the tag-specific diversified key using tag version data, counter, mobile-device identity, and attributes. |
POST /nfc-tags/{id}/authenticate |
Supports the backend challenge-authentication client path. |
POST /nfc/ship |
Ships UID and generated serial information from the legacy NDEF path. |
The main provisioned-tag exchange currently requests the diversified master key and completes EV2 authentication on the phone. Keep DTO names, authorization headers, timeouts, and endpoint paths synchronized with the backend.
Testing strategy
- Local unit tests cover crypto helpers, ISO-DEP response parsing, display models, model selection, JSON contracts, and backend-client behavior.
- Instrumented tests exercise NTAG 424 and DESFire EV3 processors with Android APIs and compatible physical or controlled test infrastructure.
- Before release, test fresh provisioning, repeat authentication, wrong-operator rejection, missing local-key recovery, legacy operator-file migration, tag removal, AUTH_DELAY, and backend timeout.
- Run a real-device smoke test for every application flavor and signing configuration that changes OAuth or backend endpoints.