Wenn die Firebase PNV Bibliothek die Telefonnummer eines Geräts erfolgreich bestätigt, gibt sie die bestätigte Telefonnummer und ein signiertes Token zurück, das diese enthält. Wenn Sie die bestätigte Telefonnummer außerhalb des App-Clients verwenden, sollten Sie das Token anstelle der Telefonnummer selbst weitergeben, damit Sie die Integrität der Telefonnummer bei der Verwendung überprüfen können. Zum Bestätigen des Tokens können Sie eine beliebige JWT-Bestätigungsbibliothek verwenden. Verwenden Sie die Bibliothek, um Folgendes zu bestätigen:
Der
typ-Header ist aufJWTfestgelegt.Das Token wird mit einem der Schlüssel signiert, die am Firebase PNV JWKS Endpunkt mit dem
ES256Algorithmus veröffentlicht wurden:https://fpnv.googleapis.com/v1beta/jwksDer Ausstelleranspruch enthält Ihre Firebase-Projektnummer und hat das folgende Format:
https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBERSie finden Ihre Firebase-Projektnummer auf der
Einstellungen > Allgemein Registerkarte der Firebase Konsole.Der Zielgruppenanspruch ist eine Liste, die Ihre Firebase-Projektnummer und ‑Projekt-ID enthält und das folgende Format hat:
[ https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBER, https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_ID, ]Das Token ist nicht abgelaufen.
Beispiel
Im folgenden kurzen Beispiel empfängt eine Express.js-App ein Firebase PNV Token aus
einer HTTP POST Anfrage und verwendet eine JWT-Bestätigungsbibliothek, um die
Signatur und die Ansprüche des Tokens zu prüfen:
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);