الحصول على الرموز المميّزة للتفويض

توفّر حزمة تطوير البرامج (SDK) للمستهلك التفويض باستخدام رموز JSON المميّزة للويب. ورمز JSON المميّز للويب (JWT) هو رمز تفويض يقدّم طلبًا واحدًا أو أكثر بشأن خدمة معيّنة.

تستخدم حزمة تطوير البرامج (SDK) للمستهلك رمز JSON المميّز للويب الذي يوفّره التطبيق للتواصل مع Fleet Engine. للاطّلاع على تفاصيل الرموز المميّزة التي يتوقعها خادم Fleet Engine، يُرجى الرجوع إلى مقالتَي رموز JSON المميّزة للويب وإصدار رموز JSON المميّزة للويب.

يوفّر رمز التفويض إمكانية الوصول إلى خدمات Fleet Engine التالية:

  • TripService - تمنح حزمة تطوير البرامج (SDK) للمستهلك إمكانية الوصول إلى تفاصيل الرحلة، بما في ذلك موضع المركبة ومسارها والوقت المقدَّر للوصول. يجب أن تتضمّن رموز التفويض لخدمة الرحلة طلب tripid:TRIP_ID في عنوان authorization للرمز المميّز، حيث يمثّل TRIP_ID معرّف الرحلة عند الطلب التي تتم مشاركتها.

  • VehicleService : تمنح حزمة تطوير البرامج (SDK) للمستهلك معلومات عن الموقع الجغرافي التقريبي للمركبة لعرض طبقة كثافة المركبات وتقدير الأوقات المقدَّرة للوصول إلى نقطة الالتقاء. بما أنّ حزمة تطوير البرامج (SDK) للمستهلك تستخدم المواقع الجغرافية التقريبية فقط، لا تتطلّب رموز التفويض لخدمة المركبة طلب vehicleid.

ما هو الرمز المميّز؟

يتطلّب Fleet Engine استخدام رموز JSON المميّزة للويب (JWT) لطلبات طريقة واجهة برمجة التطبيقات من البيئات غير الموثوق بها: الهواتف الذكية والمتصفّحات.

يتم إنشاء رمز JWT على الخادم وتوقيعه وتشفيره وتمريره إلى العميل لإجراء تفاعلات لاحقة مع الخادم إلى أن تنتهي صلاحيته أو يصبح غير صالح.

التفاصيل الأساسية

لمزيد من المعلومات عن رموز JSON المميّزة للويب، يُرجى الرجوع إلى مقالة رموز JSON المميّزة للويب في أساسيات Fleet Engine.

كيف يحصل العملاء على الرموز المميّزة؟

بعد تسجيل السائق أو المستهلك الدخول إلى تطبيقك باستخدام بيانات اعتماد التفويض المناسبة، يجب أن تستخدم أي تعديلات صادرة من هذا الجهاز رموز تفويض مناسبة، ما يوضّح لـ Fleet Engine أذونات التطبيق.

بصفتك المطوّر، يجب أن يوفّر تطبيق العميل إمكانية إجراء ما يلي:

  • جلب رمز JSON المميّز للويب من الخادم
  • إعادة استخدام الرمز المميّز إلى أن تنتهي صلاحيته لتقليل عمليات إعادة تحميل الرموز المميّزة
  • إعادة تحميل الرمز المميّز عند انتهاء صلاحيته

تنشئ الفئة AuthTokenFactory رموز التفويض في وقت إشعار بشأن الموقع الجغرافي. يجب أن تضمّن حزمة تطوير البرامج (SDK) الرموز المميّزة مع معلومات التعديل لإرسالها إلى Fleet Engine. تأكَّد من أنّ تطبيقك من جهة الخادم يمكنه إصدار الرموز المميّزة قبل تهيئة حزمة تطوير البرامج (SDK).

للاطّلاع على تفاصيل الرموز المميّزة التي تتوقعها خدمة Fleet Engine، يُرجى الرجوع إلى مقالة إصدار رموز JSON المميّزة للويب لخدمة Fleet Engine.

مثال على أداة جلب رموز التفويض

يوضّح مثال الرمز التالي كيفية تنفيذ معاودة الاتصال لرمز التفويض.

جافا

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

الخطوات التالية

تهيئة حزمة تطوير البرامج (SDK) للمستهلك