Saat library Firebase PNV berhasil memverifikasi nomor telepon perangkat, library tersebut akan menampilkan nomor telepon terverifikasi dan token bertanda tangan yang berisi nomor telepon tersebut. Jika menggunakan nomor telepon terverifikasi di luar klien aplikasi, Anda harus meneruskan token, bukan nomor telepon itu sendiri, sehingga Anda dapat memverifikasi integritasnya saat Anda menggunakannya. Untuk memverifikasi token, Anda dapat menggunakan library verifikasi JWT apa pun. Gunakan library untuk memverifikasi semua hal berikut:
Header
typdisetel keJWT.Token ditandatangani menggunakan salah satu kunci yang dipublikasikan di endpoint JWKS Firebase PNV dengan algoritma
ES256:https://fpnv.googleapis.com/v1beta/jwksKlaim penerbit berisi nomor project Firebase Anda dan menggunakan format berikut:
https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBERAnda dapat menemukan nomor project Firebase di halaman Project settings di Firebase console.
Klaim audiens adalah daftar yang berisi nomor project dan project ID Firebase Anda serta menggunakan format berikut:
[ https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBER, https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_ID, ]Masa berlaku token belum berakhir.
Contoh
Sebagai contoh singkat, aplikasi Express.js berikut menerima token Firebase PNV dari
permintaan POST HTTP dan menggunakan library verifikasi JWT untuk memeriksa
tanda tangan dan klaim 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);