Firebase PNV 토큰 확인

Firebase PNV 라이브러리가 기기의 전화번호를 성공적으로 인증하면 인증된 전화번호와 전화번호가 포함된 서명된 토큰을 반환합니다. 앱 클라이언트 외부에서 인증된 전화번호를 사용하는 경우 전화번호 자체 대신 토큰을 전달하여 사용할 때 무결성을 확인할 수 있습니다. 토큰을 확인하려면 JWT 인증 라이브러리를 사용하면 됩니다. 라이브러리를 사용하여 다음을 모두 확인합니다.

  • typ 헤더가 JWT로 설정됩니다.

  • 토큰은 ES256 알고리즘을 사용하여 Firebase PNV JWKS 엔드포인트에 게시된 키 중 하나를 사용하여 서명됩니다.

    https://fpnv.googleapis.com/v1beta/jwks
    
  • 발급자 클레임에는 Firebase 프로젝트 번호가 포함되어 있으며 형식은 다음과 같습니다.

    https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBER
    

    Firebase 프로젝트 번호는 Firebase Console의 프로젝트 설정 페이지에서 확인할 수 있습니다.

  • 대상 클레임은 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);