การยืนยันข้อมูลเข้าสู่ระบบแบบดิจิทัลภายในแอป Android สามารถใช้เพื่อตรวจสอบสิทธิ์และอนุญาตตัวตนของผู้ใช้ (เช่น บัตรประจำตัวประชาชน) พร็อพเพอร์ตี้เกี่ยวกับผู้ใช้รายนั้น (เช่น ใบขับขี่ ปริญญา หรือแอตทริบิวต์ เช่น อายุหรือที่อยู่) หรือสถานการณ์อื่นๆ ที่ต้องออกและยืนยันข้อมูลเข้าสู่ระบบเพื่อยืนยันความถูกต้องของนิติบุคคล
การรับรองทางดิจิทัลเป็นมาตรฐานสาธารณะของ W3C ที่ระบุวิธีเข้าถึงการรับรองทางดิจิทัลที่ยืนยันได้ของผู้ใช้จากกระเป๋าสตางค์ดิจิทัล และนำมาใช้งานสำหรับกรณีการใช้งานบนเว็บด้วย W3C Credential Management API ใน Android ระบบจะใช้ DigitalCredential
API ของ Credential Manager เพื่อยืนยันข้อมูลเข้าสู่ระบบดิจิทัล
การใช้งาน
หากต้องการยืนยันข้อมูลเข้าสู่ระบบดิจิทัลในโปรเจ็กต์ Android ให้ทําดังนี้
- เพิ่ม Dependency ลงในสคริปต์บิลด์ของแอปและเริ่มต้นคลาส
CredentialManager
- สร้างคำขอใบรับรองดิจิทัลและใช้เพื่อเริ่มต้น
DigitalCredentialOption
ตามด้วยการสร้างGetCredentialRequest
- เปิดใช้งานโฟลว์
getCredential
ด้วยคำขอที่สร้างขึ้นเพื่อรับGetCredentialResponse
ที่สำเร็จหรือจัดการข้อยกเว้นที่อาจเกิดขึ้น เมื่อดึงข้อมูลสําเร็จแล้ว ให้ตรวจสอบคําตอบ
เพิ่มการพึ่งพาและเริ่มต้น
เพิ่ม Dependency ต่อไปนี้ลงในสคริปต์บิลด์ Gradle
dependencies {
implementation("androidx.credentials:credentials:")
implementation("androidx.credentials:credentials-play-services-auth:")
}
จากนั้นเริ่มต้นอินสแตนซ์ของคลาส CredentialManager
val credentialManager = CredentialManager.create(context)
สร้างคําขอข้อมูลเข้าสู่ระบบแบบดิจิทัล
สร้างคำขอข้อมูลเข้าสู่ระบบดิจิทัลและใช้เพื่อเริ่มต้นใช้งาน DigitalCredentialOption
// The request in the JSON format to conform with
// the JSON-ified Digital Credentials 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 มีดังนี้ ดูข้อมูลอ้างอิงฉบับเต็มได้ที่เว็บไซต์นี้
{
"digital": {
"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 เพื่อแสดงตัวเลือกข้อมูลเข้าสู่ระบบที่พร้อมใช้งานของผู้ใช้และรวบรวมรายการที่เลือก จากนั้น แอป Wallet ที่มีตัวเลือกข้อมูลเข้าสู่ระบบที่เลือกจะแสดง 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}")
}
}