Firebase PNV ライブラリがデバイスの電話番号の確認に成功すると、確認済みの電話番号と、その電話番号を含む署名付きトークンが返されます。確認済みの電話番号をアプリ クライアントの外部で使用する場合は、電話番号自体ではなくトークンを渡して、使用時に完全性を確認できるようにする必要があります。トークンを確認するには、任意の JWT 確認ライブラリを使用できます。ライブラリを使用して、次の点をすべて確認します。
typヘッダーはJWTに設定されている。トークンは、
ES256アルゴリズムを使用して Firebase PNV JWKS エンドポイントで公開された鍵のいずれかを使用して署名される。https://fpnv.googleapis.com/v1beta/jwks発行者クレームには Firebase プロジェクト番号が含まれており、次の形式になっている。
https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBERFirebase プロジェクト番号は、Firebase コンソールの [プロジェクトの設定] ページで確認できます。
オーディエンス クレームは、Firebase プロジェクト番号とプロジェクト ID を含むリストで、次の形式になっている。
[ https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBER, https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_ID, ]トークンの有効期限が切れていない。
例
簡単な例として、次の Express.js アプリは HTTP POST リクエストから Firebase PNV トークンを受け取り、JWT 確認ライブラリを使用してトークンの署名とクレームをチェックします。
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);