Nucleus
Recipes

Lock a screen with biometrics

Hide a screen until the device owner confirms with Touch ID, Face ID, Windows Hello, Android BiometricPrompt or polkit, from shared Kotlin code.

View as Markdown

A vault screen stays locked until the device owner confirms with the system prompt. The code lives in commonMain and runs unchanged on macOS, Windows, Linux, Android and iOS.

This is a gate, not a key: success releases nothing cryptographic. Keep secrets in Keychain access control or the Android Keystore, and use this check to decide what the UI shows.

Add the module

build.gradle.kts
// commonMain in a KMP project, dependencies { } in a JVM desktop app
implementation("dev.nucleusframework:nucleus.authentication:3.0.0")

Lock and unlock

import dev.nucleusframework.authentication.Authenticator

@Composable
fun VaultScreen() {
    var unlocked by remember { mutableStateOf(false) }
    var error by remember { mutableStateOf<AuthenticationError?>(null) }
    val scope = rememberCoroutineScope()
    val available = remember { Authenticator.availability(AuthenticationPolicy.DeviceOwner).isAvailable }

    if (unlocked) {
        VaultContent(onLock = { unlocked = false })
        return
    }

    error?.let { Text("Unlock failed: $it") }
    Button(enabled = available, onClick = {
        scope.launch {
            try {
                Authenticator.authenticate(
                    AuthenticationPrompt(
                        reason = "unlock your vault",
                        linuxActionId = "com.example.app.unlock", // required on Linux
                    ),
                )
                unlocked = true
            } catch (e: AuthenticationException) {
                if (e.error != AuthenticationError.UserCanceled) error = e.error
            }
        }
    }) { Text("Unlock") }
}

DeviceOwner, the default policy, accepts a biometric or whatever unlocks the device instead: the passcode, the macOS login password, the Windows Hello PIN or the password polkit asks for. AuthenticationPolicy.Biometrics refuses those fallbacks, but Windows and Linux cannot enforce it and fail with UnsupportedPolicy.

Inside a Nucleus window, LocalNucleusWindow.current.authenticate(prompt) parents the prompt to that window.

Set up each platform

  • Linux: declare the polkit action. The plugin installs the .policy file with the Deb, RPM and Pacman packages.

    build.gradle.kts
    nucleus.application {
        nativeDistributions {
            linux {
                polkitActions {
                    action("com.example.app.unlock") {
                        message = "Authentication is required to unlock Example"
                    }
                }
            }
        }
    }
  • iOS: add NSFaceIDUsageDescription to Info.plist to use Face ID.

  • Android: nothing to declare; the prompt needs API 28.

  • macOS and Windows: nothing to declare. The Windows Hello dialog cannot be closed by the app.

What's next

Was this page helpful?
Edit on GitHub

Last updated on