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

Android অ্যাপের মধ্যে ডিজিটাল ক্রেডেনশিয়াল যাচাইকরণ ব্যবহারকারীর পরিচয় (যেমন, সরকারি আইডি), ব্যবহারকারীর সম্পর্কে তথ্য (যেমন, ড্রাইভিং লাইসেন্স, অ্যাকাডেমিক ডিগ্রি, বা বয়স বা ঠিকানার মতো অ্যাট্রিবিউট) যাচাই ও অনুমোদন করতে বা অন্য কোনও পরিস্থিতিতে যেখানে কোনও এন্টিটির সত্যতা যাচাই করার জন্য ক্রেডেনশিয়াল ইস্যু ও যাচাই করতে হয়, সেখানে ব্যবহার করা যেতে পারে।

Verifier API, Android 9 (API লেভেল ২৮) এবং তার পরবর্তী যেকোনও ভার্সনে কাজ করে।

প্রয়োগ

আপনার Android প্রোজেক্টে ডিজিটাল ক্রেডেনশিয়াল যাচাই করতে, নিম্নলিখিত ধাপগুলি অনুসরণ করুন:

  1. আপনার অ্যাপের বিল্ড স্ক্রিপ্টে ডিপেন্ডেন্সি যোগ করুন এবং একটি CredentialManager ক্লাস শুরু করুন।
  2. একটি ডিজিটাল ক্রেডেনশিয়াল অনুরোধ তৈরি করুন এবং এটি ব্যবহার করে একটি DigitalCredentialOption শুরু করুন, তারপরে GetCredentialRequest তৈরি করুন।
  3. সফল GetCredentialResponse পেতে বা ঘটতে পারে এমন কোনও ব্যতিক্রম ম্যানেজ করতে, তৈরি করা অনুরোধের সাথে getCredential ফ্লো লঞ্চ করুন। সফলভাবে ফিরিয়ে আনার পরে, উত্তরটি যাচাই করুন।

ডিপেন্ডেন্সি যোগ করুন এবং শুরু করুন

আপনার Gradle বিল্ড স্ক্রিপ্টে নিম্নলিখিত ডিপেন্ডেন্সি যোগ করুন:

dependencies {
    implementation("androidx.credentials:credentials:1.6.0-beta01")
    implementation("androidx.credentials:credentials-play-services-auth:1.6.0-beta01")
}

এরপর, CredentialManager ক্লাসের একটি ইনস্ট্যান্স ইনিশিয়ালাইজ করুন।

val credentialManager = CredentialManager.create(context)

ডিজিটাল ক্রেডেনশিয়াল সংক্রান্ত অনুরোধ তৈরি করুন

একটি ডিজিটাল ক্রেডেনশিয়াল অনুরোধ তৈরি করুন এবং এটি ব্যবহার করে একটি DigitalCredentialOption শুরু করুন।

// The request in the JSON format to conform with
// the JSON-ified Credential Manager - Verifier API request definition.
val requestJson = generateRequestFromServer()
val digitalCredentialOption =
    GetDigitalCredentialOption(requestJson = requestJson)

// Use the option from the previous step to build the `GetCredentialRequest`.
val getCredRequest = GetCredentialRequest(
    listOf(digitalCredentialOption)
)

নিম্নলিখিত উদাহরণে OpenId4Vp১ অনুরোধ দেখানো হয়েছে। এই অনুরোধের ফিল্ড সম্পর্কে বিস্তারিত জানতে, OpenID ডকুমেন্টেশন দেখুন।

{
  "requests": [
    {
      "protocol": "openid4vp-v1-unsigned",
      "data": {
        "response_type": "vp_token",
        "response_mode": "dc_api",
        "nonce": "OD8eP8BYfr0zyhgq4QCVEGN3m7C1Ht_No9H5fG5KJFk",
        "dcql_query": {
          "credentials": [
            {
              "id": "cred1",
              "format": "mso_mdoc",
              "meta": {
                "doctype_value": "org.iso.18013.5.1.mDL"
              },
              "claims": [
                {
                  "path": [
                    "org.iso.18013.5.1",
                    "family_name"
                  ]
                },
                {
                  "path": [
                    "org.iso.18013.5.1",
                    "given_name"
                  ]
                },
                {
                  "path": [
                    "org.iso.18013.5.1",
                    "age_over_21"
                  ]
                }
              ]
            }
          ]
        }
      }
    }
  ]
}

ক্রেডেনশিয়াল পান

তৈরি করা অনুরোধের মাধ্যমে getCredential ফ্লো লঞ্চ করুন। আপনি হয় সফল GetCredentialResponse, অথবা অনুরোধ ব্যর্থ হলে GetCredentialException পাবেন।

getCredential ফ্লো, Android সিস্টেম ডায়ালগ ট্রিগার করে যাতে ব্যবহারকারীর উপলভ্য ক্রেডেনশিয়াল বিকল্প দেখানো এবং তার বেছে নেওয়া বিকল্প সংগ্রহ করা যায়। এরপর, বেছে নেওয়া ক্রেডেনশিয়াল বিকল্পটি যে Wallet অ্যাপে আছে সেটি সম্মতি সংগ্রহ করার জন্য UI দেখাবে এবং ডিজিটাল ক্রেডেনশিয়াল রেসপন্স জেনারেট করার জন্য প্রয়োজনীয় অ্যাকশন পারফর্ম করবে।

coroutineScope.launch {
    try {
        val result = credentialManager.getCredential(
            context = activityContext,
            request = getCredRequest
        )
        verifyResult(result)
    } catch (e : GetCredentialException) {
        handleFailure(e)
    }
}

// Handle the successfully returned credential.
fun verifyResult(result: GetCredentialResponse) {
    val credential = result.credential
    when (credential) {
        is DigitalCredential -> {
            val responseJson = credential.credentialJson
            validateResponseOnServer(responseJson)
        }
        else -> {
            // Catch any unrecognized credential type here.
            Log.e(TAG, "Unexpected type of credential ${credential.type}")
        }
    }
}

// Handle failure.
fun handleFailure(e: GetCredentialException) {
  when (e) {
        is GetCredentialCancellationException -> {
            // The user intentionally canceled the operation and chose not
            // to share the credential.
        }
        is GetCredentialInterruptedException -> {
            // Retry-able error. Consider retrying the call.
        }
        is NoCredentialException -> {
            // No credential was available.
        }
        else -> Log.w(TAG, "Unexpected exception type ${e::class.java}")
    }
}
getCredential কল করার সময় দেখানো ডিজিটাল ক্রেডেনশিয়াল সিলেক্টর
  দেখানো ছবি।
ছবি ১. getCredential কল করার সময় দেখানো ডিজিটাল ক্রেডেনশিয়াল সিলেক্টর।

ফ্লেক্সিবেল মাল্টি-ক্রেডেনশিয়াল অনুরোধ

এছাড়াও, Digital Credentials API, OpenID4VP ব্যবহার করে একসাথে একাধিক ধরনের ক্রেডেনশিয়ালের জন্য অনুরোধ করার সুবিধা প্রদান করে:

  • একসাথে একাধিক অনুরোধ: ব্যবহারকারীর একটি ট্রানজ্যাকশনে একাধিক ক্রেডেনশিয়ালের জন্য অনুরোধ করুন। যেমন, কোনও ট্রাভেল অ্যাপ একই সাথে বোর্ডিং পাস ও সরকারি আইডি চাইতে পারে, যার ফলে ব্যবহারকারী এক ট্যাপেই দুটি পর্যালোচনা করে সম্মতি দিতে পারেন।
  • ফ্লেক্সিবল ফুলফিলমেন্ট: একাধিক ক্রেডেনশিয়াল ধরন বা ইস্যুকারীর মধ্যে যেকোনও একটি গ্রহণ করুন। যেমন, বয়স যাচাইকরণের ফ্লোতে ড্রাইভিং লাইসেন্স বা পাসপোর্ট, যেকোনও একটি গ্রহণ করা যেতে পারে। এর ফলে, ব্যবহারকারী তার হোল্ডারে যে ক্রেডেনশিয়াল সেভ করে রেখেছেন, সেটি ব্যবহার করেই অনুরোধ পূরণ করতে পারবেন।

একটি অনুরোধে একাধিক ক্রেডেনশিয়াল চাওয়ার উদাহরণ এখানে দেওয়া হল:

{
  "credentials": [
    {
      "id": "gov_id1",
      "format": "mso_mdoc",
      "meta": {
        "doctype_value": "org.iso.18013.5.1.mDL"
      },
      "claims": [
        {
          "path": [
            "org.iso.18013.5.1",
            "age_over_21"
          ]
        }
      ]
    },
    {
      "id": "boarding_pass1",
      "format": "dc+sd-jwt",
      "meta": {
        "vct_values": [
          "https://credentials.example.airline/boarding-pass"
        ]
      },
      "claims": [
        {
          "path": [
            "flight_number"
          ]
        },
        {
          "path": [
            "seat"
          ]
        }
      ]
    }
  ]
}
একাধিক ধরনের ক্রেডেনশিয়াল কল করার সময় দেখানো ডিজিটাল ক্রেডেনশিয়াল সিলেক্টর
  দেখানো ছবি।
ছবি ১. একাধিক ধরনের ক্রেডেনশিয়াল কল করার সময় দেখানো ডিজিটাল ক্রেডেনশিয়াল সিলেক্টর।

উত্তর ম্যানেজ করুন

ক্রেডেনশিয়াল অনুরোধ করার পরে, CredentialManager থেকে ফেরত আসা GetCredentialResponse ম্যানেজ করুন।

// Handle the successfully returned credential.
fun verifyResult(result: GetCredentialResponse) {
    val credential = result.credential
    when (credential) {
        is DigitalCredential -> {
            val responseJson = credential.credentialJson
            validateResponseOnServer(responseJson)
        }
        else -> {
            // Catch any unrecognized credential type here.
            Log.e(TAG, "Unexpected type of credential ${credential.type}")
        }
    }
}

ব্যতিক্রম ম্যানেজ করুন

আপনার অ্যাপকে এমন ত্রুটিগুলি ম্যানেজ করতে হবে যা ঘটতে পারে, যার মধ্যে ডিভাইসে কোনও ক্রেডেনশিয়াল না থাকলে বা ব্যবহারকারী অপারেশন বাতিল করলে:

// Handle failure.
fun handleFailure(e: GetCredentialException) {
    when (e) {
        is GetCredentialCancellationException -> {
            // The user intentionally canceled the operation and chose not
            // to share the credential.
        }
        is GetCredentialInterruptedException -> {
            // Retry-able error. Consider retrying the call.
        }
        is NoCredentialException -> {
            // No credential was available.
        }
        is CreateCredentialUnknownException -> {
            // An unknown, usually unexpected, error has occurred. Check the
            // message error for any additional debugging information.
        }
        is CreateCredentialCustomException -> {
            // You have encountered a custom error thrown by the wallet.
            // If you made the API call with a request object that's a
            // subclass of CreateCustomCredentialRequest using a 3rd-party SDK,
            // then you should check for any custom exception type constants
            // within that SDK to match with e.type. Otherwise, drop or log the
            // exception.
        }
        else -> Log.w(TAG, "Unexpected exception type ${e::class.java}")
    }
}