Bringing seamless authentication to your apps with passkeys using Credential Manager API | by Niharika Arora | Android Developers | Mar, 2023 | Medium

Traditional authentication solutions pose a number of security and usability challenges. The Credential Manager API helps you and your users overcome these challenges.

Passwords are widely used but…

Android has created the Credential Manager API to simplify the sign-in experience and address security risks by supporting passkeys, the next generation industry standard for passwordless authentication.

Credential Manager brings together support for passkeys and combines it with traditional authentication methods such as passwords, Sign in with Google etc.

When interacting with apps that support passkeys, using the Credential Manager API, users can create passkeys and store them in the Google Password Manager. The Google Password Manager then syncs those passkeys across all the Android devices where that user has signed in. Users can also use passkeys to authenticate in a seamless and secure way. In addition to supporting passkeys, Credential Manager also supports existing sign in methods, such as user-id/password (from Google Password Manager) and Google Sign In.

This blog explains how to create a passkey using the Credential Manager API, and how to authenticate the user via the API .

Users can secure access to their device via biometrics or a standard screen lock credential (PIN, pattern or password).

In the context of passkeys, biometric authentication can be used in conjunction with — or instead of — traditional authentication methods such as passwords or PINs. When using biometric authentication, the user provides their biometric data, which is then compared to the stored biometric data on device to confirm the user’s identity.

Using biometric authentication can provide a higher level of security than traditional authentication methods, as it is much more difficult for an unauthorized user to imitate someone’s unique biometric characteristics. Biometric information and processing continues to stay on the device and is never sent to any remote server — the server only sees an assurance that the biometric check was successful.

The first step to enable support for passkeys for your Android app is to associate your app and the website.

To achieve this, declare associations by hosting a Digital Asset Links JSON file on your website, and add a link to the Digital Asset Link file to your app’s manifest.

The Digital Asset Links protocol and API enable an app or website to make public, verifiable statements about other apps or websites. For example, a website can declare that it is associated with a specific Android app, or it can declare that it wants to share user credentials with another website.

To know more on how to associate your app with a website, please check our official documentation.

The four components of the passkey registration flow are:

You need to create a passkey, associate it with a user’s account, and store the passkey’s public key on your server before the user can sign in with it.

Diagram illustrating the creation of a passkey, showing the moving of information from the backend into the createCredential(…) method

Create a passkey

To create a passkey:

Note : The ideal UX would be that the passkey creation is suggested after the user signs in, without the need to go to settings and have the user manually look for the ‘create a passkey’ button.

Associate a passkey with a user’s account

This is the information returned from the backend:

2. Now call createCredential(<info returned from the backend>) request, which is a suspendable function and waits for the user to consent to your app’s use of a passkey, as well as which credential provider, and account it should be stored with.

Note : You need to initialize a CredentialManager object before calling createCredential() method:

Next, create a passkey :

Note: Even with the same parameter object, the above call creates a new credential every time it is called. Use the exclude list to prevent registering the same authenticator twice. See excludeCredentials above

The system displays this UI near the bottom of the screen.

3. A passkey is created after receiving the user’s consent using the device’s screen lock/biometrics. The credential provider creates a new asymmetric key pair and securely stores the private key on the device.

4. After the suspendable call is resolved, a public key credential containing a new public key, a credential ID, and other attestation data is returned to your app.

Store the passkey’s public key on your server

Your app should now send the public key credential to the backend. The backend stores the credential ID and the public key associated with the user account for future authentications.

The public key credential object contains the following properties:

After the credentials get registered on the server successfully, the user can use the passkey the next time they sign in to your app.

Save the credential info in your app for future use

You can then store the information retrieved from the credential to the on-device database for future use. The following list includes some typical properties to save:

To authenticate a user with a previously-created passkey, follow the steps in this section. The API and components to be used are the same as used when you create a passkey (described above). Only the workflow differs:

Diagram illustrating the authentication  of a passkey

5. Build the sign-in request, using the options retrieved from the previous step:

6. Now, call credentialManager.getCredential(…) . The api validates the rp.id against the origin, and hashes the clientData.

7. The credential provider finds a credential that matches the Relying Party ID and prompts the user to consent to the authentication. Once the user selects a passkey to authenticate and the steps are successful, the credential provider creates a new assertion by signing over the clientDataHash and authenticatorData with the private key generated for this account during registration.

8. The authenticator returns the authenticatorData and assertion signature back to the client.

9. The credential manager APIs return a PublicKeyCredential object that contains the AuthenticatorAssertionResponse, which is returned to the RP to finalize the authentication.

Send the returned public key credential to your app server #

response.authenticatorData: An ArrayBuffer of authenticator data. This field contains information such as the RP ID. The authenticator data structure is a byte array of 37 bytes or more.

response.signature: An ArrayBuffer of the signature. This value is the core of the credential and needs to be verified on the server. This is the signature generated by the private key associated with this credential. On the server, the public key will be used to verify that this signature is valid.

response.userHandle: An ArrayBuffer that contained the user ID that was set at creation time. This value can be used, instead of the credential ID, if the server needs to pick the ID values that it uses, or if the backend wishes to avoid creating an index on credential IDs. This field represents the user.id that was supplied during registration. It can be used to relate this assertion to the user on the server.

authenticatorAttachment: Returns platform when this credential came from the local device. Otherwise cross-platform, notably when the user used a phone to sign in. If the user needs to use a phone to sign-in, consider prompting them to create a passkey on the local device.

type: This field is always set to “public-key”.

Validate the Authentication data

Support for 3rd-party password managers is available on devices that run Android 14 or higher.

Now let’s take a moment to summarize what we discussed in this blog today!

Here are the key takeaways for developers:

To know more about how the API and methods work, you can read through our documentation here: https://developer.android.com/training/sign-in/passkeys

Next Post

How to Maximize Your Content Output on TikTok [VIDEO]

Excerpt from the TikTok Advertisement Masterclass The Ford Method is a tactic for maximizing inventive output by breaking the imaginative approach into modular elements. Instead than considering of an advertisement as a singular entity, the Ford Approach breaks it up into more compact sections, these types of as opening hooks, […]
How to Maximize Your Content Output on TikTok [VIDEO]

You May Like