التحقّق من رموز PNV المميزة في Firebase

عندما تتحقّق مكتبة 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 التالي رمزًا مميّزًا من Firebase PNV من طلب HTTP POST ويستخدم مكتبة للتحقّق من رموز 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);