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