Firebase से फ़ोन नंबर की पुष्टि करने की सुविधा (पीएनवी) को इंटिग्रेट करने के बारे में जानकारी देने वाले पेजFirebase Phone Number Verification पर, Firebase PNV को इंटिग्रेट करने का तरीका बताया गया है. इसमें, getVerifiedPhoneNumber() तरीके का इस्तेमाल किया गया है. यह तरीका, Firebase PNV की पूरी प्रोसेस को मैनेज करता है. इसमें, उपयोगकर्ता की सहमति पाने से लेकर, Firebase PNV के बैकएंड पर ज़रूरी नेटवर्क कॉल करने तक की प्रोसेस शामिल है.
ज़्यादातर डेवलपर के लिए, एक ही तरीके वाला एपीआई (getVerifiedPhoneNumber()) इस्तेमाल करने का सुझाव दिया जाता है. हालांकि, अगर आपको
Android क्रेडेंशियल मैनेजर के साथ इंटरैक्शन पर ज़्यादा कंट्रोल चाहिए, तो Firebase PNV लाइब्रेरी, ये दो तरीके भी उपलब्ध कराती है. इनमें से हर तरीका,
Firebase PNV के बैकएंड के साथ अलग-अलग इंटरैक्शन को मैनेज करता है. उदाहरण के लिए, फ़ोन नंबर के साथ-साथ अन्य क्रेडेंशियल का अनुरोध करना:
getDigitalCredentialPayload()से, सर्वर से साइन किया गया अनुरोध मिलता है. इसका इस्तेमाल, क्रेडेंशियल मैनेजर को शुरू करने के लिए किया जाता है.exchangeCredentialResponseForPhoneNumber()से, क्रेडेंशियल मैनेजर से मिले जवाब को, साइन किए गए टोकन से बदला जाता है. इस टोकन में, पुष्टि किया गया फ़ोन नंबर शामिल होता है. इस चरण में बिलिंग की जाएगी.
इनमें से हर तरीके को कॉल करने के बीच, Android के क्रेडेंशियल मैनेजर एपीआई के साथ इंटरैक्शन को मैनेज करने की ज़िम्मेदारी आपकी होती है. इस पेज पर, तीन हिस्सों वाली इस प्रोसेस को लागू करने के तरीके की खास जानकारी दी गई है.
शुरू करने से पहले
अपना Firebase प्रोजेक्ट सेट अप करें और Firebase PNV डिपेंडेंसी इंपोर्ट करें. इसके लिए, शुरू करने का तरीका पेज पर दिया गया तरीका अपनाएं.
1. डिजिटल क्रेडेंशियल के अनुरोध का पेलोड पाना
डिवाइस के फ़ोन नंबर के लिए अनुरोध जनरेट करने के लिए, getDigitalCredentialPayload() तरीके को कॉल करें. अगले चरण में, यह अनुरोध, क्रेडेंशियल मैनेजर एपीआई के साथ आपके इंटरैक्शन का पेलोड होगा.
// This instance does not require an Activity context.
val fpnv = FirebasePhoneNumberVerification.getInstance()
// Your request should include a nonce, which will propagate through the flow
// and be present in the final response from FPNV. See the section "Verifying
// the Firebase PNV token" for details on generating and verifying this.
val nonce = fetchNonceFromYourServer()
fpnv.getDigitalCredentialPayload(nonce, "https://example.com/privacy-policy")
.addOnSuccessListener { fpnvDigitalCredentialPayload ->
// Use the payload in the next step.
// ...
}
.addOnFailureListener { e -> /* Handle payload fetch failure */ }
2. क्रेडेंशियल मैनेजर का इस्तेमाल करके, डिजिटल क्रेडेंशियल का अनुरोध करना
इसके बाद, अनुरोध को क्रेडेंशियल मैनेजर को पास करें.
ऐसा करने के लिए, आपको डिजिटल क्रेडेंशियल एपीआई के अनुरोध में, अनुरोध के पेलोड को रैप करना होगा. इस अनुरोध में, वही नॉनस शामिल होना चाहिए जिसे आपने getDigitalCredentialPayload() को पास किया था.
// This example uses string interpolation for clarity, but you should use some kind of type-safe
// serialization method.
fun buildDigitalCredentialRequestJson(nonce: String, fpnvDigitalCredentialPayload: String) = """
{
"requests": [
{
"protocol": "openid4vp-v1-unsigned",
"data": {
"response_type": "vp_token",
"response_mode": "dc_api",
"nonce": "$nonce",
"dcql_query": { "credentials": [$fpnvDigitalCredentialPayload] }
}
}
]
}
""".trimIndent()
ऐसा करने के बाद, क्रेडेंशियल मैनेजर एपीआई का इस्तेमाल करके अनुरोध किया जा सकता है:
suspend fun makeFpnvRequest(
context: Activity, nonce: String, fpnvDigitalCredentialPayload: String): GetCredentialResponse {
// Helper function to build the digital credential request (defined above).
// Pass the same nonce you passed to getDigitalCredentialPayload().
val digitalCredentialRequestJson =
buildDigitalCredentialRequestJson(nonce, fpnvDigitalCredentialPayload)
// CredentialManager requires an Activity context.
val credentialManager = CredentialManager.create(context)
// Build a Credential Manager request that includes the Firebase PNV option. Note that
// you can't combine the digital credential option with other options.
val request = GetCredentialRequest.Builder()
.addCredentialOption(GetDigitalCredentialOption(digitalCredentialRequestJson))
.build()
// getCredential is a suspend function, so it must run in a coroutine scope,
val cmResponse: GetCredentialResponse = try {
credentialManager.getCredential(context, request)
} catch (e: GetCredentialException) {
// If the user cancels the operation, the feature isn't available, or the
// SIM doesn't support the feature, a GetCredentialCancellationException
// will be returned. Otherwise, a GetCredentialUnsupportedException will
// be returned with details in the exception message.
throw e
}
return cmResponse
}
अगर क्रेडेंशियल मैनेजर का कॉल पूरा हो जाता है, तो उसके जवाब में डिजिटल क्रेडेंशियल शामिल होगा. इसे निकालने के लिए, यहां दिए गए उदाहरण जैसा कोड इस्तेमाल किया जा सकता है:
val dcApiResponse = extractApiResponse(cmResponse)
fun extractApiResponse(response: GetCredentialResponse): String {
val credential = response.credential
when (credential) {
is DigitalCredential -> {
val json = JSONObject(credential.credentialJson)
val firebaseJwtArray =
json.getJSONObject("data").getJSONObject("vp_token").getJSONArray("firebase")
return firebaseJwtArray.getString(0)
}
else -> {
// Handle any unrecognized credential type here.
Log.e(TAG, "Unexpected type of credential ${credential.type}")
}
}
}
3. डिजिटल क्रेडेंशियल के जवाब को Firebase PNV टोकन से बदलना
आखिर में, exchangeCredentialResponseForPhoneNumber() तरीके को कॉल करें. इससे, डिजिटल क्रेडेंशियल के जवाब को, पुष्टि किए गए फ़ोन नंबर और
Firebase PNV टोकन से बदला जा सकता है:
fpnv.exchangeCredentialResponseForPhoneNumber(dcApiResponse)
.addOnSuccessListener { result ->
val phoneNumber = result.getPhoneNumber()
// Verification successful
}
.addOnFailureListener { e -> /* Handle exchange failure */ }
यह चरण पूरा होने पर, बिलिंग की जाएगी. साथ ही, पुष्टि किया गया फ़ोन नंबर आपके ऐप्लिकेशन को वापस कर दिया जाएगा.
4. Firebase PNV टोकन की पुष्टि करना
अगर प्रोसेस पूरी हो जाती है, तो getVerifiedPhoneNumber() तरीका, पुष्टि किया गया फ़ोन नंबर और उस नंबर वाला साइन किया गया टोकन दिखाता है. निजता नीति में बताए गए तरीके के मुताबिक, इस डेटा का इस्तेमाल अपने ऐप्लिकेशन में किया जा सकता है.
अगर पुष्टि किए गए फ़ोन नंबर का इस्तेमाल, ऐप्लिकेशन क्लाइंट के बाहर किया जाता है, तो आपको फ़ोन नंबर के बजाय टोकन पास करना चाहिए. इससे, टोकन का इस्तेमाल करते समय, उसकी इंटिग्रिटी की पुष्टि की जा सकती है. टोकन की पुष्टि करने के लिए, आपको दो एंडपॉइंट लागू करने होंगे:
- नॉनस जनरेट करने वाला एंडपॉइंट
- टोकन की पुष्टि करने वाला एंडपॉइंट
इन एंडपॉइंट को लागू करने की ज़िम्मेदारी आपकी है. यहां दिए गए उदाहरणों में, Node.js और Express का इस्तेमाल करके, इन्हें लागू करने का तरीका बताया गया है.
नॉनस जनरेट करना
यह एंडपॉइंट, एक बार इस्तेमाल किए जा सकने वाली वैल्यू जनरेट करता है और उन्हें कुछ समय के लिए सेव करता है. इन वैल्यू को नॉनस कहा जाता है. इनका इस्तेमाल, आपके एंडपॉइंट पर रीप्ले हमलों को रोकने के लिए किया जाता है. उदाहरण के लिए, आपके पास Express का कोई ऐसा रूट हो सकता है जिसे इस तरह से तय किया गया हो:
app.get('/fpnvNonce', async (req, res) => {
const nonce = crypto.randomUUID();
// TODO: Save the nonce to a database, key store, etc.
// You should also assign the nonce an expiration time and periodically
// clear expired nonces from your database.
await persistNonce({
nonce,
expiresAt: Date.now() + 180000, // Give it a short duration.
});
// Return the nonce to the caller.
res.send({ nonce });
});
यह वह एंडपॉइंट है जिसे पहले चरण में, प्लेसहोल्डर फ़ंक्शन fetchNonceFromYourServer() कॉल करेगा. नॉनस, क्लाइंट की ओर से किए जाने वाले अलग-अलग नेटवर्क कॉल के ज़रिए आगे बढ़ेगा. इसके बाद, Firebase PNV टोकन में आपके सर्वर पर वापस आ जाएगा. अगले चरण में, यह पुष्टि की जाती है कि टोकन में आपके जनरेट किया गया नॉनस शामिल है.
टोकन की पुष्टि करना
यह एंडपॉइंट, आपके क्लाइंट से Firebase PNV टोकन लेता है और उनकी पुष्टि करता है. टोकन की पुष्टि करने के लिए, आपको यह देखना होगा कि:
typहेडर कोJWTपर सेट किया गया हो.टोकन पर, Firebase PNV JWKS एंडपॉइंट पर पब्लिश की गई किसी एक कुंजी का इस्तेमाल करके,
ES256एल्गोरिदम से हस्ताक्षर किया गया हो:https://fpnv.googleapis.com/v1beta/jwksजारी करने वाले के दावे में, आपके Firebase प्रोजेक्ट का नंबर शामिल हो और वह इस फ़ॉर्मैट में हो:
https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBERFirebase कंसोल में, सेटिंग
सेटिंग > सामान्य टैब पर जाकर, अपने Firebase प्रोजेक्ट का नंबर देखा जा सकता है.Firebaseऑडियंस के दावे में, एक सूची शामिल होती है. इसमें, आपके Firebase प्रोजेक्ट का नंबर और प्रोजेक्ट आईडी शामिल होता है. यह सूची इस फ़ॉर्मैट में होती है:
[ https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBER, https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_ID, ]टोकन की समयसीमा खत्म न हुई हो.
टोकन में मान्य नॉनस शामिल हो. कोई नॉनस तब मान्य होता है, जब:
- आपने उसे जनरेट किया हो. इसका मतलब है कि वह उस परसिस्टेंस मैकेनिज़्म में मौजूद हो जिसका इस्तेमाल किया जा रहा है
- उसका इस्तेमाल पहले न किया गया हो
- उसकी समयसीमा खत्म न हुई हो
उदाहरण के लिए, Express को लागू करने का तरीका कुछ ऐसा हो सकता है:
import { JwtVerifier } from "aws-jwt-verify";
// Find your Firebase project number in the Firebase console.
const FIREBASE_PROJECT_NUMBER = "123456789";
// The issuer and audience claims of the FPNV token are specific to your
// project.
const issuer = `https://fpnv.googleapis.com/projects/${FIREBASE_PROJECT_NUMBER}`;
const audience = `https://fpnv.googleapis.com/projects/${FIREBASE_PROJECT_NUMBER}`;
// The JWKS URL contains the current public signing keys for FPNV tokens.
const jwksUri = "https://fpnv.googleapis.com/v1beta/jwks";
// Configure a JWT verifier to check the following:
// - The token is signed by Google
// - The issuer and audience claims match your project
// - The token has not yet expired (default begavior)
const fpnvVerifier = JwtVerifier.create({ issuer, audience, jwksUri });
app.post('/verifiedPhoneNumber', async (req, res) => {
if (!req.body) return res.sendStatus(400);
// Get the token from the body of the request.
const fpnvToken = req.body;
try {
// Attempt to verify the token using the verifier configured above.
const verifiedPayload = await fpnvVerifier.verify(fpnvToken);
// Now that you've verified the signature and claims, verify the nonce.
// TODO: Try to look up the nonce in your database and remove it if it's
// found; if it's not found or it's expired, throw an error.
await testAndRemoveNonce(verifiedPayload.nonce);
// Only after verifying the JWT signature, claims, and nonce, get the
// verified phone number from the subject claim.
// You can use this value however it's needed by your app.
const verifiedPhoneNumber = verifiedPayload.sub;
// (Do something with it...)
return res.sendStatus(200);
} catch {
// If verification fails, reject the token.
return res.sendStatus(400);
}
});