From c7601d048c10f70ab11c0087bca9c5d9a6f3822d Mon Sep 17 00:00:00 2001 From: lepicekmichal Date: Mon, 9 Sep 2024 21:15:50 +0200 Subject: [PATCH] fix access token swith when redirecting --- gradle.properties | 2 +- .../signalrkore/HubConnection.kt | 24 +++++++------------ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/gradle.properties b/gradle.properties index 70ec75c..5acdd29 100644 --- a/gradle.properties +++ b/gradle.properties @@ -19,7 +19,7 @@ RELEASE_SIGNING_ENABLED=true GROUP=eu.lepicekmichal.signalrkore POM_ARTIFACT_ID=signalrkore -VERSION_NAME=0.8.6 +VERSION_NAME=0.8.7 POM_NAME=SignalR Kore POM_DESCRIPTION=Connect to SignalR Core server with library written in Kotlin and coroutines. diff --git a/signalrkore/src/commonMain/kotlin/eu/lepicekmichal/signalrkore/HubConnection.kt b/signalrkore/src/commonMain/kotlin/eu/lepicekmichal/signalrkore/HubConnection.kt index 97171ff..cdebf67 100644 --- a/signalrkore/src/commonMain/kotlin/eu/lepicekmichal/signalrkore/HubConnection.kt +++ b/signalrkore/src/commonMain/kotlin/eu/lepicekmichal/signalrkore/HubConnection.kt @@ -18,7 +18,6 @@ import io.ktor.utils.io.core.* import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.IO import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancelChildren import kotlinx.coroutines.delay @@ -50,7 +49,7 @@ class HubConnection private constructor( private val httpClient: HttpClient, private val transportEnum: TransportEnum, private val handshakeResponseTimeout: Duration, - private val headers: Map, + private val headers: MutableMap, // does this need to be mutable? private val skipNegotiate: Boolean, private val automaticReconnect: AutomaticReconnect, override val logger: Logger, @@ -94,7 +93,7 @@ class HubConnection private constructor( httpClient: HttpClient?, protocol: HubProtocol, handshakeResponseTimeout: Duration, - headers: Map, + headers: MutableMap, transportEnum: TransportEnum, transport: Transport?, json: Json, @@ -130,7 +129,7 @@ class HubConnection private constructor( val (negotiationTransport, negotiationUrl) = if (!skipNegotiate) { try { - startNegotiate(baseUrl, 0, headers) + startNegotiate(baseUrl, 0) } catch (ex: Exception) { if (!reconnectionAttempt) { if (automaticReconnect !is AutomaticReconnect.Inactive) reconnect(ex.message) @@ -215,7 +214,6 @@ class HubConnection private constructor( private suspend fun startNegotiate( url: String, negotiateAttempts: Int, - headers: Map, ): Negotiation { if (connectionState.value != HubConnectionState.CONNECTING && connectionState.value != HubConnectionState.RECONNECTING) throw RuntimeException("HubConnection trying to negotiate when not in the CONNECTING state.") @@ -230,17 +228,13 @@ class HubConnection private constructor( is NegotiateResponse.Redirect -> { if (negotiateAttempts >= MAX_NEGOTIATE_ATTEMPTS) throw RuntimeException("Negotiate redirection limit exceeded.") + response.accessToken?.let { token -> + headers["Authorization"] = "Bearer $token" + } + return startNegotiate( - response.url, - negotiateAttempts + 1, - headers.map { - ( - key, - value, - ), - -> - key to (if (key == "Authorization") "Bearer " + response.accessToken else value) - }.toMap() + url = response.url, + negotiateAttempts = negotiateAttempts + 1, ) }