Consumer SDK, JSON वेब टोकन का इस्तेमाल करके अनुमति देता है. JSON वेब टोकन (JWT) एक अनुमति टोकन है, जो किसी सेवा पर एक या उससे ज़्यादा दावे करता है.
Consumer SDK, Fleet Engine से कम्यूनिकेट करने के लिए, ऐप्लिकेशन से मिले JSON वेब टोकन का इस्तेमाल करता है. Fleet Engine सर्वर को जिन टोकन की ज़रूरत होती है उनके बारे में जानने के लिए, JSON वेब टोकन और JSON वेब टोकन जारी करना लेख पढ़ें.
अनुमति टोकन से, Fleet Engine की इन सेवाओं का ऐक्सेस मिलता है:
TripService- Consumer SDK को यात्रा की जानकारी का ऐक्सेस देता है. इसमें वाहन की जगह, रूट, और ईटीए शामिल हैं. यात्रा की सेवा के लिए अनुमति टोकन में , टोकन केauthorizationहेडर मेंtripid:TRIP_IDदावा शामिल होना चाहिए . यहांTRIP_ID, ऑन-डिमांड यात्रा का आईडी है.VehicleService- Consumer SDK को वाहन की अनुमानित जगह की जानकारी देता है, ताकि वाहन डेंसिटी लेयर दिखाई जा सके और पिकअप पॉइंट के ईटीए का अनुमान लगाया जा सके. Consumer SDK सिर्फ़ अनुमानित जगहों का इस्तेमाल करता है. इसलिए, वाहन की सेवा के लिए अनुमति टोकन मेंvehicleidदावे की ज़रूरत नहीं होती.
टोकन क्या होता है?
Fleet Engine को कम भरोसे वाले एनवायरमेंट (स्मार्टफ़ोन और ब्राउज़र) से, एपीआई के तरीके को कॉल करने के लिए JSON वेब टोकन (JWT) की ज़रूरत होती है.
JWT आपके सर्वर पर जनरेट होता है. इस पर हस्ताक्षर किए जाते हैं, इसे एनक्रिप्ट किया जाता है, और क्लाइंट को भेजा जाता है. ऐसा तब तक होता है, जब तक यह खत्म नहीं हो जाता या मान्य नहीं रहता.
मुख्य जानकारी
- Fleet Engine के ख़िलाफ़ पुष्टि करने और अनुमति पाने के लिए, ऐप्लिकेशन के डिफ़ॉल्ट क्रेडेंशियल का इस्तेमाल करें.
- JWT पर हस्ताक्षर करने के लिए, सही सेवा खाते का इस्तेमाल करें. **Fleet Engine की बुनियादी बातें** में, Fleet Engine के सेवा खाते की भूमिकाएं देखें.
JSON वेब टोकन के बारे में ज़्यादा जानने के लिए, JSON वेब टोकन लेख पढ़ें. यह लेख Fleet Engine की ज़रूरी बातें में दिया गया है.
क्लाइंट को टोकन कैसे मिलते हैं?
जब कोई ड्राइवर या ग्राहक, अनुमति के सही क्रेडेंशियल का इस्तेमाल करके आपके ऐप्लिकेशन में लॉग इन करता है, तो उस डिवाइस से जारी किए गए किसी भी अपडेट के लिए, अनुमति के सही टोकन का इस्तेमाल करना ज़रूरी है. इससे Fleet Engine को ऐप्लिकेशन की अनुमतियों के बारे में पता चलता है.
डेवलपर के तौर पर, आपके क्लाइंट के लागू करने के तरीके में ये काम करने की सुविधा होनी चाहिए:
- अपने सर्वर से JSON वेब टोकन फ़ेच करना.
- टोकन को रीफ़्रेश करने की ज़रूरत को कम करने के लिए, खत्म होने तक उसका फिर से इस्तेमाल करना.
- टोकन खत्म होने पर उसे रीफ़्रेश करना.
AuthTokenFactory क्लास, डिवाइस की जगह की जानकारी का अपडेट होने के समय, अनुमति टोकन जनरेट करती है. Fleet Engine को भेजने के लिए, SDK टूल को टोकन को अपडेट की जानकारी के साथ पैकेज करना होगा. पक्का करें कि आपका सर्वर-साइड लागू करने का तरीका, SDK टूल को शुरू करने से पहले टोकन जारी कर सके.
Fleet Engine की सेवा को जिन टोकन की ज़रूरत होती है उनके बारे में जानने के लिए, Fleet Engine के लिए JSON वेब टोकन जारी करना लेख पढ़ें.
अनुमति टोकन फ़ेचर का उदाहरण
यहां दिए गए कोड के उदाहरण में, अनुमति टोकन कॉलबैक को लागू करने का तरीका बताया गया है.
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
}
}
}