অনুমোদন টোকেন পান

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

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

অনুমোদন টোকেন নিম্নলিখিত ফ্লিট ইঞ্জিন পরিষেবাগুলিতে অ্যাক্সেস প্রদান করে:

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

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

টোকেন কী?

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

একটি JWT আপনার সার্ভারে উৎপন্ন হয়, স্বাক্ষরিত হয়, এনক্রিপ্ট করা হয় এবং পরবর্তী সার্ভার ইন্টারঅ্যাকশনের জন্য ক্লায়েন্টের কাছে প্রেরণ করা হয় যতক্ষণ না এটির মেয়াদ শেষ হয় বা আর বৈধ থাকে না।

মূল বিবরণ

  • ফ্লিট ইঞ্জিনের বিরুদ্ধে প্রমাণীকরণ এবং অনুমোদনের জন্য অ্যাপ্লিকেশন ডিফল্ট শংসাপত্র ব্যবহার করুন।
  • JWT স্বাক্ষর করার জন্য একটি উপযুক্ত পরিষেবা অ্যাকাউন্ট ব্যবহার করুন। Fleet Engine Basics-Fleet Engine serviceaccount ভূমিকা দেখুন।

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

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

একবার ড্রাইভার বা গ্রাহক উপযুক্ত অনুমোদনের শংসাপত্র ব্যবহার করে আপনার অ্যাপে লগ ইন করলে, সেই ডিভাইস থেকে জারি করা যেকোনো আপডেটের জন্য উপযুক্ত অনুমোদন টোকেন ব্যবহার করতে হবে, যা ফ্লিট ইঞ্জিনকে অ্যাপের অনুমতি সম্পর্কে অবহিত করে।

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

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

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

ফ্লিট ইঞ্জিন পরিষেবা দ্বারা প্রত্যাশিত টোকেনগুলির বিশদ বিবরণের জন্য, ফ্লিট ইঞ্জিনের জন্য JSON ওয়েব টোকেন ইস্যু দেখুন।

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

নিম্নলিখিত কোড উদাহরণটি দেখায় যে কীভাবে একটি অনুমোদন টোকেন কলব্যাক বাস্তবায়ন করতে হয়।

জাভা

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

কোটলিন

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

এরপর কি?

কনজিউমার SDK আরম্ভ করুন