scoop:scoop.http.client:0.1.0
src/authentication/plan.scoop
Selected production source · src/authentication/plan.scoop
1
package scoop.http.client3
import scoop.http.*5
internal fun planClientAuthentication(call: ClientCall, head: HttpResponseHead,6
authentication: ClientCallAuthentication): ClientAuthenticationSession? {7
val scope = authentication.scope8
val options = scope.options(call)9
if (head.status != (if (scope.isProxy) 407U else 401U) || options.providers.size == 0L ||10
options.maxAttempts == 0L || scope.hasExplicitCredentials(call)) return None11
var relevant = false12
for (provider in options.providers) if (provider.origin == scope.origin(call)) relevant = true13
if (!relevant) return None14
val challenges = try { HttpAuthChallenge.parseFields(head.fields, scope.challengeField) }15
catch (error: HttpFieldParseException) {16
throw HttpAuthenticationException(HttpAuthenticationFailure.InvalidChallenge, scope.url(call), Some(head), Some(error))17
}18
for (authenticator in options.providers) {19
if (authenticator.origin != scope.origin(call)) continue20
val session = when (authenticator.provider) {21
case Digest(val provider) -> planClientDigest(call, head, authentication, authenticator, provider, challenges)22
else -> {23
val selected = planClientSimpleAuthentication(call, head, authentication, authenticator, challenges)24
if (clientPresent(selected)) requireClientAuthenticationFollowUp(call, head, scope)25
selected26
}27
}28
if (clientPresent(session)) return session29
}30
return None31
}33
internal fun requireClientAuthenticationFollowUp(call: ClientCall, head: HttpResponseHead, scope: ClientAuthenticationScope) {34
if (scope.count(call.sequence) >= scope.options(call).maxAttempts)35
throw HttpAuthenticationException(HttpAuthenticationFailure.LimitExceeded, scope.url(call), Some(head))36
if (!call.originalRequest.body.isReplayable && call.lock.withLock { call.bodyStarted })37
throw HttpAuthenticationException(HttpAuthenticationFailure.BodyNotReplayable, scope.url(call), Some(head))38
}40
private fun planClientSimpleAuthentication(call: ClientCall, head: HttpResponseHead, authentication: ClientCallAuthentication,41
provider: HttpAuthenticator, challenges: List<HttpAuthChallenge>): ClientAuthenticationSession? {42
val scope = authentication.scope43
val basic = when (provider.provider) { case Basic(_) -> true; else -> false }44
for (challenge in challenges) {45
if (challenge.scheme != (if (basic) "basic" else "bearer")) continue46
val realm = challenge.parameter("realm")47
if (basic && !clientPresent(realm))48
throw HttpAuthenticationException(HttpAuthenticationFailure.InvalidChallenge, scope.url(call), Some(head))49
if (clientPresent(provider.realm) && (!clientPresent(realm) ||50
!clientBytesEqual(provider.realm!!, realm!!.value))) continue51
val previous = authentication.session52
var rejectedBearer: ClientBearerValue? = None53
if (basic) {54
if (clientPresent(previous) && previous!!.authenticator === provider && clientPresent(previous!!.challenge)) {55
val previousRealm = previous!!.challenge!!.parameter("realm")!!56
if (clientBytesEqual(previousRealm.value, realm!!.value))57
throw HttpAuthenticationException(HttpAuthenticationFailure.Rejected, scope.url(call), Some(head))58
}59
} else {60
if (clientPresent(challenge.token68))61
throw HttpAuthenticationException(HttpAuthenticationFailure.InvalidChallenge, scope.url(call), Some(head))62
val error = challenge.parameter("error")63
if (clientPresent(error) && !clientBytesEqual(error!!.value, "invalid_token".toByteArray())) continue64
if (clientPresent(previous) && previous!!.authenticator === provider) rejectedBearer = previous!!.bearerValue65
}66
return Some(ClientAuthenticationSession(scope, provider, Some(challenge),67
if (basic && !scope.isProxy) clientAuthenticationDirectory(call.originalRequest) else "/", rejectedBearer))68
}69
return None70
}