Gdy biblioteka Firebase PNV pomyślnie zweryfikuje numer telefonu urządzenia, zwróci zweryfikowany numer telefonu i podpisany token, który go zawiera. Jeśli używasz zweryfikowanego numeru telefonu poza klientem aplikacji, zamiast samego numeru telefonu powinieneś przekazywać token, aby móc sprawdzić jego integralność. Aby zweryfikować token, możesz użyć dowolnej biblioteki weryfikacji JWT. Użyj biblioteki, aby sprawdzić te elementy:
Nagłówek
typma wartośćJWT.Token jest podpisany jednym z kluczy opublikowanych w punkcie końcowym Firebase PNV JWKS za pomocą algorytmu
ES256:https://fpnv.googleapis.com/v1beta/jwksDeklaracja wystawcy zawiera numer Twojego projektu w Firebase i ma ten format:
https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBERNumer projektu w Firebase znajdziesz w konsoli Firebase na karcie
Ustawienia > Ogólne .FirebaseDeklaracja odbiorcy to lista, która zawiera numer i identyfikator Twojego projektu w Firebase i ma ten format:
[ https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBER, https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_ID, ]Token nie wygasł.
Przykład
Poniżej znajdziesz krótki przykład aplikacji Express.js, która otrzymuje token Firebase PNV z
żądania HTTP POST i używa biblioteki weryfikacji JWT do sprawdzenia
podpisu i deklaracji tokena:
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);