Credential


public abstract class Credential

Known direct subclasses
CustomCredential

Base class for a custom credential with which the user consented to authenticate to the app.

DigitalCredential

Represents the user's digital credential, generally used for verification or sign-in purposes.

PasswordCredential

Represents the user's password credential granted by the user for app sign-in.

PublicKeyCredential

Represents the user's passkey credential granted by the user for app sign-in.

RestoreCredential

Represents the user's restore credential for the app sign in.


Base class for a credential with which the user consented to authenticate to the app.

import androidx.credentials.Credential
import androidx.credentials.CustomCredential
import androidx.credentials.PasswordCredential
import androidx.credentials.PublicKeyCredential

when (credential) {
    is PublicKeyCredential -> {
        val responseJson = credential.authenticationResponseJson
        val userCredential = fidoAuthenticateWithServer(responseJson)
        loginWithPasskey(userCredential)
    }
    is PasswordCredential -> {
        val userName = credential.id
        val password = credential.password
        loginWithPassword(userName, password)
    }
    is CustomCredential -> {
        // If you are also using any external sign-in libraries, parse them here with the
        // utility functions provided they provided.
        if (credential.type == ExampleCustomCredential.TYPE) {
            try {
                val exampleCustomCredential =
                    ExampleCustomCredential.createFrom(credential.data)
                loginWithExampleCustomCredential(exampleCustomCredential)
            } catch (e: ExampleCustomCredential.ExampleCustomCredentialParsingException) {
                // Unlikely to happen. If it does, you likely need to update the dependency
                // version of your external sign-in library.
                Log.e(TAG, "Failed to parse an ExampleCustomCredential", e)
            }
        } else {
            Log.w(
                TAG,
                "Received unrecognized credential type ${credential.type}. " +
                    "This shouldn't happen"
            )
        }
    }
    else -> {
        Log.w(
            TAG,
            "Received unrecognized credential type ${credential.type}. This shouldn't happen"
        )
    }
}

Summary

Public methods

static final @NonNull Credential
@RequiresApi(value = 34)
@Discouraged(message = "It is recommended to construct a Credential by directly instantiating a Credential subclass")
createFrom(@NonNull Credential credential)

Parses the credential into an instance of Credential.

static final @NonNull Credential
@Discouraged(message = "It is recommended to construct a Credential by directly instantiating a Credential subclass")
createFrom(@NonNull String type, @NonNull Bundle data)

Parses the raw data into an instance of Credential.

final @NonNull Bundle

the credential data in the Bundle format

final @NonNull String

the credential type determined by the credential-type-specific subclass (e.g. PasswordCredential.TYPE_PASSWORD_CREDENTIAL for PasswordCredential or PublicKeyCredential.TYPE_PUBLIC_KEY_CREDENTIAL for PublicKeyCredential)

Public methods

createFrom

Added in 1.5.0-alpha05
@RequiresApi(value = 34)
@Discouraged(message = "It is recommended to construct a Credential by directly instantiating a Credential subclass")
public static final @NonNull Credential createFrom(@NonNull Credential credential)

Parses the credential into an instance of Credential.

Parameters
@NonNull Credential credential

the framework Credential object

createFrom

Added in 1.5.0-alpha05
@Discouraged(message = "It is recommended to construct a Credential by directly instantiating a Credential subclass")
public static final @NonNull Credential createFrom(@NonNull String type, @NonNull Bundle data)

Parses the raw data into an instance of Credential.

Parameters
@NonNull String type

matches Credential.type, the credential type

@NonNull Bundle data

matches Credential.data, the credential data in the Bundle format; this should be constructed and retrieved from the a given Credential itself and never be created from scratch

getData

Added in 1.2.0
public final @NonNull Bundle getData()

the credential data in the Bundle format

getType

Added in 1.2.0
public final @NonNull String getType()

the credential type determined by the credential-type-specific subclass (e.g. PasswordCredential.TYPE_PASSWORD_CREDENTIAL for PasswordCredential or PublicKeyCredential.TYPE_PUBLIC_KEY_CREDENTIAL for PublicKeyCredential)