รับโทเค็นการให้สิทธิ์

Consumer SDK จะให้สิทธิ์โดยใช้ JSON Web Token JSON Web Token (JWT) คือโทเค็นการให้สิทธิ์ที่มีการอ้างสิทธิ์อย่างน้อย 1 รายการในบริการ

Consumer SDK ใช้ JSON เว็บโทเค็นที่แอปพลิเคชันมีให้เพื่อ สื่อสารกับ Fleet Engine สำหรับรายละเอียดโทเค็นที่คาดว่า เซิร์ฟเวอร์ Fleet Engine โปรดดู JSON Web Tokens และออกโทเค็นเว็บ JSON

โทเค็นการให้สิทธิ์จะให้สิทธิ์เข้าถึงบริการ Fleet Engine ต่อไปนี้

  • TripService - ให้สิทธิ์ Consumer SDK เข้าถึงรายละเอียดการเดินทาง ซึ่งรวมถึง ตําแหน่งของรถ เส้นทาง และเวลาถึงโดยประมาณ โทเค็นการให้สิทธิ์สำหรับบริการการเดินทาง ต้องมีการอ้างสิทธิ์ tripid:TRIP_ID ในส่วนหัว authorization ของโทเค็น โดยที่ TRIP_ID คือรหัสการเดินทางของการเดินทางแบบออนดีมานด์ที่กำลังแชร์

  • VehicleService - ให้ข้อมูล SDK สำหรับผู้บริโภคเกี่ยวกับ ตำแหน่งโดยประมาณของรถเพื่อแสดงเลเยอร์ความหนาแน่นของยานพาหนะและ กำลังประมาณเวลาถึงโดยประมาณของจุดรับสินค้า เนื่องจาก Consumer SDK ใช้เฉพาะค่าประมาณ สถานที่ โทเค็นการให้สิทธิ์สำหรับบริการยานพาหนะไม่จำเป็นต้องมี การอ้างสิทธิ์ vehicleid ครั้ง

โทเค็นคืออะไร

Fleet Engine กำหนดให้ใช้ JSON Web Token (JWT) ที่ลงนามโดย บัญชีบริการที่เหมาะสมสำหรับการเรียกเมธอด API จาก low-Trust สภาพแวดล้อมการทำงาน สภาพแวดล้อมที่มีความน่าเชื่อถือต่ำรวมถึงสมาร์ทโฟนและเบราว์เซอร์ต่างๆ JWT จะทำงานในเซิร์ฟเวอร์ของคุณ ซึ่งเป็นสภาพแวดล้อมที่เชื่อถือได้อย่างสมบูรณ์ JWT ลงชื่อ เข้ารหัส และส่งต่อไปยังไคลเอ็นต์สำหรับเซิร์ฟเวอร์ต่อๆ ไป การโต้ตอบต่างๆ จนกว่าหมดอายุหรือใช้ไม่ได้

แบ็กเอนด์ของคุณควรตรวจสอบสิทธิ์และให้สิทธิ์กับ Fleet Engine โดยใช้ กลไก Application Default Credentials แบบมาตรฐาน สร้าง อย่าลืมใช้ JWT ที่ลงชื่อโดยบัญชีบริการที่เหมาะสม สำหรับ รายการของบทบาทบัญชีบริการ โปรดดูบทบาทบัญชีบริการ Fleet Engine ในข้อมูลพื้นฐานเกี่ยวกับ Fleet Engine

สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ JSON Web Token โปรดดู JSON Web Token ใน Fleet Engine Essentials

ลูกค้าจะได้รับโทเค็นอย่างไร

เมื่อคนขับหรือผู้บริโภคเข้าสู่ระบบแอปของคุณโดยใช้ ข้อมูลเข้าสู่ระบบการให้สิทธิ์ การอัปเดตที่มาจากอุปกรณ์จะต้องใช้ โทเค็นการให้สิทธิ์ที่เหมาะสม ซึ่งจะสื่อสารกับ Fleet Engine สิทธิ์ของแอป

ในฐานะนักพัฒนาซอฟต์แวร์ การใช้งานของไคลเอ็นต์ของคุณควรจะช่วยให้คุณ ดังต่อไปนี้

  • เรียก JSON Web Token จากเซิร์ฟเวอร์ของคุณ
  • ใช้โทเค็นซ้ำจนกว่าจะหมดอายุเพื่อลดการรีเฟรชโทเค็น
  • โปรดรีเฟรชโทเค็นเมื่อหมดอายุ

คลาส AuthTokenFactory สร้างโทเค็นการให้สิทธิ์เมื่ออัปเดตตำแหน่ง SDK ต้องสร้างแพ็กเกจโทเค็นที่มีการอัปเดต เพื่อส่งไปยัง Fleet Engine โปรดตรวจสอบว่าฝั่งเซิร์ฟเวอร์ อาจออกโทเค็นก่อนที่จะเริ่มต้น SDK

โปรดดูรายละเอียดโทเค็นที่บริการ Fleet Engine คาดไว้ที่หัวข้อปัญหา JSON Web Token สำหรับ Fleet Engine

ตัวอย่างตัวดึงข้อมูลโทเค็นการให้สิทธิ์

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีติดตั้งโทเค็นการให้สิทธิ์ Callback

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 ของผู้บริโภค