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

توفّر حزمة تطوير البرامج (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 على الخادم الخاص بك، والتي تُعد بيئة موثوقة بالكامل. رمز JWT توقيعها وتشفيرها وتمريرها إلى العميل في الخادم اللاحق إلى أن تنتهي صلاحيتها أو لا تعود صالحة.

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

لمزيد من المعلومات حول رموز JSON المميّزة للويب، يمكنك الاطّلاع على رموز JSON المميّزة للويب في أساسيات Fleet Engine

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

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

وبصفتك مطور البرامج، يجب أن يوفر تنفيذ العميل القدرة على تنفيذ ما يلي:

  • أحضر رمز JSON المميّز للويب (JSON) من الخادم.
  • يمكنك إعادة استخدام الرمز المميّز إلى أن تنتهي صلاحيته لتقليل عدد عمليات إعادة تحميل الرمز المميّز.
  • أعِد تحميل الرمز المميّز عند انتهاء صلاحيته.

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

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

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

يوضّح مثال الرمز التالي كيفية تطبيق رمز مميّز للمصادقة. معاودة الاتصال.

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

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

إعداد حزمة تطوير البرامج (SDK) للمستهلكين