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

Credential Manager API cho phép bạn phát hành thông tin xác thực cho các ứng dụng người nắm giữ (còn gọi là "ví") trên Android. Hướng dẫn này giải thích cách lưu thông tin đăng nhập vào trình lưu trữ ưu tiên của người dùng.

Triển khai

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

Thêm phần phụ thuộc

Thêm các phần phụ thuộc sau đây 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 chạy một phiên bản 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ề 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
)

Đưa ra yêu cầu phát hành

Phát hành thông tin đăng nhập cho người dùng bằng hàm createCredential. Hàm này khởi 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 đăng nhập.

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, một CreateDigitalCredentialResponse sẽ được trả về. 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ý các trường hợp ngoại lệ

Nếu quy trình phát hành thất bại, createCredential sẽ gửi ra một CreateCredentialException mà ứng dụng của bạn phải 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
        }
    }
}