Quando la libreria Firebase PNV verifica correttamente il numero di telefono di un dispositivo, restituisce il numero di telefono verificato e un token firmato che lo contiene. Se utilizzi il numero di telefono verificato al di fuori del client dell'app, devi passare il token anziché il numero di telefono stesso in modo da poterne verificare l'integrità quando lo utilizzi. Per verificare il token, puoi utilizzare qualsiasi libreria di verifica JWT. Utilizza la libreria per verificare tutti i seguenti elementi:
L'intestazione
typè impostata suJWT.Il token è firmato utilizzando una delle chiavi pubblicate nell'endpoint JWKS Firebase PNV con l'algoritmo
ES256:https://fpnv.googleapis.com/v1beta/jwksLe attestazioni dell'emittente contengono il numero del tuo progetto Firebase e sono nel seguente formato:
https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBERPuoi trovare il numero del tuo progetto Firebase nella
scheda > Generali della Firebase console.L'attestazione del pubblico è un elenco che contiene il numero e l'ID progetto del tuo progetto Firebase ed è nel seguente formato:
[ https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBER, https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_ID, ]Il token non è scaduto.
Esempio
Come breve esempio, la seguente app Express.js riceve un Firebase PNV token da
una richiesta HTTP POST e utilizza una libreria di verifica JWT per controllare la
firma e le attestazioni del token:
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);