Kotlin के लिए OAuth क्लाइंट: ktor-client और kotlinx-serialization के साथ प्रोवाइडर इंटीग्रेशन
OAuth 2.0 एक मानक ऑथराइज़ेशन प्रोटोकॉल है जो एप्लिकेशन को उपयोगकर्ता के क्रेडेंशियल्स साझा किए बिना उनके संसाधनों तक सीमित पहुंच प्रदान करता है। Kotlin इकोसिस्टम में, JVM संगतता और मल्टीप्लेटफ़ॉर्म सपोर्ट के कारण, आधुनिक लाइब्रेरीज़ का उपयोग करके OAuth क्लाइंट को प्रभावी ढंग से लागू किया जा सकता है। इस लेख में हम ktor-client (HTTP रिक्वेस्ट के लिए) और kotlinx-serialization (JSON प्रोसेसिंग के लिए) का उपयोग करके Kotlin में OAuth क्लाइंट बनाने की प्रक्रिया समझेंगे।
OAuth क्लाइंट के मुख्य घटक
- HTTP क्लाइंट —
ktor-clientऑथराइज़ेशन और रिसोर्स सर्वर को रिक्वेस्ट भेजने के लिए। - सीरियलाइज़ेशन —
kotlinx-serializationरिस्पॉन्स को data-क्लासेस में पार्स करने के लिए। - टोकन स्टोरेज — in-memory या स्थायी (SharedPreferences, फ़ाइलें)।
- स्टेट मैनेजर — authorization code, refresh token, CSRF सुरक्षा को हैंडल करने के लिए।
डिपेंडेंसीज़ जोड़ना
Kotlin में OAuth के साथ काम करने के लिए build.gradle.kts (या build.gradle) में निम्नलिखित डिपेंडेंसीज़ जोड़ें:
// build.gradle.kts
dependencies {
implementation("io.ktor:ktor-client-core:2.3.12")
implementation("io.ktor:ktor-client-cio:2.3.12") // इंजन
implementation("io.ktor:ktor-client-content-negotiation:2.3.12")
implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.12")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0")
}
OAuth क्लाइंट की बुनियादी आर्किटेक्चर
लॉजिक को तीन लेयर में बांटें:
- मॉडल (DTO) — रिक्वेस्ट/रिस्पॉन्स के लिए data-क्लासेस।
- सर्विस — क्लास जो ऑथराइज़ेशन फ़्लो को मैनेज करता है।
- क्लाइंट — ktor-client के ऊपर एक रैपर जो विभिन्न ग्रांट्स के लिए मेथड प्रदान करता है।
डेटा मॉडल
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class TokenResponse(
@SerialName("access_token") val accessToken: String,
@SerialName("token_type") val tokenType: String = "Bearer",
@SerialName("expires_in") val expiresIn: Long? = null,
@SerialName("refresh_token") val refreshToken: String? = null,
@SerialName("scope") val scope: String? = null,
@SerialName("id_token") val idToken: String? = null // OpenID Connect के लिए
)
@Serializable
data class OAuthError(
val error: String,
@SerialName("error_description") val errorDescription: String? = null
)
OAuth2 क्लाइंट
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json
class OAuth2Client(
private val clientId: String,
private val clientSecret: String,
private val redirectUri: String,
private val tokenEndpoint: String,
private val authEndpoint: String
) {
private val httpClient = HttpClient(CIO) {
install(ContentNegotiation) {
json(Json {
ignoreUnknownKeys = true
isLenient = true
})
}
}
// ऑथराइज़ेशन पेज पर रीडायरेक्ट करने के लिए URL जनरेट करें
fun getAuthorizationUrl(state: String, scope: String = "openid profile email"): String {
return "$authEndpoint?response_type=code" +
"&client_id=$clientId" +
"&redirect_uri=${java.net.URLEncoder.encode(redirectUri, "UTF-8")}" +
"&scope=${java.net.URLEncoder.encode(scope, "UTF-8")}" +
"&state=$state"
}
// authorization code को टोकन से बदलें