scoop:scoop.http.client:0.1.0
src/authentication/basic.scoop
Selected production source · src/authentication/basic.scoop
1
package scoop.http.client3
import scoop.charset.Charset4
import scoop.charset.normalizeNfc5
import scoop.http.HttpCredentials6
import scoop.http.HttpField7
import scoop.io.encoding.Base648
import scoop.text.lowercaseAscii10
internal suspend fun clientBasicField(call: ClientCall, session: ClientAuthenticationSession,11
provider: ClientBasicAuthentication): HttpField {12
var utf8 = provider.charset == HttpBasicCharset.Utf813
val challenge = session.challenge14
if (clientPresent(challenge)) {15
val charset = challenge!!.parameter("charset")16
if (clientPresent(charset) && Charset.Iso8859_1.decode(charset!!.value).lowercaseAscii() == "utf-8")17
utf8 = true18
}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
}25
}27
internal fun clientBasicCredentials(credentials: HttpPasswordCredentials, utf8: Boolean): HttpCredentials {28
val username = if (utf8) normalizeNfc(credentials.username) else credentials.username29
val password = if (utf8) normalizeNfc(credentials.password) else credentials.password30
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_139
return HttpCredentials.withToken68("Basic", Base64.Default.encode(charset.encode(username + ":" + password)))40
}