https://kotlinlang.org logo
#ktor
Title
# ktor
m

Md Hanif

11/01/2021, 5:12 PM
Hi I am facing one issue with Ktor Auth module. I have a kmm project in which i have been using ktor for the api calls. I have a requirement in which i need to update my access token with the help of refresh tokens if they are expired. Basically i just need to add an authentication module in my ktor client. No I have gone through all the documentation of Ktor (https://ktor.io/docs/auth.html#configure_authentication) and added
Auth
module in my kmm. Now when i add auth module in my http client it gets added successfully and whenever i receive UnAuthorized user error from any api it calls my refresh token api. The issue is even though it calls my refresh token api but on success of refresh token it does not call the other api from which I have received UnAuthorized user error. It works as expected in android but the only issue is in ios client. Expected (Works fine in Android Http client) :- - Call any api -> if received any UnAuthorized user error call refresh token api -> onSuccess of refresh token api -> Call the first api again with the updated refresh token. Issue i am facing :- - Call any api -> if received any UnAuthorized user error call refresh token api -> onSuccess of refresh token api -> Does nothing on this step just throws unauthorized user error from the first api. HttpClient for iOS :-
actual class HttpBaseClient {
actual val tokenClient = HttpClient {
defaultRequest {
host = ApiEndPoints.Base.url
url {
protocol = URLProtocol.HTTPS
}
contentType(ContentType.Application.Json)
header(CONNECTION, CLOSE)
}
install(JsonFeature) {
val json = kotlinx.serialization.json.Json {
ignoreUnknownKeys = true
coerceInputValues = true
}
serializer = KotlinxSerializer(json)
}
}
actual val httpClient: HttpClient = HttpClient {
defaultRequest {
host = ApiEndPoints.Base.url
url {
protocol = URLProtocol.HTTPS
}
contentType(ContentType.Application.Json)
header(CONNECTION, CLOSE)
}
// Validate Response
expectSuccess = false
// Install Auth
install(Auth) {
lateinit var refreshTokenInfo : LoginResponse
bearer {
refreshTokens { unauthorizedResponse: HttpResponse ->
NSLog("Unauthorized response received")
BaseAPIClass().refreshAuthToken().fold(
failed = {
// On Failed
NSLog("Token Failed"). // No Callback received here
},
succeeded = { response ->
refreshTokenInfo = response
NSLog("Token Updated") // No Callback received here even when api is success
}
)
BearerTokens(
accessToken = refreshTokenInfo.accessToken ?: "",
refreshToken = refreshTokenInfo.refreshToken ?: ""
)
}
}
}
// JSON Deserializer
install(JsonFeature) {
val json = kotlinx.serialization.json.Json {
ignoreUnknownKeys = true
coerceInputValues = true
}
serializer = KotlinxSerializer(json)
}
Android Client (Pretty much same):-
actual class HttpBaseClient {
actual val tokenClient = HttpClient {
defaultRequest {
host = ApiEndPoints.Base.url
url {
protocol = URLProtocol.HTTPS
}
contentType(ContentType.Application.Json)
header(CONNECTION, CLOSE)
}
install(JsonFeature) {
val json = kotlinx.serialization.json.Json {
ignoreUnknownKeys = true
coerceInputValues = true
}
serializer = KotlinxSerializer(json)
}
install(Logging) {
logger = Logger.DEFAULT
level = LogLevel.ALL
}
}
actual val httpClient: HttpClient = HttpClient {
defaultRequest {
host = ApiEndPoints.Base.url
url {
protocol = URLProtocol.HTTPS
}
contentType(ContentType.Application.Json)
header(CONNECTION, CLOSE)
}
// Validate Response
expectSuccess = false
//Authentication
install(Auth) {
lateinit var refreshTokenInfo : LoginResponse
bearer {
refreshTokens { unauthorizedResponse: HttpResponse ->
BaseAPIClass().refreshAuthToken().fold(
failed = {
// On Failed
},
succeeded = { response ->
refreshTokenInfo = response
}
)
BearerTokens(
accessToken = refreshTokenInfo.accessToken ?: "",
refreshToken = refreshTokenInfo.refreshToken ?: ""
)
}
}
}
Ktor version :- 1.6.2 (tried 1.6.4 as well after reading this :- https://youtrack.jetbrains.com/issue/KTOR-491 but didn't work)
🧵 5
Any solutions or workaround for now ?
a

Aleksei Tirman [JB]

11/02/2021, 8:30 AM
I've left a comment in KTOR-3350.
👍 1
164 Views