Scoop API

scoop:scoop.http.client:0.1.0

src/authentication/session.scoop

Selected production source · src/authentication/session.scoop

1package scoop.http.client
3import scoop.http.*
4import scoop.text.lastIndexOf
5import scoop.text.startsWith
6import scoop.text.substring
8internal class ClientAuthenticationSession(
9 val scope: ClientAuthenticationScope,
10 val authenticator: HttpAuthenticator,
11 val challenge: HttpAuthChallenge?,
12 val path: String,
13 val rejectedBearer: ClientBearerValue? = None,
14 var digest: ClientDigestSession? = None
15) {
16 var field: HttpField? = None
17 var bearerValue: ClientBearerValue? = None
20internal suspend fun prepareClientAuthentication(
21 call: ClientCall,
22 authentication: ClientCallAuthentication
23) {
24 val scope = authentication.scope
25 val request = call.originalRequest
26 if (scope.hasExplicitCredentials(call))
27 return
28 val sequence = call.sequence
29 var selected: ClientAuthenticationSession? = None
30 var index = sequence.authenticationSessions.size
31 while (index > 0L) {
32 index -= 1L
33 val session = sequence.authenticationSessions[index]
34 if (
35 sameClientAuthenticationScope(session.scope, scope) &&
36 session.authenticator.origin ==
37 scope.origin(call) &&
38 (scope.isProxy || clientAuthenticationPath(request).startsWith(session.path)) &&
39 (
40 !clientPresent(session.digest) ||
41 clientDigestCovers(scope, session.digest!!.nonce.challenge, request)
42 )
43 ) {
44 selected = Some(session);
45 break
46 }
47 }
48 if (!clientPresent(selected)) {
49 for (provider in scope.options(call).providers) {
50 if (provider.origin == scope.origin(call) && provider.sendWithoutChallenge) {
51 val available =
52 when (provider.provider) {
53 case Digest(val options) -> cachedClientDigest(call, scope, provider, options)
54 else -> Some(ClientAuthenticationSession(scope, provider, None, "/"))
55 }
56 if (!clientPresent(available))
57 continue
58 val session = available!!
59 sequence.authenticationSessions.add(session)
60 selected = Some(session)
61 break
62 }
63 }
64 }
65 if (!clientPresent(selected))
66 return
67 val session = selected!!
68 val field =
69 when (session.authenticator.provider) {
70 case Basic(val provider) -> session.field ?: clientBasicField(call, session, provider)
71 case Bearer(val provider) -> {
72 val value = awaitClientBearer(call, session, provider)
73 requireClientCall(call)
74 session.bearerValue = Some(value)
75 value.field
76 }
77 case Digest(val provider) -> clientDigestField(call, authentication, session, provider)
78 }
79 requireClientCall(call)
80 session.field = Some(field)
81 authentication.session = Some(session)
82 if (!scope.isProxy) {
83 val fields = ArrayList<HttpField>()
84 for (value in request.suppliedFields)
85 fields.add(value)
86 fields.add(field)
87 call.request =
88 httpRequest(request.url) {
89 this.method = request.method
90 this.fields = fields
91 this.body = request.body
92 this.options = request.options
93 }
94 }
97internal fun hasClientAuthorization(request: HttpRequest): Boolean {
98 for (field in request.suppliedFields)
99 if (field.hasName("Authorization".toByteArray()))
100 return true
101 return false
104internal fun clientAuthenticationPath(request: HttpRequest): String =
105 if (request.url.encodedPath.charLength == 0L)
106 "/"
107 else
108 request.url.encodedPath
110internal fun clientAuthenticationDirectory(request: HttpRequest): String {
111 val path = clientAuthenticationPath(request)
112 return path.substring(0L, path.lastIndexOf('/') + 1L)