Class2BiometricOrCredentialAuthExtensionsKt

Added in 1.4.0-alpha02

public final class Class2BiometricOrCredentialAuthExtensionsKt


Summary

Public methods

static final @NonNull BiometricPrompt.AuthenticationResult

Shows an authentication prompt to the user.

static final @NonNull BiometricPrompt.AuthenticationResult
authenticateWithClass2BiometricsOrCredentials(
    @NonNull Fragment receiver,
    @NonNull CharSequence title,
    CharSequence subtitle,
    CharSequence description,
    boolean confirmationRequired
)

Prompts the user to authenticate with a Class 2 biometric (e.g. fingerprint, face, or iris) or the screen lock credential (i.e. PIN, pattern, or password) for the device.

static final @NonNull BiometricPrompt.AuthenticationResult
authenticateWithClass2BiometricsOrCredentials(
    @NonNull FragmentActivity receiver,
    @NonNull CharSequence title,
    CharSequence subtitle,
    CharSequence description,
    boolean confirmationRequired
)

Prompts the user to authenticate with a Class 2 biometric (e.g. fingerprint, face, or iris) or the screen lock credential (i.e. PIN, pattern, or password) for the device.

static final @NonNull AuthPrompt
startClass2BiometricOrCredentialAuthentication(
    @NonNull Fragment receiver,
    @NonNull CharSequence title,
    CharSequence subtitle,
    CharSequence description,
    boolean confirmationRequired,
    Executor executor,
    @NonNull AuthPromptCallback callback
)

Prompts the user to authenticate with a Class 2 biometric (e.g. fingerprint, face, or iris) or the screen lock credential (i.e. PIN, pattern, or password) for the device.

static final @NonNull AuthPrompt
startClass2BiometricOrCredentialAuthentication(
    @NonNull FragmentActivity receiver,
    @NonNull CharSequence title,
    CharSequence subtitle,
    CharSequence description,
    boolean confirmationRequired,
    Executor executor,
    @NonNull AuthPromptCallback callback
)

Prompts the user to authenticate with a Class 2 biometric (e.g. fingerprint, face, or iris) or the screen lock credential (i.e. PIN, pattern, or password) for the device.

Public methods

public static final @NonNull BiometricPrompt.AuthenticationResult authenticate(
    @NonNull Class2BiometricOrCredentialAuthPrompt receiver,
    @NonNull AuthPromptHost host
)

Shows an authentication prompt to the user.

import androidx.biometric.BiometricPrompt
import androidx.biometric.auth.AuthPromptErrorException
import androidx.biometric.auth.AuthPromptFailureException
import androidx.biometric.auth.AuthPromptHost
import androidx.biometric.auth.Class2BiometricOrCredentialAuthPrompt
import androidx.biometric.auth.CredentialAuthPrompt
import androidx.biometric.auth.authenticate

val payload = "A message to encrypt".toByteArray(Charset.defaultCharset())

// Construct AuthPrompt with localized Strings to be displayed to UI.
val authPrompt =
    Class2BiometricOrCredentialAuthPrompt.Builder(title)
        .apply {
            setSubtitle(subtitle)
            setDescription(description)
            setConfirmationRequired(true)
        }
        .build()

try {
    val authResult = authPrompt.authenticate(AuthPromptHost(this))

    // Encrypt a payload using the result of crypto-based auth.
    val encryptedPayload = authResult.cryptoObject?.cipher?.doFinal(payload)

    // Use the encrypted payload somewhere interesting.
    sendEncryptedPayload(encryptedPayload)
} catch (e: AuthPromptErrorException) {
    // Handle irrecoverable error during authentication.
    // Possible values for AuthPromptErrorException.errorCode are listed in the @IntDef,
    // androidx.biometric.BiometricPrompt.AuthenticationError.
} catch (e: AuthPromptFailureException) {
    // Handle auth failure due biometric credentials being rejected.
}
Parameters
@NonNull AuthPromptHost host

A wrapper for the component that will host the prompt.

Returns
@NonNull BiometricPrompt.AuthenticationResult

AuthenticationResult for a successful authentication.

Throws
androidx.biometric.auth.AuthPromptErrorException

when an unrecoverable error has been encountered and authentication has stopped.

androidx.biometric.auth.AuthPromptFailureException

when an authentication attempt by the user has been rejected.

See also
authenticate

( AuthPromptHost, AuthPromptCallback )

authenticateWithClass2BiometricsOrCredentials

public static final @NonNull BiometricPrompt.AuthenticationResult authenticateWithClass2BiometricsOrCredentials(
    @NonNull Fragment receiver,
    @NonNull CharSequence title,
    CharSequence subtitle,
    CharSequence description,
    boolean confirmationRequired
)

Prompts the user to authenticate with a Class 2 biometric (e.g. fingerprint, face, or iris) or the screen lock credential (i.e. PIN, pattern, or password) for the device.

Note that Class 3 biometrics are guaranteed to meet the requirements for Class 2 and thus will also be accepted.

Parameters
@NonNull CharSequence title

The title to be displayed on the prompt.

CharSequence subtitle

An optional subtitle to be displayed on the prompt.

CharSequence description

An optional description to be displayed on the prompt.

boolean confirmationRequired

Whether user confirmation should be required for passive biometrics.

Returns
@NonNull BiometricPrompt.AuthenticationResult

An AuthPrompt handle to the shown prompt.

Throws
androidx.biometric.auth.AuthPromptErrorException

when an unrecoverable error has been encountered and authentication has stopped.

androidx.biometric.auth.AuthPromptFailureException

when an authentication attempt by the user has been rejected.

authenticateWithClass2BiometricsOrCredentials

public static final @NonNull BiometricPrompt.AuthenticationResult authenticateWithClass2BiometricsOrCredentials(
    @NonNull FragmentActivity receiver,
    @NonNull CharSequence title,
    CharSequence subtitle,
    CharSequence description,
    boolean confirmationRequired
)

Prompts the user to authenticate with a Class 2 biometric (e.g. fingerprint, face, or iris) or the screen lock credential (i.e. PIN, pattern, or password) for the device.

Note that Class 3 biometrics are guaranteed to meet the requirements for Class 2 and thus will also be accepted.

Parameters
@NonNull CharSequence title

The title to be displayed on the prompt.

CharSequence subtitle

An optional subtitle to be displayed on the prompt.

CharSequence description

An optional description to be displayed on the prompt.

boolean confirmationRequired

Whether user confirmation should be required for passive biometrics.

Returns
@NonNull BiometricPrompt.AuthenticationResult

AuthenticationResult for a successful authentication.

Throws
androidx.biometric.auth.AuthPromptErrorException

when an unrecoverable error has been encountered and authentication has stopped.

androidx.biometric.auth.AuthPromptFailureException

when an authentication attempt by the user has been rejected.

startClass2BiometricOrCredentialAuthentication

public static final @NonNull AuthPrompt startClass2BiometricOrCredentialAuthentication(
    @NonNull Fragment receiver,
    @NonNull CharSequence title,
    CharSequence subtitle,
    CharSequence description,
    boolean confirmationRequired,
    Executor executor,
    @NonNull AuthPromptCallback callback
)

Prompts the user to authenticate with a Class 2 biometric (e.g. fingerprint, face, or iris) or the screen lock credential (i.e. PIN, pattern, or password) for the device.

Note that Class 3 biometrics are guaranteed to meet the requirements for Class 2 and thus will also be accepted.

Parameters
@NonNull CharSequence title

The title to be displayed on the prompt.

CharSequence subtitle

An optional subtitle to be displayed on the prompt.

CharSequence description

An optional description to be displayed on the prompt.

boolean confirmationRequired

Whether user confirmation should be required for passive biometrics.

Executor executor

An executor for callback methods. If null, these will run on the main thread.

@NonNull AuthPromptCallback callback

The object that will receive and process authentication events.

Returns
@NonNull AuthPrompt

An AuthPrompt handle to the shown prompt.

startClass2BiometricOrCredentialAuthentication

public static final @NonNull AuthPrompt startClass2BiometricOrCredentialAuthentication(
    @NonNull FragmentActivity receiver,
    @NonNull CharSequence title,
    CharSequence subtitle,
    CharSequence description,
    boolean confirmationRequired,
    Executor executor,
    @NonNull AuthPromptCallback callback
)

Prompts the user to authenticate with a Class 2 biometric (e.g. fingerprint, face, or iris) or the screen lock credential (i.e. PIN, pattern, or password) for the device.

Note that Class 3 biometrics are guaranteed to meet the requirements for Class 2 and thus will also be accepted.

Parameters
@NonNull CharSequence title

The title to be displayed on the prompt.

CharSequence subtitle

An optional subtitle to be displayed on the prompt.

CharSequence description

An optional description to be displayed on the prompt.

boolean confirmationRequired

Whether user confirmation should be required for passive biometrics.

Executor executor

An executor for callback methods. If null, these will run on the main thread.

@NonNull AuthPromptCallback callback

The object that will receive and process authentication events.

Returns
@NonNull AuthPrompt

An AuthPrompt handle to the shown prompt.