Khi thư viện Firebase PNV xác minh thành công số điện thoại của một thiết bị, thư viện này sẽ trả về số điện thoại đã xác minh và một mã thông báo đã ký có chứa số điện thoại đó. Nếu sử dụng số điện thoại đã xác minh bên ngoài ứng dụng, bạn nên truyền mã thông báo thay vì chính số điện thoại để có thể xác minh tính toàn vẹn của số điện thoại khi sử dụng. Để xác minh mã thông báo, bạn có thể sử dụng bất kỳ thư viện xác minh JWT nào. Sử dụng thư viện này để xác minh tất cả những điều sau:
Tiêu đề
typđược đặt thànhJWT.Mã thông báo được ký bằng một trong các khoá được xuất bản tại điểm cuối JWKS Firebase PNV bằng thuật toán
ES256:https://fpnv.googleapis.com/v1beta/jwksThông tin xác nhận của tổ chức phát hành chứa số dự án Firebase của bạn và có định dạng sau:
https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBERBạn có thể tìm thấy số dự án Firebase trên trang Cài đặt dự án của bảng điều khiển Firebase.
Yêu cầu về đối tượng là một danh sách chứa số dự án và mã dự án Firebase của bạn, có định dạng như sau:
[ https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBER, https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_ID, ]Mã thông báo chưa hết hạn.
Ví dụ
Ví dụ ngắn gọn: ứng dụng Express.js sau đây nhận mã thông báo Firebase PNV từ một yêu cầu HTTP POST và sử dụng một thư viện xác minh JWT để kiểm tra chữ ký và các yêu cầu của mã thông báo:
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);