Skip to content

Commit

Permalink
fix access token swith when redirecting
Browse files Browse the repository at this point in the history
  • Loading branch information
lepicekmichal committed Sep 9, 2024
1 parent 2b1aeb0 commit c7601d0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,7 +49,7 @@ class HubConnection private constructor(
private val httpClient: HttpClient,
private val transportEnum: TransportEnum,
private val handshakeResponseTimeout: Duration,
private val headers: Map<String, String>,
private val headers: MutableMap<String, String>, // does this need to be mutable?
private val skipNegotiate: Boolean,
private val automaticReconnect: AutomaticReconnect,
override val logger: Logger,
Expand Down Expand Up @@ -94,7 +93,7 @@ class HubConnection private constructor(
httpClient: HttpClient?,
protocol: HubProtocol,
handshakeResponseTimeout: Duration,
headers: Map<String, String>,
headers: MutableMap<String, String>,
transportEnum: TransportEnum,
transport: Transport?,
json: Json,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -215,7 +214,6 @@ class HubConnection private constructor(
private suspend fun startNegotiate(
url: String,
negotiateAttempts: Int,
headers: Map<String, String>,
): Negotiation {
if (connectionState.value != HubConnectionState.CONNECTING && connectionState.value != HubConnectionState.RECONNECTING)
throw RuntimeException("HubConnection trying to negotiate when not in the CONNECTING state.")
Expand All @@ -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,
)
}

Expand Down

0 comments on commit c7601d0

Please sign in to comment.