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