Scoop API

scoop:scoop.http.client:0.1.0

src/authentication/scope.scoop

Selected production source · src/authentication/scope.scoop

1package scoop.http.client
3import scoop.http.*
4import scoop.url.Url
5import scoop.url.UrlOrigin
7internal enum ClientAuthenticationScope {
8 Origin, Proxy(HttpProxyEndpoint)
10 val isProxy: Boolean get() = when (this) { case Origin -> false; case Proxy(_) -> true }
11 val proxyUrl: Url? get() = when (this) { case Origin -> None; case Proxy(val endpoint) -> Some(endpoint.url) }
12 val challengeField: String get() = if (isProxy) "Proxy-Authenticate" else "WWW-Authenticate"
13 val infoField: String get() = if (isProxy) "Proxy-Authentication-Info" else "Authentication-Info"
15 fun url(call: ClientCall): Url = proxyUrl ?: call.originalRequest.url
16 fun origin(call: ClientCall): UrlOrigin = when (this) {
17 case Origin -> call.originalRequest.origin
18 case Proxy(val endpoint) -> endpoint.url.httpOrigin()
19 }
20 fun options(call: ClientCall): HttpAuthenticationOptions = when (this) {
21 case Origin -> call.sequence.authentication
22 case Proxy(val endpoint) -> endpoint.authentication
23 }
24 fun count(sequence: ClientRequestSequence): Long =
25 if (isProxy) sequence.proxyAuthenticationCount else sequence.authenticationCount
26 fun hasExplicitCredentials(call: ClientCall): Boolean = when (this) {
27 case Origin -> hasClientAuthorization(call.originalRequest) || call.attemptAuthorization
28 case Proxy(val endpoint) -> clientPresent(endpoint.authorization)
29 }
30 fun request(call: ClientCall, challenge: HttpAuthChallenge?): HttpAuthenticationRequest =
31 HttpAuthenticationRequest(call.originalRequest, challenge, count(call.sequence), proxyUrl)
32 fun field(credentials: HttpCredentials): HttpField {
33 val field = if (isProxy) credentials.toProxyAuthorizationField() else credentials.toAuthorizationField()
34 return HttpField(field.name, field.value, neverIndex = true)
35 }
36 fun usesConnect(call: ClientCall): Boolean = isProxy && when (call.route) {
37 case HttpTunnel(_) -> true
38 else -> false
39 }
40 fun method(call: ClientCall): Array<Byte> = if (usesConnect(call)) "CONNECT".toByteArray() else call.originalRequest.methodBytes
41 fun target(call: ClientCall): Array<Byte> =
42 if (usesConnect(call)) call.originalRequest.origin.authority(true).toByteArray() else clientRequestTargetBytes(call)
45internal class ClientCallAuthentication(val scope: ClientAuthenticationScope) {
46 var session: ClientAuthenticationSession? = None
47 var digestUse: ClientDigestUse? = None
50internal fun sameClientAuthenticationScope(first: ClientAuthenticationScope, second: ClientAuthenticationScope): Boolean = when (first) {
51 case Origin -> when (second) { case Origin -> true; else -> false }
52 case Proxy(val endpoint) -> when (second) { case Proxy(val other) -> endpoint === other; else -> false }