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

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

  • typ हेडर, JWT पर सेट है.

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

    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);