Consumer SDK ให้การให้สิทธิ์โดยใช้โทเค็นเว็บ JSON โทเค็นเว็บ JSON (JWT) คือโทเค็นการให้สิทธิ์ที่ให้การอ้างสิทธิ์อย่างน้อย 1 รายการในบริการ
Consumer SDK ใช้โทเค็นเว็บ JSON ที่แอปพลิเคชันระบุเพื่อ สื่อสารกับ Fleet Engine ดูรายละเอียดของโทเค็นที่เซิร์ฟเวอร์ Fleet Engine คาดหวังได้ที่โทเค็นเว็บ JSON และออกโทเค็นเว็บ JSON
โทเค็นการให้สิทธิ์จะให้สิทธิ์เข้าถึงบริการ Fleet Engine ต่อไปนี้
- TripService- ให้สิทธิ์เข้าถึงรายละเอียดการเดินทางแก่ Consumer SDK ซึ่งรวมถึง ตำแหน่งของยานพาหนะ เส้นทาง และเวลาถึงโดยประมาณ โทเค็นการให้สิทธิ์สำหรับบริการการเดินทาง ต้องมีอ้างสิทธิ์- tripid:TRIP_IDในส่วนหัว- authorizationของโทเค็น โดย- TRIP_IDคือรหัสการเดินทางของการเดินทางแบบออนดีมานด์ที่แชร์
- VehicleService- ให้ข้อมูลเกี่ยวกับ ตำแหน่งโดยประมาณของยานพาหนะแก่ Consumer SDK เพื่อแสดงเลเยอร์ความหนาแน่นของยานพาหนะและ ประมาณเวลาถึงโดยประมาณของจุดรับ เนื่องจาก Consumer SDK ใช้เฉพาะตำแหน่งโดยประมาณ โทเค็นการให้สิทธิ์สำหรับบริการยานพาหนะจึงไม่จำเป็นต้องมี- vehicleidอ้างสิทธิ์
โทเค็นคืออะไร
Fleet Engine กำหนดให้ใช้ JSON Web Token (JWT) สำหรับการเรียกเมธอด API จากสภาพแวดล้อมที่มีความน่าเชื่อถือต่ำ ได้แก่ สมาร์ทโฟนและเบราว์เซอร์
JWT มาจากเซิร์ฟเวอร์ของคุณ มีการลงชื่อ เข้ารหัส และส่งไปยังไคลเอ็นต์ สำหรับการโต้ตอบกับเซิร์ฟเวอร์ในภายหลังจนกว่าจะหมดอายุหรือไม่ถูกต้องอีกต่อไป
รายละเอียดสำคัญ
- ใช้ข้อมูลรับรองเริ่มต้นของแอปพลิเคชันเพื่อตรวจสอบสิทธิ์และ ให้สิทธิ์กับ Fleet Engine
- ใช้บัญชีบริการที่เหมาะสมเพื่อลงนามใน JWT ดูบทบาทของบัญชีบริการ Fleet Engine ในข้อมูลพื้นฐานเกี่ยวกับ Fleet Engine
ดูข้อมูลเพิ่มเติมเกี่ยวกับโทเค็นเว็บ JSON ได้ที่โทเค็นเว็บ JSON ในข้อควรทราบเกี่ยวกับ 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
    }
  }
}