Android ऐप्लिकेशन में डिजिटल क्रेडेंशियल की पुष्टि करने की सुविधा का इस्तेमाल, उपयोगकर्ता की पहचान (जैसे, सरकारी आईडी), उस उपयोगकर्ता की प्रॉपर्टी (जैसे, ड्राइविंग लाइसेंस, शैक्षणिक डिग्री या उम्र या पता जैसे एट्रिब्यूट) की पुष्टि करने और उसे अनुमति देने के लिए किया जा सकता है. इसके अलावा, इस सुविधा का इस्तेमाल ऐसे अन्य मामलों में भी किया जा सकता है जहां किसी इकाई की पुष्टि करने के लिए, क्रेडेंशियल जारी करना और उसकी पुष्टि करना ज़रूरी हो.
डिजिटल क्रेडेंशियल, W3C का एक सार्वजनिक स्टैंडर्ड है. इससे, डिजिटल वॉलेट में मौजूद उपयोगकर्ता के ऐसे डिजिटल क्रेडेंशियल को ऐक्सेस करने का तरीका पता चलता है जिनकी पुष्टि की जा सकती है. इसे W3C क्रेडेंशियल मैनेजमेंट एपीआई की मदद से, वेब के इस्तेमाल के उदाहरणों के लिए लागू किया जाता है. Android पर, डिजिटल क्रेडेंशियल की पुष्टि करने के लिए, Credential Manager के DigitalCredential
एपीआई का इस्तेमाल किया जाता है.
लागू करना
अपने Android प्रोजेक्ट में डिजिटल क्रेडेंशियल की पुष्टि करने के लिए, यह तरीका अपनाएं:
- अपने ऐप्लिकेशन की बिल्ड स्क्रिप्ट में डिपेंडेंसी जोड़ें और
CredentialManager
क्लास को शुरू करें. - डिजिटल क्रेडेंशियल का अनुरोध बनाएं और इसका इस्तेमाल
DigitalCredentialOption
को शुरू करने के लिए करें. इसके बाद,GetCredentialRequest
बनाएं. GetCredentialResponse
पाने के लिए, बनाए गए अनुरोध के साथgetCredential
फ़्लो लॉन्च करें या किसी भी अपवाद को मैनेज करें. डेटा वापस पाने के बाद, जवाब की पुष्टि करें.
डिपेंडेंसी जोड़ना और शुरू करना
अपनी 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 सिस्टम डायलॉग को ट्रिगर करता है, ताकि उपयोगकर्ता को क्रेडेंशियल के उपलब्ध विकल्प दिखाए जा सकें और उनका चुना गया विकल्प इकट्ठा किया जा सके. इसके बाद, चुने गए क्रेडेंशियल का विकल्प देने वाला वॉलेट ऐप्लिकेशन, सहमति इकट्ठा करने के लिए यूज़र इंटरफ़ेस (यूआई) दिखाएगा. साथ ही, डिजिटल क्रेडेंशियल का जवाब जनरेट करने के लिए ज़रूरी कार्रवाइयां करेगा.
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}")
}
}