scoop:scoop.http.client:0.1.0
src/authentication/scope.scoop
Selected production source · src/authentication/scope.scoop
1
package scoop.http.client3
import scoop.http.*4
import scoop.url.Url5
import scoop.url.UrlOrigin7
internal 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.url16
fun origin(call: ClientCall): UrlOrigin = when (this) {17
case Origin -> call.originalRequest.origin18
case Proxy(val endpoint) -> endpoint.url.httpOrigin()19
}20
fun options(call: ClientCall): HttpAuthenticationOptions = when (this) {21
case Origin -> call.sequence.authentication22
case Proxy(val endpoint) -> endpoint.authentication23
}24
fun count(sequence: ClientRequestSequence): Long =25
if (isProxy) sequence.proxyAuthenticationCount else sequence.authenticationCount26
fun hasExplicitCredentials(call: ClientCall): Boolean = when (this) {27
case Origin -> hasClientAuthorization(call.originalRequest) || call.attemptAuthorization28
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(_) -> true38
else -> false39
}40
fun method(call: ClientCall): Array<Byte> = if (usesConnect(call)) "CONNECT".toByteArray() else call.originalRequest.methodBytes41
fun target(call: ClientCall): Array<Byte> =42
if (usesConnect(call)) call.originalRequest.origin.authority(true).toByteArray() else clientRequestTargetBytes(call)43
}45
internal class ClientCallAuthentication(val scope: ClientAuthenticationScope) {46
var session: ClientAuthenticationSession? = None47
var digestUse: ClientDigestUse? = None48
}50
internal 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 }53
}