অ্যান্ড্রয়েডে ফায়ারবেস ফোন নম্বর যাচাইকরণ প্রবাহ কাস্টমাইজ করুন

"Get Started with Firebase Phone Number Verification পৃষ্ঠায় getVerifiedPhoneNumber() পদ্ধতি ব্যবহার করে Firebase PNV সাথে কীভাবে একীভূত করতে হয় তার বিশদ বিবরণ দেওয়া আছে, যা ব্যবহারকারীর সম্মতি নেওয়া থেকে শুরু করে Firebase PNV ব্যাকএন্ডে প্রয়োজনীয় নেটওয়ার্ক কল করা পর্যন্ত সম্পূর্ণ Firebase PNV প্রবাহ পরিচালনা করে।

বেশিরভাগ ডেভেলপারদের জন্য একক-পদ্ধতি API ( getVerifiedPhoneNumber() ) সুপারিশ করা হয়। তবে, যদি আপনার Android Credential Manager-এর সাথে ইন্টারঅ্যাকশনের উপর আরও সূক্ষ্ম নিয়ন্ত্রণের প্রয়োজন হয় - উদাহরণস্বরূপ, ফোন নম্বরের সাথে অন্যান্য ক্রেডেনশিয়াল অনুরোধ করার জন্য - Firebase PNV লাইব্রেরি নিম্নলিখিত দুটি পদ্ধতিও প্রদান করে, যার প্রতিটি Firebase PNV ব্যাকএন্ডের সাথে আলাদা ইন্টারঅ্যাকশন পরিচালনা করে:

  • getDigitalCredentialPayload() একটি সার্ভার-স্বাক্ষরিত অনুরোধ পায় যা আপনি ক্রেডেনশিয়াল ম্যানেজার ব্যবহার করতে ব্যবহার করবেন।
  • exchangeCredentialResponseForPhoneNumber() যাচাইকৃত ফোন নম্বর সম্বলিত একটি স্বাক্ষরিত টোকেনের জন্য ক্রেডেনশিয়াল ম্যানেজারের প্রতিক্রিয়া বিনিময় করে।

এই পদ্ধতিগুলির প্রতিটি কল করার মধ্যে, আপনি অ্যান্ড্রয়েডের ক্রেডেনশিয়াল ম্যানেজার API গুলির সাথে ইন্টারঅ্যাকশন পরিচালনা করার জন্য দায়ী। এই পৃষ্ঠাটি আপনি এই তিন-অংশের প্রবাহটি কীভাবে বাস্তবায়ন করেন তার একটি সারসংক্ষেপ দেয়।

শুরু করার আগে

আপনার Firebase প্রকল্প সেট আপ করুন এবং Get started পৃষ্ঠায় বর্ণিত Firebase PNV নির্ভরতাগুলি আমদানি করুন।

১. ডিজিটাল ক্রেডেনশিয়াল রিকোয়েস্ট পেলোড পান

ডিভাইসের ফোন নম্বরের জন্য একটি অনুরোধ তৈরি করতে getDigitalCredentialPayload() পদ্ধতিতে কল করুন। পরবর্তী ধাপে, এই অনুরোধটি হবে Credential Manager API এর সাথে আপনার ইন্টারঅ্যাকশনের পেলোড।

// This instance does not require an Activity context.
val fpnv = FirebasePhoneNumberVerification.getInstance()

// Your request should include a nonce, which will propagate through the flow
// and be present in the final response from FPNV. See the section "Verifying
// the Firebase PNV token" for details on generating and verifying this.
val nonce = fetchNonceFromYourServer()

fpnv.getDigitalCredentialPayload(nonce, "https://example.com/privacy-policy")
  .addOnSuccessListener { fpnvDigitalCredentialPayload ->
    // Use the payload in the next step.
    // ...
  }
  .addOnFailureListener { e -> /* Handle payload fetch failure */ }

2. ক্রেডেনশিয়াল ম্যানেজার ব্যবহার করে একটি ডিজিটাল ক্রেডেনশিয়াল অনুরোধ করুন

এরপর, অনুরোধটি ক্রেডেনশিয়াল ম্যানেজারের কাছে পাঠান।

এটি করার জন্য, আপনাকে অনুরোধের পেলোডটি একটি DigitalCredential API অনুরোধে মোড়ানো প্রয়োজন। এই অনুরোধে অবশ্যই getDigitalCredentialPayload() এ আপনি যে নন্সটি পাস করেছেন তা অন্তর্ভুক্ত থাকতে হবে।

// This example uses string interpolation for clarity, but you should use some kind of type-safe
// serialization method.
fun buildDigitalCredentialRequestJson(nonce: String, fpnvDigitalCredentialPayload: String) = """
    {
      "requests": [
        {
          "protocol": "openid4vp-v1-unsigned",
          "data": {
            "response_type": "vp_token",
            "response_mode": "dc_api",
            "nonce": "$nonce",
            "dcql_query": { "credentials": [$fpnvDigitalCredentialPayload] }
          }
        }
      ]
    }
""".trimIndent()

এটি করার পরে, আপনি ক্রেডেনশিয়াল ম্যানেজার API ব্যবহার করে অনুরোধটি করতে পারেন:

suspend fun makeFpnvRequest(
  context: Activity, nonce: String, fpnvDigitalCredentialPayload: String): GetCredentialResponse {
  // Helper function to build the digital credential request (defined above).
  // Pass the same nonce you passed to getDigitalCredentialPayload().
  val digitalCredentialRequestJson =
    buildDigitalCredentialRequestJson(nonce, fpnvDigitalCredentialPayload)

  // CredentialManager requires an Activity context.
  val credentialManager = CredentialManager.create(context)

  // Build a Credential Manager request that includes the Firebase PNV option. Note that
  // you can't combine the digital credential option with other options.
  val request = GetCredentialRequest.Builder()
    .addCredentialOption(GetDigitalCredentialOption(digitalCredentialRequestJson))
    .build()

  // getCredential is a suspend function, so it must run in a coroutine scope,
  val cmResponse: GetCredentialResponse = try {
    credentialManager.getCredential(context, request)
  } catch (e: GetCredentialException) {
    // If the user cancels the operation, the feature isn't available, or the
    // SIM doesn't support the feature, a GetCredentialCancellationException
    // will be returned. Otherwise, a GetCredentialUnsupportedException will
    // be returned with details in the exception message.
    throw e
  }
  return cmResponse
}

যদি ক্রেডেনশিয়াল ম্যানেজার কলটি সফল হয়, তাহলে এর প্রতিক্রিয়ায় একটি ডিজিটাল ক্রেডেনশিয়াল থাকবে, যা আপনি নিম্নলিখিত উদাহরণের মতো কোড ব্যবহার করে বের করতে পারবেন:

val dcApiResponse = extractApiResponse(cmResponse)
fun extractApiResponse(response: GetCredentialResponse): String {
  val credential = response.credential
  when (credential) {
    is DigitalCredential -> {
      val json = JSONObject(credential.credentialJson)
      val firebaseJwtArray =
          json.getJSONObject("data").getJSONObject("vp_token").getJSONArray("firebase")
      return firebaseJwtArray.getString(0)

    }
    else -> {
      // Handle any unrecognized credential type here.
      Log.e(TAG, "Unexpected type of credential ${credential.type}")
    }
  }
}

৩. একটি Firebase PNV টোকেনের জন্য ডিজিটাল শংসাপত্রের প্রতিক্রিয়া বিনিময় করুন

অবশেষে, যাচাইকৃত ফোন নম্বর এবং একটি Firebase PNV টোকেনের জন্য ডিজিটাল ক্রেডেনশিয়াল প্রতিক্রিয়া বিনিময় করতে exchangeCredentialResponseForPhoneNumber() পদ্ধতিতে কল করুন:

fpnv.exchangeCredentialResponseForPhoneNumber(dcApiResponse)
  .addOnSuccessListener { result ->
    val phoneNumber = result.getPhoneNumber()
    // Verification successful
  }
  .addOnFailureListener { e -> /* Handle exchange failure */ }

৪. Firebase PNV টোকেন যাচাই করা

যদি প্রবাহটি সফল হয়, তাহলে getVerifiedPhoneNumber() পদ্ধতিটি যাচাইকৃত ফোন নম্বর এবং এটি সম্বলিত একটি স্বাক্ষরিত টোকেন ফেরত পাঠাবে। আপনার গোপনীয়তা নীতি অনুসারে আপনি এই ডেটা আপনার অ্যাপে ব্যবহার করতে পারেন।

যদি আপনি অ্যাপ ক্লায়েন্টের বাইরে যাচাইকৃত ফোন নম্বরটি ব্যবহার করেন, তাহলে ফোন নম্বরের পরিবর্তে টোকেনটি পাস করা উচিত যাতে আপনি এটি ব্যবহার করার সময় এর অখণ্ডতা যাচাই করতে পারেন। টোকেন যাচাই করার জন্য, আপনাকে দুটি এন্ডপয়েন্ট বাস্তবায়ন করতে হবে:

  • একটি ননস জেনারেশন এন্ডপয়েন্ট
  • একটি টোকেন যাচাইকরণের শেষ বিন্দু

এই এন্ডপয়েন্টগুলির বাস্তবায়ন আপনার উপর নির্ভর করে; নিম্নলিখিত উদাহরণগুলি দেখায় যে আপনি Node.js এবং Express ব্যবহার করে কীভাবে এগুলি বাস্তবায়ন করতে পারেন।

ননসেস তৈরি করা হচ্ছে

এই এন্ডপয়েন্টটি ননসেস নামক একক-ব্যবহারের মান তৈরি এবং অস্থায়ীভাবে সংরক্ষণের জন্য দায়ী, যা আপনার এন্ডপয়েন্টের বিরুদ্ধে রিপ্লে আক্রমণ প্রতিরোধ করতে ব্যবহৃত হয়। উদাহরণস্বরূপ, আপনার কাছে একটি এক্সপ্রেস রুট এভাবে সংজ্ঞায়িত করা যেতে পারে:

app.get('/fpnvNonce', async (req, res) => {
    const nonce = crypto.randomUUID();

    // TODO: Save the nonce to a database, key store, etc.
    // You should also assign the nonce an expiration time and periodically
    // clear expired nonces from your database.
    await persistNonce({
        nonce,
        expiresAt: Date.now() + 180000, // Give it a short duration.
    });

    // Return the nonce to the caller.
    res.send({ nonce });
});

ধাপ ১-এ প্লেসহোল্ডার ফাংশন, fetchNonceFromYourServer() , এই শেষ বিন্দুটিকে কল করবে। ননস ক্লায়েন্টের দ্বারা সম্পাদিত বিভিন্ন নেটওয়ার্ক কলের মাধ্যমে প্রচারিত হবে এবং অবশেষে Firebase PNV টোকেনে আপনার সার্ভারে ফিরে আসবে। পরবর্তী ধাপে, আপনি যাচাই করবেন যে টোকেনটিতে আপনার তৈরি করা একটি ননস রয়েছে।

টোকেন যাচাই করা হচ্ছে

এই এন্ডপয়েন্টটি আপনার ক্লায়েন্টের কাছ থেকে Firebase PNV টোকেন গ্রহণ করে এবং তাদের সত্যতা যাচাই করে। একটি টোকেন যাচাই করার জন্য, আপনাকে পরীক্ষা করতে হবে:

  • Firebase PNV JWKS এন্ডপয়েন্টে প্রকাশিত একটি কী ব্যবহার করে টোকেনটি স্বাক্ষরিত হয়:

    https://fpnv.googleapis.com/v1beta/jwks
    
  • শ্রোতা এবং ইস্যুকারীর দাবিতে আপনার Firebase প্রকল্প নম্বর রয়েছে এবং নিম্নলিখিত ফর্ম্যাটে রয়েছে:

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

    আপনি Firebase কনসোলের Project সেটিংস পৃষ্ঠায় আপনার Firebase প্রজেক্ট নম্বরটি খুঁজে পেতে পারেন।

  • টোকেনটির মেয়াদ শেষ হয়নি।

  • টোকেনটিতে একটি বৈধ ননস রয়েছে। একটি ননস বৈধ হবে যদি:

    • আপনি এটি তৈরি করেছেন (অর্থাৎ, আপনি যে কোনও অধ্যবসায় ব্যবস্থা ব্যবহার করছেন তাতে এটি পাওয়া যাবে)
    • এটি ইতিমধ্যে ব্যবহার করা হয়নি।
    • এটির মেয়াদ শেষ হয়নি।

উদাহরণস্বরূপ, এক্সপ্রেস বাস্তবায়নটি দেখতে এরকম কিছু হতে পারে:

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 begavior)
const fpnvVerifier = JwtVerifier.create({ issuer, audience, jwksUri });

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 above.
        const verifiedPayload = await fpnvVerifier.verify(fpnvToken);

        // Now that you've verified the signature and claims, verify the nonce.
        // TODO: Try to look up the nonce in your database and remove it if it's
        // found; if it's not found or it's expired, throw an error.
        await testAndRemoveNonce(verifiedPayload.nonce);

        // Only after verifying the JWT signature, claims, and nonce, get the
        // verified phone number from the subject claim.
        // 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);
    }
});