การยืนยันข้อมูลเข้าสู่ระบบดิจิทัลภายในแอป Android สามารถใช้เพื่อตรวจสอบสิทธิ์และให้สิทธิ์ระบุตัวตนของผู้ใช้ (เช่น บัตรประจำตัวที่ออกโดยรัฐบาล) คุณสมบัติเกี่ยวกับผู้ใช้รายนั้น (เช่น ใบขับขี่ วุฒิการศึกษา หรือแอตทริบิวต์ เช่น อายุหรือที่อยู่) หรือสถานการณ์อื่นๆ ที่ต้องออกและยืนยันข้อมูลเข้าสู่ระบบเพื่อยืนยันความถูกต้องของเอนทิตี
ข้อมูลเข้าสู่ระบบดิจิทัลเป็นมาตรฐานสาธารณะของ W3C ที่ระบุวิธีเข้าถึงข้อมูลเข้าสู่ระบบดิจิทัลที่ตรวจสอบได้ของผู้ใช้จากกระเป๋าเงินดิจิทัล และมีการนำไปใช้สำหรับกรณีการใช้งานบนเว็บด้วย W3C Credential Management API ใน
Android ระบบจะใช้ DigitalCredential API ของ Credential Manager เพื่อ
ยืนยันข้อมูลเข้าสู่ระบบดิจิทัล
ความเข้ากันได้กับ Android เวอร์ชันต่างๆ
Verifier API รองรับใน Android 6 (ระดับ API 23) ขึ้นไป
การใช้งาน
หากต้องการยืนยันข้อมูลเข้าสู่ระบบดิจิทัลในโปรเจ็กต์ Android ให้ทำดังนี้
- เพิ่มทรัพยากร Dependency ลงในสคริปต์บิวด์ของแอปและเริ่มต้นคลาส
CredentialManager - สร้างคำขอข้อมูลเข้าสู่ระบบดิจิทัลและใช้คำขอดังกล่าวเพื่อเริ่มต้น
DigitalCredentialOptionจากนั้นสร้างGetCredentialRequest - เปิดใช้โฟลว์
getCredentialด้วยคำขอที่สร้างขึ้นเพื่อรับGetCredentialResponseที่สำเร็จ หรือจัดการข้อยกเว้นที่อาจเกิดขึ้น เมื่อดึงข้อมูลสำเร็จแล้ว ให้ตรวจสอบการตอบกลับ
เพิ่มทรัพยากร Dependency และเริ่มต้น
เพิ่มทรัพยากร Dependency ต่อไปนี้ลงในสคริปต์บิวด์ Gradle
dependencies {
implementation("androidx.credentials:credentials:1.6.0-beta01")
implementation("androidx.credentials:credentials-play-services-auth:1.6.0-beta01")
}
จากนั้นเริ่มต้นอินสแตนซ์ของคลาส CredentialManager
val credentialManager = CredentialManager.create(context)
สร้างคำขอข้อมูลเข้าสู่ระบบดิจิทัล
สร้างคำขอข้อมูลเข้าสู่ระบบดิจิทัลและใช้คำขอดังกล่าวเพื่อเริ่มต้น DigitalCredentialOption
// The request in the JSON format to conform with
// the JSON-ified Credential Manager - Verifier API request definition.
val requestJson = generateRequestFromServer()
val digitalCredentialOption =
GetDigitalCredentialOption(requestJson = requestJson)
// Use the option from the previous step to build the `GetCredentialRequest`.
val getCredRequest = GetCredentialRequest(
listOf(digitalCredentialOption)
)
นี่คือตัวอย่างคำขอ OpenId4Vp ดูข้อมูลอ้างอิงทั้งหมดได้ที่ เว็บไซต์นี้
{
"requests": [
{
"protocol": "openid4vp-v1-unsigned",
"data": {
"response_type": "vp_token",
"response_mode": "dc_api",
"nonce": "OD8eP8BYfr0zyhgq4QCVEGN3m7C1Ht_No9H5fG5KJFk",
"dcql_query": {
"credentials": [
{
"id": "cred1",
"format": "mso_mdoc",
"meta": {
"doctype_value": "org.iso.18013.5.1.mDL"
},
"claims": [
{
"path": [
"org.iso.18013.5.1",
"family_name"
]
},
{
"path": [
"org.iso.18013.5.1",
"given_name"
]
},
{
"path": [
"org.iso.18013.5.1",
"age_over_21"
]
}
]
}
]
}
}
}
]
}
รับข้อมูลเข้าสู่ระบบ
เปิดใช้โฟลว์ getCredential ด้วยคำขอที่สร้างขึ้น คุณจะได้รับ GetCredentialResponse ที่สำเร็จ หรือ GetCredentialException หากคำขอไม่สำเร็จ
โฟลว์ getCredential จะทริกเกอร์กล่องโต้ตอบของระบบ Android เพื่อแสดงตัวเลือกข้อมูลเข้าสู่ระบบที่มีให้แก่ผู้ใช้และรวบรวมตัวเลือกของผู้ใช้ จากนั้นแอปกระเป๋าเงินที่มีตัวเลือกข้อมูลเข้าสู่ระบบที่เลือกไว้จะแสดง UI เพื่อขอความยินยอมและดำเนินการที่จำเป็นในการสร้างการตอบกลับข้อมูลเข้าสู่ระบบดิจิทัล
coroutineScope.launch {
try {
val result = credentialManager.getCredential(
context = activityContext,
request = getCredRequest
)
verifyResult(result)
} catch (e : GetCredentialException) {
handleFailure(e)
}
}
// Handle the successfully returned credential.
fun verifyResult(result: GetCredentialResponse) {
val credential = result.credential
when (credential) {
is DigitalCredential -> {
val responseJson = credential.credentialJson
validateResponseOnServer(responseJson)
}
else -> {
// Catch any unrecognized credential type here.
Log.e(TAG, "Unexpected type of credential ${credential.type}")
}
}
}
// Handle failure.
fun handleFailure(e: GetCredentialException) {
when (e) {
is GetCredentialCancellationException -> {
// The user intentionally canceled the operation and chose not
// to share the credential.
}
is GetCredentialInterruptedException -> {
// Retry-able error. Consider retrying the call.
}
is NoCredentialException -> {
// No credential was available.
}
is CreateCredentialUnknownException -> {
// An unknown, usually unexpected, error has occurred. Check the
// message error for any additional debugging information.
}
is CreateCredentialCustomException -> {
// You have encountered a custom error thrown by the wallet.
// If you made the API call with a request object that's a
// subclass of CreateCustomCredentialRequest using a 3rd-party SDK,
// then you should check for any custom exception type constants
// within that SDK to match with e.type. Otherwise, drop or log the
// exception.
}
else -> Log.w(TAG, "Unexpected exception type ${e::class.java}")
}
}