Android-এ Firebase ফোন নম্বর যাচাইকরণ ফ্লো কাস্টমাইজ করা

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

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

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

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

শুরু করার আগে

আপনার Firebase প্রোজেক্ট সেট-আপ করুন এবং শুরু করুন পৃষ্ঠায় যেভাবে বর্ণনা করা হয়েছে সেই অনুযায়ী Firebase PNV ডিপেন্ডেন্সি ইমপোর্ট করুন।

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

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

Kotlin

// 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 */ }

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

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

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

Kotlin

// 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 ব্যবহার করে অনুরোধ করতে পারবেন:

Kotlin

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
}

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

Kotlin

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() পদ্ধতি কল করুন:

Kotlin

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

এই ধাপটি সফলভাবে সম্পূর্ণ হলে এবং যাচাই করা ফোন নম্বর আপনার অ্যাপে ফেরত আসলে, বিলিং ট্রিগার হবে।

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

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

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

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

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

ননস জেনারেট করা হচ্ছে

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

Node.js

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 টোকেন পায় এবং সেটির সত্যতা যাচাই করে। টোকেন যাচাই করতে, আপনাকে এগুলি চেক করতে হবে:

  • typ হেডার JWT-এ সেট করা আছে।

  • Firebase PNV JWKS এন্ডপয়েন্টে প্রকাশিত কীগুলির মধ্যে একটি ব্যবহার করে টোকেনটি সাইন করা হয় ES256 অ্যালগরিদম সহ:

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

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

    আপনি Firebase কনসোলের সেটিংস > সাধারণ ট্যাব থেকে আপনার Firebase প্রোজেক্ট নম্বর খুঁজে পেতে পারেন।

  • অডিয়েন্স ক্লেম হল একটি তালিকা যাতে আপনার Firebase প্রোজেক্ট নম্বর ও প্রোজেক্ট আইডি থাকে এবং এটি নিম্নলিখিত ফর্ম্যাটে থাকে:

    [
      https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_NUMBER,
      https://fpnv.googleapis.com/projects/FIREBASE_PROJECT_ID,
    ]
    
  • টোকেনের মেয়াদ শেষ হয়নি।

  • টোকেনে সঠিক ননস আছে। ননস সঠিক হবে যদি:

    • আপনি এটি তৈরি করেছেন (অর্থাৎ, আপনি যে পারসিস্টেন্স মেকানিজম ব্যবহার করছেন, সেখানে এটি খুঁজে পাওয়া যাবে)
    • এটি আগে ব্যবহার করা হয়নি
    • এটির মেয়াদ শেষ হয়নি

যেমন, এক্সপ্রেস ইমপ্লিমেন্টেশন নিচে উল্লেখ করা উদাহরণের মতো দেখতে হতে পারে:

Node.js

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);
    }
});