Firebase PNV kitaplığı bir cihazın telefon numarasını başarıyla doğruladığında, doğrulanmış telefon numarasını ve bunu içeren imzalı bir jetonu döndürür. Doğrulanmış telefon numarasını uygulama istemcisinin dışında kullanıyorsanız telefon numarasının kendisi yerine jetonu iletmeniz gerekir. Böylece, kullandığınızda jetonun bütünlüğünü doğrulayabilirsiniz. Jetonu doğrulamak için herhangi bir JWT doğrulama kitaplığını kullanabilirsiniz. Aşağıdakilerin tümünü doğrulamak için kitaplığı kullanın:
typüstbilgisiJWTolarak ayarlanır.Jeton, Firebase PNV JWKS uç noktasında
ES256algoritmasıyla yayınlanan anahtarlardan biri kullanılarak imzalanır:https://fpnv.googleapis.com/v1beta/jwksVeren talepleri, Firebase proje numaranızı içerir ve şu biçimdedir:
https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBERFirebase proje numaranızı Firebase konsolunun Proje ayarları sayfasında bulabilirsiniz.
Kitle talebi, Firebase proje numaranızı ve proje kimliğinizi içeren bir listedir ve aşağıdaki biçimdedir:
[ https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBER, https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_ID, ]Jetonun süresi dolmamış olmalıdır.
Örnek
Kısa bir örnek olarak, aşağıdaki Express.js uygulaması bir HTTP POST isteğinden Firebase PNV jetonu alır ve jetonun imzasını ve taleplerini kontrol etmek için bir JWT doğrulama kitaplığı kullanır:
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);