অনুমোদন টোকেন পাওয়া

Consumer SDK, JSON ওয়েব টোকেন ব্যবহার করে অনুমোদন প্রদান করে। JSON ওয়েব টোকেন (JWT) হল একটি অনুমোদনের টোকেন যা কোনও পরিষেবার উপর এক বা একাধিক দাবি প্রদান করে।

Consumer SDK, Fleet Engine-এর সাথে যোগাযোগ করার জন্য অ্যাপ্লিকেশন দ্বারা প্রদান করা JSON ওয়েব টোকেন ব্যবহার করে। Fleet Engine সার্ভারের প্রত্যাশিত টোকেনের বিবরণের জন্য, JSON ওয়েব টোকেন এবং JSON ওয়েব টোকেন ইস্যু করুন দেখুন।

অনুমোদন টোকেন নিম্নলিখিত Fleet Engine পরিষেবাগুলিতে অ্যাক্সেস প্রদান করে:

  • TripService - Consumer SDK-কে ট্রিপের বিবরণ অ্যাক্সেস করার অনুমতি দেয়, যার মধ্যে গাড়ির অবস্থান, রুট ও ETA অন্তর্ভুক্ত। ট্রিপ পরিষেবার জন্য অথরাইজেশন টোকেনে টোকেনের authorization হেডারে অবশ্যই একটি tripid:TRIP_ID দাবি অন্তর্ভুক্ত থাকতে হবে, যেখানে TRIP_ID হল শেয়ার করা অন-ডিমান্ড ট্রিপের ট্রিপ আইডি।

  • VehicleService - এটি Consumer SDK-কে গাড়ির আনুমানিক লোকেশন সম্পর্কে তথ্য দেয় যাতে গাড়ির ডেনসিটি লেয়ার দেখানো যায় এবং পিক-আপ পয়েন্টের ETA অনুমান করা যায়। Consumer SDK শুধুমাত্র আনুমানিক লোকেশন ব্যবহার করে বলে, গাড়ি পরিষেবার জন্য অথরাইজেশন টোকেনে vehicleid ক্লেমের প্রয়োজন হয় না।

টোকেন কী?

কম-বিশ্বস্ত পরিবেশ: স্মার্টফোন এবং ব্রাউজার থেকে API মেথড কলের জন্য Fleet Engine-এর JSON ওয়েব টোকেন (JWTs) ব্যবহার করা প্রয়োজন।

আপনার সার্ভারে একটি JWT তৈরি হয়, সেটি সাইন ও এনক্রিপ্ট করা হয় এবং ক্লায়েন্টের কাছে পাঠানো হয় যাতে মেয়াদ শেষ না হওয়া বা আর বৈধ না থাকা পর্যন্ত সার্ভারের সাথে ইন্টার‍্যাক্ট করা যায়।

গুরুত্বপূর্ণ বিবরণ

JSON ওয়েব টোকেন সম্পর্কে আরও জানতে, Fleet Engine Essentials-এ JSON ওয়েব টোকেন দেখুন।

ক্লায়েন্ট কীভাবে টোকেন পান?

ড্রাইভার বা গ্রাহক উপযুক্ত অনুমোদনের ক্রেডেনশিয়াল ব্যবহার করে আপনার অ্যাপে লগ-ইন করলে, সেই ডিভাইস থেকে ইস্যু করা যেকোনও আপডেটের ক্ষেত্রে উপযুক্ত অনুমোদনের টোকেন ব্যবহার করতে হবে, যা Fleet Engine-এর সাথে অ্যাপের অনুমতির ব্যাপারে যোগাযোগ করে।

ডেভেলপার হিসেবে, আপনার ক্লায়েন্ট ইমপ্লিমেন্টেশনে নিম্নলিখিত কাজগুলি করার ক্ষমতা প্রদান করতে হবে:

  • আপনার সার্ভার থেকে JSON ওয়েব টোকেন ফেচ করুন।
  • টোকেন রিফ্রেশ করার সংখ্যা কমাতে, মেয়াদ শেষ না হওয়া পর্যন্ত টোকেনটি আবার ব্যবহার করুন।
  • টোকেনের মেয়াদ শেষ হয়ে গেলে সেটি রিফ্রেশ করুন।

AuthTokenFactory ক্লাস লোকেশন আপডেট করার সময় অথরাইজেশন টোকেন জেনারেট করে। SDK-কে অবশ্যই আপডেট তথ্যের সাথে টোকেন প্যাকেজ করতে হবে Fleet Engine-এ পাঠাতে। SDK শুরু করার আগে আপনার সার্ভার-সাইড ইমপ্লিমেন্টেশন টোকেন ইস্যু করতে পারে কিনা তা নিশ্চিত করুন।

Fleet Engine পরিষেবার জন্য প্রয়োজনীয় টোকেনের বিবরণ জানতে, Fleet Engine-এর জন্য JSON ওয়েব টোকেন ইস্যু করুন দেখুন।

অনুমোদন টোকেন ফেচারের উদাহরণ

নিচের কোডের উদাহরণ থেকে বোঝা যাবে যে কীভাবে অনুমোদনের টোকেন কলব্যাক প্রয়োগ করতে হয়।

Java

class JsonAuthTokenFactory implements AuthTokenFactory {

  private static final String TOKEN_URL =
      "https://yourauthserver.example/token";

  private static class CachedToken {
    String tokenValue;
    long expiryTimeMs;
    String tripId;
  }

  private CachedToken token;

  /*

*   This method is called on a background thread. Blocking is OK. However, be
*   aware that no information can be obtained from Fleet Engine until this
*   method returns.
*/
@Override
public String getToken(AuthTokenContext context) {
  // If there is no existing token or token has expired, go get a new one.
  String tripId = context.getTripId();
  if (tripId == null) {
    throw new RuntimeException("Trip ID is missing from AuthTokenContext");
  }
  if (token == null || System.currentTimeMillis() > token.expiryTimeMs ||
      !tripId.equals(token.tripId)) {
    token = fetchNewToken(tripId);
  }
  return token.tokenValue;
}

  private static CachedToken fetchNewToken(String tripId) {
    String url = TOKEN_URL + "/" + tripId;
    CachedToken token = new CachedToken();

    try (Reader r = new InputStreamReader(new URL(url).openStream())) {
      com.google.gson.JsonObject obj
          = com.google.gson.JsonParser.parseReader(r).getAsJsonObject();

      token.tokenValue = obj.get("ServiceToken").getAsString();
      token.expiryTimeMs = obj.get("TokenExpiryMs").getAsLong();

      /*

    *   The expiry time could be an hour from now, but just to try and avoid
    *   passing expired tokens, we subtract 5 minutes from that time.
    */
    token.expiryTimeMs -= 5 * 60 * 1000;
  } catch (IOException e) {
    /*
    *   It's OK to throw exceptions here. The error listeners will receive the
    *   error thrown here.
    */
    throw new RuntimeException("Could not get auth token", e);
  }
  token.tripId = tripId;

    return token;
  }
}

Kotlin

class JsonAuthTokenFactory : AuthTokenFactory() {

  private var token: CachedToken? = null

  /*

*   This method is called on a background thread. Blocking is OK. However, be
*   aware that no information can be obtained from Fleet Engine until this
*   method returns.
*/
override fun getToken(context: AuthTokenContext): String {
  // If there is no existing token or token has expired, go get a new one.
  val tripId =
    context.getTripId() ?:
      throw RuntimeException("Trip ID is missing from AuthTokenContext")

    if (token == null || System.currentTimeMillis() > token.expiryTimeMs ||
        tripId != token.tripId) {
      token = fetchNewToken(tripId)
    }

    return token.tokenValue
  }

  class CachedToken(
    var tokenValue: String? = "",
    var expiryTimeMs: Long = 0,
    var tripId: String? = "",
  )

  private companion object {
    const val TOKEN_URL = "https://yourauthserver.example/token"

    fun fetchNewToken(tripId: String) {
      val url = "$TOKEN_URL/$tripId"
      val token = CachedToken()

      try {
        val reader = InputStreamReader(URL(url).openStream())

        reader.use {
          val obj = com.google.gson.JsonParser.parseReader(r).getAsJsonObject()

          token.tokenValue = obj.get("ServiceToken").getAsString()
          token.expiryTimeMs = obj.get("TokenExpiryMs").getAsLong()

          /*

        *   The expiry time could be an hour from now, but just to try and avoid
        *   passing expired tokens, we subtract 5 minutes from that time.
        */
        token.expiryTimeMs -= 5 * 60 * 1000
      }
    } catch (e: IOException) {
      /*
            *   It's OK to throw exceptions here. The error listeners will receive the
            *   error thrown here.
      */
      throw RuntimeException("Could not get auth token", e)
    }

      token.tripId = tripId

      return token
    }
  }
}

এর পরে কী করতে হবে

Consumer SDK শুরু করা