|
| 1 | +/* |
| 2 | + * Copyright 2025 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.wear.snippets.auth |
| 18 | + |
| 19 | +import android.app.Activity |
| 20 | +import android.content.Context |
| 21 | +import android.os.Bundle |
| 22 | +import androidx.credentials.Credential |
| 23 | +import androidx.credentials.CredentialManager |
| 24 | +import androidx.credentials.CustomCredential |
| 25 | +import androidx.credentials.GetCredentialRequest |
| 26 | +import androidx.credentials.GetCredentialResponse |
| 27 | +import androidx.credentials.GetPasswordOption |
| 28 | +import androidx.credentials.GetPublicKeyCredentialOption |
| 29 | +import androidx.credentials.PasswordCredential |
| 30 | +import androidx.credentials.PublicKeyCredential |
| 31 | +import androidx.credentials.exceptions.GetCredentialCancellationException |
| 32 | +import com.google.android.libraries.identity.googleid.GetGoogleIdOption |
| 33 | + |
| 34 | +/** |
| 35 | + * Handles authentication operations using the Android Credential Manager API. |
| 36 | + * |
| 37 | + * This class interacts with an [AuthenticationServer] to facilitate sign-in processes |
| 38 | + * using Passkeys, Passwords, and Sign-In With Google credentials. |
| 39 | + * |
| 40 | + * @param context The Android [Context] used to create the [CredentialManager]. |
| 41 | + * @param authenticationServer The [AuthenticationServer] responsible for handling authentication requests. |
| 42 | + */ |
| 43 | +class CredentialManagerAuthenticator( |
| 44 | + applicationContext: Context, |
| 45 | + private val authenticationServer: AuthenticationServer, |
| 46 | +) { |
| 47 | + private val credentialManager: CredentialManager = CredentialManager.create(applicationContext) |
| 48 | + |
| 49 | + internal suspend fun signInWithCredentialManager(activity: Activity): Boolean { |
| 50 | + // [START android_wear_credential_manager_secondary_fallback] |
| 51 | + try { |
| 52 | + val getCredentialResponse: GetCredentialResponse = |
| 53 | + credentialManager.getCredential(activity, createGetCredentialRequest()) |
| 54 | + return authenticate(getCredentialResponse.credential) |
| 55 | + } catch (_: GetCredentialCancellationException) { |
| 56 | + navigateToSecondaryAuthentication() |
| 57 | + } |
| 58 | + // [END android_wear_credential_manager_secondary_fallback] |
| 59 | + return false |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Creates a [GetCredentialRequest] with standard Wear Credential types. |
| 64 | + * |
| 65 | + * @return A configured [GetCredentialRequest] ready to be used with [CredentialManager.getCredential]. |
| 66 | + */ |
| 67 | + private fun createGetCredentialRequest(): GetCredentialRequest { |
| 68 | + return GetCredentialRequest( |
| 69 | + credentialOptions = listOf( |
| 70 | + GetPublicKeyCredentialOption(authenticationServer.getPublicKeyRequestOptions()), |
| 71 | + GetPasswordOption(), |
| 72 | + GetGoogleIdOption.Builder() |
| 73 | + .setServerClientId("<Your Google Sign in Server Client ID here.").build(), |
| 74 | + ), |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Routes the credential received from `getCredential` to the appropriate authentication |
| 80 | + * type handler on the [AuthenticationServer]. |
| 81 | + * |
| 82 | + * @param credential The selected cre |
| 83 | + * @return `true` if the credential was successfully processed and authenticated, else 'false'. |
| 84 | + */ |
| 85 | + private fun authenticate(credential: Credential): Boolean { |
| 86 | + when (credential) { |
| 87 | + is PublicKeyCredential -> { |
| 88 | + return authenticationServer.loginWithPasskey(credential.authenticationResponseJson) |
| 89 | + } |
| 90 | + |
| 91 | + is PasswordCredential -> { |
| 92 | + return authenticationServer.loginWithPassword( |
| 93 | + credential.id, |
| 94 | + credential.password, |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + is CustomCredential -> { |
| 99 | + return authenticationServer.loginWithCustomCredential( |
| 100 | + credential.type, |
| 101 | + credential.data, |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + else -> { |
| 106 | + return false |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/** Placeholder authentication server would make network calls to your authentication server.*/ |
| 113 | +class AuthenticationServer { |
| 114 | + |
| 115 | + /** Retrieves the public key credential request options from the authentication server.*/ |
| 116 | + internal fun getPublicKeyRequestOptions(): String { |
| 117 | + return "result of network call" |
| 118 | + } |
| 119 | + |
| 120 | + fun loginWithPasskey(passkeyResponseJSON: String): Boolean { |
| 121 | + return true |
| 122 | + } |
| 123 | + |
| 124 | + fun loginWithPassword(username: String, password: String): Boolean { |
| 125 | + return true |
| 126 | + } |
| 127 | + |
| 128 | + fun loginWithCustomCredential(type: String, data: Bundle): Boolean { |
| 129 | + return true |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +/** placeholder navigation function. */ |
| 134 | +fun navigateToSecondaryAuthentication() { |
| 135 | +} |
0 commit comments