Consumer SDK 可使用 JSON Web Token 提供授權,JSON Web Token (JWT) 是一種授權權杖,可以對服務提供一或多項憑證附加資訊。
Consumer SDK 會使用應用程式提供的 JSON Web Token, 與 Fleet Engine 通訊如要進一步瞭解 Fleet Engine 伺服器,請參閱 JSON Web Token 並發出 JSON Web Token。
授權權杖提供下列 Fleet Engine 服務的存取權:
TripService
- 授予 Consumer SDK 存取行程詳細資料的權限,包括 車輛位置、路線和預計到達時間。行程服務的授權權杖 必須在符記的authorization
標頭中加入tripid:TRIP_ID
憑證附加資訊, 其中TRIP_ID
是分享的隨選行程的行程 ID。VehicleService
:提供 Consumer SDK 的 用來顯示車輛密度圖層的大概車輛位置 估算取貨地點的預計到達時間由於 Consumer SDK 只會使用概略值 車輛服務的授權權杖不需要vehicleid
著作權聲明。
什麼是權杖?
Fleet Engine 必須使用 適用於來自 low-trust 的 API 方法呼叫的適當服務帳戶 環境。低信任的環境包括智慧型手機和瀏覽器。JWT 起源於您的伺服器,這也是值得信賴的環境JWT 簽署、加密並傳遞至用戶端,以供後續伺服器使用 互動,直到過期或不再有效。
您的後端應使用 標準的應用程式預設憑證機制。廠牌 請務必使用適當服務帳戶簽署的 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 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
}
}
}