デジタル認証情報を発行する

Credential Manager API を使用すると、Android ホルダー(「ウォレット」とも呼ばれます)アプリに認証情報を発行できます。このガイドでは、ユーザーが選択したホルダーに認証情報を保存する方法について説明します。

実装

このセクションでは、デジタル認証情報を発行するために必要な手順について説明します。

依存関係を追加する

gradle ビルド スクリプトに次の依存関係を追加します。

Kotlin

dependencies {
    implementation("androidx.credentials:credentials:1.7.0-alpha02")
    implementation("androidx.credentials:credentials-play-services-auth:1.7.0-alpha02")
}

Groovy

dependencies {
    implementation "androidx.credentials:credentials:1.7.0-alpha02"
    implementation "androidx.credentials:credentials-play-services-auth:1.7.0-alpha02"
}

認証情報マネージャーを初期化する

CredentialManager クラスのインスタンスを初期化します。

val credentialManager = CredentialManager.create(context)

発行リクエストを作成する

デジタル認証情報の作成リクエストには、 OpenID4VCI 標準プロトコルに準拠した JSON 文字列を含める必要があります。OpenID4VCI リクエストの例を次に示します。

"requests": [
  {
    "protocol": "openid4vci-v1",
    "data": {
      "credential_issuer": "https://digital-credentials.dev",
      "credential_configuration_ids": [
        "com.emvco.payment_card"
      ],
      "grants": {
        "urn:ietf:params:oauth:grant-type:pre-authorized_code": {
          "pre-authorized_code": "..."
        }
      }
    }
  }
]

発行リクエストを含む CreateDigitalCredentialRequest を作成します。

val issuanceRequestJson = "{ ... }" // Your issuance JSON
val createRequest = CreateDigitalCredentialRequest(
    requestJson = issuanceRequestJson,
    origin = null
)

発行リクエストを行う

createCredential 関数を使用して、ユーザーのホルダーに認証情報を発行します。この関数は、認証情報マネージャーのボトムシート セレクタを起動します。これにより、ユーザーは認証情報の保存先となるホルダーアプリを選択できます。

try {
    val response = credentialManager.createCredential(
        context = context,
        request = createRequest
    )
    handleSuccess(response as CreateDigitalCredentialResponse)
} catch (e: CreateCredentialException) {
    handleCreateException(e)
}

レスポンスを処理する

発行リクエストを行うと、CreateDigitalCredentialResponse が返されます。このレスポンスには、発行の結果を示す responseJson 文字列が含まれています。

fun handleSuccess(response: CreateDigitalCredentialResponse) {
    val responseJson = response.responseJson
    // Parse responseJson according to your protocol (e.g. OpenID4VCI)
}

例外を処理する

発行フローが失敗した場合、createCredentialCreateCredentialException をスローします。アプリでこの例外を処理する必要があります。

fun handleCreateException(e: CreateCredentialException) {
    when (e) {
        is CreateCredentialCancellationException -> {
            // The user canceled the flow
        }
        is CreateCredentialInterruptedException -> {
            // The flow was interrupted (e.g. by another UI element)
        }
        is CreateCredentialNoCreateOptionException -> {
            // No wallet application is available to handle the request
        }
        is CreateCredentialUnsupportedException -> {
            // The device or the system doesn't support this request
        }
        is CreateCredentialProviderConfigurationException -> {
            // There is a configuration issue with the wallet provider
        }
        is CreateCredentialCustomException -> {
            // A protocol-specific error occurred
            val errorType = e.type
            val errorMessage = e.message
        }
        is CreateCredentialUnknownException -> {
            // An unknown error occurred
        }
        else -> {
            // Generic error handling
        }
    }
}