scoop:scoop.http.client:0.1.0
src/authentication/bearer/owner.scoop
Selected production source · src/authentication/bearer/owner.scoop
1
package scoop.http.client3
import scoop.http.HttpCredentials5
internal fun startClientBearerFlight(flight: ClientBearerFlight) {6
try {7
launchClientTask(8
flight.parent,9
{ error -> finishClientBearerFlight(flight, ClientBearerOutcome.Failed(error)) }10
) {11
suspendCoroutine<Unit> {12
hold ->13
flight.client.lock.withLock { flight.hold = Some(hold) }14
startCoroutine<ClientBearerValue>(15
suspend { runClientBearerFlight(flight) },16
ClientBearerCompletion(flight)17
)18
}19
}20
} catch (error: Throwable) {21
finishClientBearerFlight(flight, ClientBearerOutcome.Failed(error))22
}23
}25
private suspend fun runClientBearerFlight(flight: ClientBearerFlight): ClientBearerValue =26
coroutineScope<ClientBearerValue> {27
val scope = this29
val stopped =30
flight.client.lock.withLock {31
flight.scope = Some(scope);32
flight.stopping33
}34
if (stopped)35
scope.job.cancel()36
scope.job.ensureActive()37
val provider = flight.entry.provider38
val previous = flight.previous39
val tokens =40
if (!clientPresent(previous))41
provider.loadTokens(flight.request)42
else {43
val request = flight.request44
val refreshed =45
provider.refreshTokens(46
HttpBearerRefreshRequest(47
request.request,48
request.challenge!!,49
request.attempt,50
previous!!.tokens,51
request.proxyUrl52
)53
)54
scope.job.ensureActive()55
if (56
!clientPresent(refreshed) ||57
refreshed!!.accessToken ==58
previous!!.tokens.accessToken59
)60
throw HttpAuthenticationException(61
HttpAuthenticationFailure.Rejected,62
request.proxyUrl ?: request.request.url63
)64
refreshed!!65
}66
scope.job.ensureActive()67
val field =68
try {69
flight.entry.scope.field(HttpCredentials.withToken68("Bearer", tokens.accessToken))70
}71
catch (error: IllegalArgumentException) {72
throw HttpAuthenticationException(73
HttpAuthenticationFailure.InvalidCredentials,74
flight.request.proxyUrl ?: flight.request.request.url,75
cause = Some(error)76
)77
}78
ClientBearerValue(tokens, field)79
}81
private class ClientBearerCompletion(val flight: ClientBearerFlight): Continuation<82
ClientBearerValue83
> {84
public override fun resume(value: ClientBearerValue) {85
finishClientBearerFlight(flight, ClientBearerOutcome.Value(value))86
}87
public override fun resumeWithException(exception: Throwable) {88
finishClientBearerFlight(flight, ClientBearerOutcome.Failed(exception))89
}90
}92
internal fun finishClientBearerFlight(flight: ClientBearerFlight, outcome: ClientBearerOutcome) {93
val completed =94
flight.client.lock.withLock {95
if (clientPresent(flight.outcome))96
return@withLock false97
flight.outcome = Some(outcome)98
flight.cleanup =99
when (outcome) {100
case Value(_) -> None101
case Failed(val error) -> {102
if (error is CancellationException)103
None104
else105
if (106
error is107
ResourceUseException &&108
error.primaryFailure is109
CancellationException110
)111
Some(ClientBearerCleanup(error.cleanupFailure))112
else113
Some(ClientBearerCleanup(error))114
}115
}116
flight.entry.flight = None117
if (!flight.stopping && !flight.entry.discard) {118
when (outcome) {119
case Value(val value) -> flight.entry.value = Some(value);120
case Failed(_) -> {}121
}122
}123
if (flight.entry.discard)124
removeClientBearerEntry(flight.client, flight.entry)125
true126
}127
if (!completed)128
return129
val(hold, waiters, settled) =130
flight.client.lock.withLock {131
val saved = flight.hold132
flight.hold = None;133
flight.scope = None134
val waiting = flight.waiters.toArray();135
flight.waiters.clear()136
val stopping = flight.settled.toArray();137
flight.settled.clear();138
(saved, waiting, stopping)139
}140
if (clientPresent(hold))141
hold!!.resume(Unit)142
for (waiter in waiters)143
deliverClientBearer(flight, waiter, outcome)144
for (continuation in settled)145
continuation.tryResume(Unit)146
}