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)
}
예외 처리
발급 흐름이 실패하면 createCredential에서 CreateCredentialException을 발생시키며, 앱은 이를 처리해야 합니다.
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
}
}
}