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

توفّر حزمة تطوير البرامج (SDK) للمستهلك التفويض باستخدام رموز JSON المميّزة للويب. ‫JSON Web Token ‏(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) للمستهلك