Scoop API

scoop:scoop.http.client:0.1.0

src/authentication/bearer/owner.scoop

Selected production source · src/authentication/bearer/owner.scoop

1package scoop.http.client
3import scoop.http.HttpCredentials
5internal 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 }
25private suspend fun runClientBearerFlight(flight: ClientBearerFlight): ClientBearerValue =
26 coroutineScope<ClientBearerValue> {
27 val scope = this
29 val stopped =
30 flight.client.lock.withLock {
31 flight.scope = Some(scope);
32 flight.stopping
33 }
34 if (stopped)
35 scope.job.cancel()
36 scope.job.ensureActive()
37 val provider = flight.entry.provider
38 val previous = flight.previous
39 val tokens =
40 if (!clientPresent(previous))
41 provider.loadTokens(flight.request)
42 else {
43 val request = flight.request
44 val refreshed =
45 provider.refreshTokens(
46 HttpBearerRefreshRequest(
47 request.request,
48 request.challenge!!,
49 request.attempt,
50 previous!!.tokens,
51 request.proxyUrl
52 )
53 )
54 scope.job.ensureActive()
55 if (
56 !clientPresent(refreshed) ||
57 refreshed!!.accessToken ==
58 previous!!.tokens.accessToken
59 )
60 throw HttpAuthenticationException(
61 HttpAuthenticationFailure.Rejected,
62 request.proxyUrl ?: request.request.url
63 )
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 }
81private class ClientBearerCompletion(val flight: ClientBearerFlight): Continuation<
82 ClientBearerValue
83> {
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 }
92internal fun finishClientBearerFlight(flight: ClientBearerFlight, outcome: ClientBearerOutcome) {
93 val completed =
94 flight.client.lock.withLock {
95 if (clientPresent(flight.outcome))
96 return@withLock false
97 flight.outcome = Some(outcome)
98 flight.cleanup =
99 when (outcome) {
100 case Value(_) -> None
101 case Failed(val error) -> {
102 if (error is CancellationException)
103 None
104 else
105 if (
106 error is
107 ResourceUseException &&
108 error.primaryFailure is
109 CancellationException
110 )
111 Some(ClientBearerCleanup(error.cleanupFailure))
112 else
113 Some(ClientBearerCleanup(error))
114 }
115 }
116 flight.entry.flight = None
117 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 true
126 }
127 if (!completed)
128 return
129 val(hold, waiters, settled) =
130 flight.client.lock.withLock {
131 val saved = flight.hold
132 flight.hold = None;
133 flight.scope = None
134 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)