קבלת אסימוני הרשאה

ה-SDK לצרכן מספק הרשאה באמצעות אסימוני אינטרנט JSON. אסימון רשת מסוג JSON (JWT) הוא אסימון הרשאה שמספק הצהרה אחת או יותר עבור שירות.

Consumer SDK משתמש ב-JSON Web Token שסופק על ידי האפליקציה כדי לתקשר עם Fleet Engine. לפרטים על האסימונים הנדרשים על ידי שרת Fleet Engine, ראו אסימוני אינטרנט מסוג JSON והנפקת אסימוני אינטרנט מסוג JSON.

אסימון ההרשאה מספק גישה לשירותי Fleet Engine הבאים:

  • TripService – מעניקה ל-SDK לצרכן גישה לפרטי הנסיעה, כולל מיקום הרכב, המסלול וזמן ההגעה המשוער. אסימוני הרשאה לשירות הנסיעה חייב לכלול הצהרת tripid:TRIP_ID בכותרת authorization של האסימון, TRIP_ID הוא מזהה הנסיעה של הנסיעה על פי דרישה שמשותף.

  • VehicleService – מספק לצרכנים ב-SDK מידע על מיקום משוער של הרכב לצורך הצגת שכבת צפיפות הרכב אנחנו מחשבים את זמן ההגעה המשוער לנקודת איסוף. מאחר שב-SDK לצרכן נעשה שימוש רק בנתונים משוערים מיקומים, אסימוני הרשאה לשירות הרכב לא דורשים תלונה אחת (vehicleid).

מה זה אסימון?

כדי להשתמש ב-Fleet Engine, צריך להשתמש ב-JSON Web Tokens (JWT) חתומים חשבון שירות מתאים לקריאות לשיטה של API מרמת אמון נמוכה של סביבות. סביבות עם רמת אמון נמוכה כוללות סמארטפונים ודפדפנים. JWT נוצר בשרת שלכם, שהוא סביבה מהימנה לגמרי. ה-JWT חתום, מוצפן ומועבר ללקוח עבור השרת הבא אינטראקציות עד שהתוקף שלהן פג או שהוא לא בתוקף.

הקצה העורפי צריך לבצע אימות ואישור מול Fleet Engine באמצעות המנגנונים הרגילים של Application Default Credentials. יצרן להשתמש באסימוני JWT שחתמו על ידי חשבון שירות מתאים. עבור לרשימת תפקידי חשבון השירות, ראו התפקידים בחשבון שירות ב-Fleet Engine במאמר Fleet Engine Basics.

מידע נוסף על אסימוני אינטרנט JSON זמין במאמר JSON Web Tokens ב- היסודות של כלל ה-Fleet Engine.

איך לקוחות מקבלים אסימונים?

אחרי שנהג או צרכן מתחברים לאפליקציה באמצעות פרטי כניסה להרשאה, כל עדכון שיופק מאותו מכשיר חייב אסימוני הרשאה מתאימים, שמעבירים ל-Fleet Engine את הרשאות לאפליקציה.

כמפתח, הטמעת הלקוח שלך אמורה לספק יכולת לעשות הבאים:

  • יש לאחזר אסימון אינטרנט מסוג 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 לצרכנים