Scoop API

scoop:scoop.http.client:0.1.0

src/authentication/basic.scoop

Selected production source · src/authentication/basic.scoop

1package scoop.http.client
3import scoop.charset.Charset
4import scoop.charset.normalizeNfc
5import scoop.http.HttpCredentials
6import scoop.http.HttpField
7import scoop.io.encoding.Base64
8import scoop.text.lowercaseAscii
10internal suspend fun clientBasicField(call: ClientCall, session: ClientAuthenticationSession,
11 provider: ClientBasicAuthentication): HttpField {
12 var utf8 = provider.charset == HttpBasicCharset.Utf8
13 val challenge = session.challenge
14 if (clientPresent(challenge)) {
15 val charset = challenge!!.parameter("charset")
16 if (clientPresent(charset) && Charset.Iso8859_1.decode(charset!!.value).lowercaseAscii() == "utf-8")
17 utf8 = true
18 }
19 val credentials = provider.credentials(session.scope.request(call, challenge))
20 requireClientCall(call)
21 try { return session.scope.field(clientBasicCredentials(credentials, utf8)) }
22 catch (_: IllegalArgumentException) {
23 throw HttpAuthenticationException(HttpAuthenticationFailure.InvalidCredentials, session.scope.url(call))
24 }
27internal fun clientBasicCredentials(credentials: HttpPasswordCredentials, utf8: Boolean): HttpCredentials {
28 val username = if (utf8) normalizeNfc(credentials.username) else credentials.username
29 val password = if (utf8) normalizeNfc(credentials.password) else credentials.password
30 for (character in username) {
31 if (character.code < 32 || character.code == 127 || character == ':')
32 throw IllegalArgumentException(Some("invalid Basic username"))
33 }
34 for (character in password) {
35 if (character.code < 32 || character.code == 127)
36 throw IllegalArgumentException(Some("invalid Basic password"))
37 }
38 val charset = if (utf8) Charset.Utf8 else Charset.Iso8859_1
39 return HttpCredentials.withToken68("Basic", Base64.Default.encode(charset.encode(username + ":" + password)))