Digital Credentials API की मदद से, ईमेल पते की पुष्टि करने की सुविधा लागू करना

खास जानकारी

डिपेंडेंसी जोड़ें

अपने ऐप्लिकेशन की build.gradle फ़ाइल में, Credential Manager के लिए ये डिपेंडेंसी जोड़ें:

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

Credential Manager शुरू करना

CredentialManager ऑब्जेक्ट बनाने के लिए, अपने ऐप्लिकेशन या गतिविधि के कॉन्टेक्स्ट का इस्तेमाल करें.

// Use your app or activity context to instantiate a client instance of
// CredentialManager.
private val credentialManager = CredentialManager.create(context)

डिजिटल क्रेडेंशियल का अनुरोध बनाना

पुष्टि किए गए ईमेल पते का अनुरोध करने के लिए, GetCredentialRequest बनाएं. इसमें GetDigitalCredentialOption शामिल होना चाहिए. इस विकल्प के लिए, OpenID for Verifiable Presentations (OpenID4VP) के अनुरोध के तौर पर फ़ॉर्मैट की गई requestJson स्ट्रिंग ज़रूरी है.

OpenID4VP के अनुरोध के JSON का स्ट्रक्चर तय होना चाहिए. मौजूदा प्रोवाइडर, "digital": {"requests": [...]} रैपर के साथ JSON स्ट्रक्चर के साथ काम करते हैं.

    val nonce = generateSecureRandomNonce()

    // This request follows the OpenID4VP spec
    val openId4vpRequest = """
{
  "requests": [
    {
      "protocol": "openid4vp-v1-unsigned",
      "data": {
        "response_type": "vp_token",
        "response_mode": "dc_api",
        "nonce": "$nonce",
        "dcql_query": {
          "credentials": [
            {
              "id": "user_info_query",
              "format": "dc+sd-jwt",
               "meta": { 
                  "vct_values": ["UserInfoCredential"] 
               },
              "claims": [ 
                {"path": ["email"]}, 
                {"path": ["name"]},  
                {"path": ["given_name"]},
                {"path": ["family_name"]},
                {"path": ["picture"]},
                {"path": ["hd"]},
                {"path": ["email_verified"]}
              ]
            }
          ]
        }
      }
    }
  ]
}
"""

    val getDigitalCredentialOption = GetDigitalCredentialOption(requestJson = openId4vpRequest)
    val request = GetCredentialRequest(listOf(getDigitalCredentialOption))
    email_verified

इसके बाद, openId4vpRequest JSON को GetDigitalCredentialOption में रैप करें. साथ ही, GetCredentialRequest बनाएं और getCredential() को कॉल करें.

उपयोगकर्ता को अनुरोध दिखाना

Credential Manager के पहले से मौजूद यूज़र इंटरफ़ेस (यूआई) का इस्तेमाल करके, उपयोगकर्ता को अनुरोध दिखाएं.

try {
    // Requesting Digital Credential from user...
    val result = credentialManager.getCredential(activity, request)

    when (val credential = result.credential) {
        is DigitalCredential -> {
            val responseJsonString = credential.credentialJson

            // Successfully received digital credential response.

            // Next, parse this response and send it to your server.
            // ...
        }

        else -> {
            // handle Unexpected State() - Up to the developer
        }
    }
} catch (e: Exception) {
    // handle exceptions - Up to the developer
}

क्लाइंट पर जवाब पार्स करना

जवाब मिलने के बाद, क्लाइंट पर शुरुआती तौर पर पार्स किया जा सकता है. यह यूज़र इंटरफ़ेस (यूआई) को तुरंत अपडेट करने के लिए काम का है. जैसे, उपयोगकर्ता का नाम दिखाना.

यहां दिए गए कोड से, रॉ Selective Disclosure JWT (SD-JWT) निकाला जाता है. साथ ही, इसके दावों को डिकोड करने के लिए, हेल्पर का इस्तेमाल किया जाता है.

// 1. Parse the outer JSON wrapper to get the `vp_token`
val responseData = JSONObject(responseJsonString)
val vpToken = responseData.getJSONObject("vp_token")

// 2. Extract the raw SD-JWT string
val credentialId = vpToken.keys().next()
val rawSdJwt = vpToken.getJSONArray(credentialId).getString(0)

// 3. Use your parser to get the verified claims
// Server-side validation/parsing is highly recommended.

// Assumes a local parser like the one in our SdJwtParser.kt sample
val claims = SdJwtParser.parse(rawSdJwt)
Log.d("TAG", "Parsed Claims: ${claims.toString(2)}")

// 4. Create your VerifiedUserInfo object with REAL data
val userInfo = VerifiedUserInfo(
    email = claims.getString("email"),
    displayName = claims.optString("name", claims.getString("email"))
)

जवाब मैनेज करना

Credential Manager API, DigitalCredential का जवाब देगा.

यहां एक उदाहरण दिया गया है कि रॉ responseJsonString कैसा दिखता है. साथ ही, अंदर मौजूद SD-JWT को पार्स करने के बाद, दावे कैसे दिखते हैं. इसमें आपको पुष्टि किए गए ईमेल पते के साथ-साथ, अतिरिक्त मेटाडेटा भी मिलता है:

/*
// Example of the raw JSON response from credential.credentialJson:
{
  "vp_token": {
    // This key matches the 'id' you set in your dcql_query
    "user_info_query": [
      // The SD-JWT string (Issuer JWT ~ Disclosures ~ Key Binding JWT)
      "eyJhbGciOiJ...~WyI...IiwgImVtYWlsIiwgInVzZXJAZXhhbXBsZS5jb20iXQ~...~eyJhbGciOiJ..."
    ]
  }
}

// Example of the parsed and verified claims from the SD-JWT on your server:
{
  "cnf": {
    "jwk": {..}
  },
  "exp": 1775688222,
  "iat": 1775083422,
  "iss": "https://verifiablecredentials-pa.googleapis.com",
  "vct": "UserInfoCredential",
  "email": "jane.doe.246745@gmail.com",
  "email_verified": true,
  "given_name": "Jane",
  "family_name": "Doe",
  "name": "Jane Doe",
  "picture": "http://example.com/janedoe/me.jpg",
  "hd": ""
}
 */

खाता बनाने के लिए, सर्वर-साइड से होने वाली पुष्टि

पुष्टि करने के लिए, आपके ऐप्लिकेशन को खाता बनाने या उपयोगकर्ता को लॉग इन करने से पहले, क्रिप्टोग्राफ़िक पुष्टि के लिए, पूरा responseJsonString आपके सर्वर पर भेजना होगा.

  • डेटा की असलियत: जारी करने वाली कंपनी (iss) यूआरएल और SD-JWT के हस्ताक्षर की पुष्टि करने से यह साबित होता है कि इस डेटा को किसी भरोसेमंद सर्टिफ़िकेट देने वाली संस्था या निकाय ने जारी किया है.

सर्वर पर की जाने वाली पुष्टि में, ये चीज़ें शामिल होनी चाहिए:

  • जारी करने वाले की पुष्टि करना: पक्का करें कि iss (जारी करने वाला) फ़ील्ड, https://verifiablecredentials-pa.googleapis.com से मेल खाता हो.
  • हस्ताक्षर की पुष्टि करना: https://verifiablecredentials-pa.googleapis.com/.well-known/vc-public-jwks पर उपलब्ध सार्वजनिक पासकोड (JWK) का इस्तेमाल करके, SD-JWT के हस्ताक्षर की जांच करें.

पूरी सुरक्षा के लिए, पक्का करें कि आपने रीप्ले हमलों को रोकने के लिए, nonce की भी पुष्टि की हो.

try {
    // Send the raw credential response and the original nonce to your server.
    // Your server must validate the response. createAccountWithVerifiedCredentials
    // is a custom implementation per each RP for server side verification and account creation.
    val serverResponse = createAccountWithVerifiedCredentials(responseJsonString, nonce)

    // Server returns the new account info (e.g., email, name)
    val claims = JSONObject(serverResponse.json)

    val userInfo = VerifiedUserInfo(
        email = claims.getString("email"),
        displayName = claims.optString("name", claims.getString("email"))
    )

    // handle response - Up to the developer
} catch (e: Exception) {
    // handle exceptions - Up to the developer
}

खाता प्रोविज़न करने के बाद, पासकी बनाना एक ज़रूरी कदम है. हालांकि, यह ज़रूरी नहीं है. हमारा सुझाव है कि खाता प्रोविज़न करने के तुरंत बाद, पासकी बनाएं. इससे उपयोगकर्ता को साइन इन करने का सुरक्षित और पासवर्ड रहित तरीका मिलता है. यह फ़्लो, पासकी के सामान्य रजिस्ट्रेशन जैसा ही होता है.

WebView पर फ़्लो काम करे, इसके लिए डेवलपर को हैंडऑफ़ की सुविधा देने के लिए, JavaScript ब्रिज (JS Bridge) लागू करना चाहिए. इस ब्रिज की मदद से, WebView ऑब्जेक्ट, नेटिव ऐप्लिकेशन को सिग्नल दे सकता है. इसके बाद, यह Credential Manager API को कॉल कर सकता है.