Phát hành thông tin đăng nhập kỹ thuật số

API Trình quản lý thông tin xác thực cho phép bạn cấp thông tin xác thực cho các ứng dụng lưu trữ (còn gọi là "ví") trên Android. Hướng dẫn này giải thích cách cấp thông tin xác thực cho ứng dụng lưu trữ mà người dùng ưu tiên.

Triển khai

Phần này trình bày chi tiết các bước cần thiết để cấp thông tin xác thực kỹ thuật số.

Thêm phần phụ thuộc

Thêm các phần phụ thuộc sau vào tập lệnh bản dựng 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"
}

Khởi chạy Trình quản lý thông tin xác thực

Khởi động một thực thể của lớp CredentialManager.

val credentialManager = CredentialManager.create(context)

Tạo yêu cầu phát hành

Yêu cầu tạo thông tin xác thực kỹ thuật số phải chứa một chuỗi JSON tuân theo giao thức tiêu chuẩn OpenID4VCI. Sau đây là ví dụ về giao diện của một yêu cầu 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": "..."
        }
      }
    }
  }
]

Tạo một CreateDigitalCredentialRequest chứa yêu cầu phát hành.

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

Gửi yêu cầu phát hành

Cấp thông tin xác thực cho người dùng bằng hàm createCredential. Hàm này sẽ chạy bộ chọn bảng dưới cùng của Trình quản lý thông tin xác thực, cho phép người dùng chọn ứng dụng lưu trữ mà họ muốn lưu trữ thông tin xác thực.

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

Xử lý phản hồi

Sau khi bạn gửi yêu cầu phát hành, hệ thống sẽ trả về một CreateDigitalCredentialResponse. Phản hồi này chứa một chuỗi responseJson mô tả kết quả của việc phát hành.

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

Xử lý ngoại lệ

Nếu quy trình phát hành không thành công, createCredential sẽ gửi một CreateCredentialException mà ứng dụng của bạn cần xử lý:

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
        }
    }
}