Firebase PNV टोकन की पुष्टि करना

जब Firebase PNV लाइब्रेरी किसी डिवाइस के फ़ोन नंबर की पुष्टि कर लेती है, तो वह पुष्टि किया गया फ़ोन नंबर और उस नंबर वाला एक हस्ताक्षर किया गया टोकन वापस भेजती है. अगर आपको ऐप्लिकेशन क्लाइंट के बाहर पुष्टि किए गए फ़ोन नंबर का इस्तेमाल करना है, तो आपको फ़ोन नंबर के बजाय टोकन पास करना चाहिए. इससे, इस्तेमाल करते समय इसकी पुष्टि की जा सकेगी. टोकन की पुष्टि करने के लिए, किसी भी JWT पुष्टि करने वाली लाइब्रेरी का इस्तेमाल किया जा सकता है. लाइब्रेरी का इस्तेमाल करके, इन सभी की पुष्टि करें:

  • typ हेडर को JWT पर सेट किया गया है.

  • टोकन पर हस्ताक्षर करने के लिए, Firebase PNV JWKS एंडपॉइंट पर पब्लिश की गई किसी एक कुंजी का इस्तेमाल किया गया है. साथ ही, ES256 एल्गोरिदम का इस्तेमाल किया गया है:

    https://fpnv.googleapis.com/v1beta/jwks
    
  • जारी करने वाले के दावे में आपका Firebase प्रोजेक्ट नंबर शामिल होता है. यह इस फ़ॉर्मैट में होता है:

    https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBER
    

    आपको Firebase प्रोजेक्ट नंबर, Firebase कंसोल में सेटिंग > सामान्य टैब पर मिल सकता है.

  • ऑडियंस का दावा करने वाली सूची में, आपका Firebase प्रोजेक्ट नंबर और प्रोजेक्ट आईडी होता है. यह सूची इस फ़ॉर्मैट में होती है:

    [
      https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBER,
      https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_ID,
    ]
    
  • टोकन की समयसीमा खत्म नहीं हुई है.

उदाहरण

उदाहरण के लिए, यहां दिए गए Express.js ऐप्लिकेशन को एचटीटीपी POST अनुरोध से Firebase PNV टोकन मिलता है. यह टोकन के हस्ताक्षर और दावों की जांच करने के लिए, JWT की पुष्टि करने वाली लाइब्रेरी का इस्तेमाल करता है:

Node.js

import express from "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 behavior)
const fpnvVerifier = JwtVerifier.create({ issuer, audience, jwksUri });

const app = express();

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
        previously.
        const verifiedPayload = await fpnvVerifier.verify(fpnvToken);

        // If verification succeeds, the subject claim of the token contains the
        // verified phone number. 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);
    }
});

app.listen(3000);