Scoop API

scoop:scoop.http.client:0.1.0

src/authentication/plan.scoop

Selected production source · src/authentication/plan.scoop

1package scoop.http.client
3import scoop.http.*
5internal fun planClientAuthentication(call: ClientCall, head: HttpResponseHead,
6 authentication: ClientCallAuthentication): ClientAuthenticationSession? {
7 val scope = authentication.scope
8 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 None
11 var relevant = false
12 for (provider in options.providers) if (provider.origin == scope.origin(call)) relevant = true
13 if (!relevant) return None
14 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)) continue
20 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 selected
26 }
27 }
28 if (clientPresent(session)) return session
29 }
30 return None
33internal 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))
40private fun planClientSimpleAuthentication(call: ClientCall, head: HttpResponseHead, authentication: ClientCallAuthentication,
41 provider: HttpAuthenticator, challenges: List<HttpAuthChallenge>): ClientAuthenticationSession? {
42 val scope = authentication.scope
43 val basic = when (provider.provider) { case Basic(_) -> true; else -> false }
44 for (challenge in challenges) {
45 if (challenge.scheme != (if (basic) "basic" else "bearer")) continue
46 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))) continue
51 val previous = authentication.session
52 var rejectedBearer: ClientBearerValue? = None
53 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())) continue
64 if (clientPresent(previous) && previous!!.authenticator === provider) rejectedBearer = previous!!.bearerValue
65 }
66 return Some(ClientAuthenticationSession(scope, provider, Some(challenge),
67 if (basic && !scope.isProxy) clientAuthenticationDirectory(call.originalRequest) else "/", rejectedBearer))
68 }
69 return None