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