scoop:scoop.http.client:0.1.0
src/authentication/digest/challenge.scoop
Selected production source · src/authentication/digest/challenge.scoop
1
package scoop.http.client3
import scoop.charset.Charset4
import scoop.http.*5
import scoop.text.*6
import scoop.url.Url8
internal fun chooseClientDigestChallenge(call: ClientCall, head: HttpResponseHead, scope: ClientAuthenticationScope,9
authenticator: HttpAuthenticator,10
provider: ClientDigestAuthentication, challenges: List<HttpAuthChallenge>): ClientDigestChallenge? {11
for (algorithm in provider.algorithms) {12
for (challenge in challenges) {13
if (challenge.scheme != "digest") continue14
val realm = challenge.parameter("realm")15
val nonce = challenge.parameter("nonce")16
if (clientPresent(challenge.token68) || !clientPresent(realm) || !clientPresent(nonce) || nonce!!.value.size == 0L)17
throw HttpAuthenticationException(HttpAuthenticationFailure.InvalidChallenge, scope.url(call), Some(head))18
if (clientPresent(authenticator.realm) && !clientBytesEqual(authenticator.realm!!, realm!!.value)) continue19
val selected = challenge.parameter("algorithm")20
val name = if (clientPresent(selected)) Charset.Iso8859_1.decode(selected!!.value) else "MD5"21
if (name.lowercaseAscii() != clientDigestName(algorithm).lowercaseAscii()) continue22
val charset = challenge.parameter("charset")23
if (clientPresent(charset) && Charset.Iso8859_1.decode(charset!!.value).lowercaseAscii() != "utf-8") continue24
val offered = challenge.parameter("qop")25
var qop: String? = None26
if (clientPresent(offered)) {27
val values = try { parseHttpTokenFields([HttpField("qop".toByteArray(), offered!!.value)], "qop") }28
catch (error: HttpFieldParseException) {29
throw HttpAuthenticationException(HttpAuthenticationFailure.InvalidChallenge, scope.url(call), Some(head), Some(error))30
}31
var auth = false32
var integrity = false33
for (value in values) { if (value == "auth") auth = true; if (value == "auth-int") integrity = true }34
if (!auth && !integrity) continue35
qop = Some(if (integrity && (provider.preferIntegrity || !auth)) "auth-int" else "auth")36
}37
val paths = digestChallengePaths(call, head, scope, challenge)38
val opaque = challenge.parameter("opaque")39
val result = ClientDigestChallenge(challenge, algorithm, clientPresent(charset), realm!!.value, nonce!!.value,40
qop, if (clientPresent(opaque)) Some(opaque!!.value) else None,41
digestParameterTrue(challenge, "userhash"), digestParameterTrue(challenge, "stale"), paths)42
if (clientDigestCovers(scope, result, call.originalRequest)) return Some(result)43
}44
}45
return None46
}48
private fun digestParameterTrue(challenge: HttpAuthChallenge, name: String): Boolean {49
val parameter = challenge.parameter(name)50
return clientPresent(parameter) && Charset.Iso8859_1.decode(parameter!!.value).lowercaseAscii() == "true"51
}53
private fun digestChallengePaths(call: ClientCall, head: HttpResponseHead, scope: ClientAuthenticationScope,54
challenge: HttpAuthChallenge): Array<String> {55
if (scope.isProxy) return ["/"]56
val domain = challenge.parameter("domain")57
if (!clientPresent(domain) || domain!!.value.size == 0L) return ["/"]58
try {59
val paths = ArrayList<String>()60
for (item in Charset.Iso8859_1.decode(domain!!.value).split(' ')) {61
if (item.charLength == 0L) continue62
for (character in item) if (character.code < 33 || character.code > 126) throw IllegalArgumentException()63
val reference = Url.parse(item)64
if ((!clientPresent(reference.scheme) && !item.startsWith('/')) || clientPresent(reference.encodedFragment))65
throw IllegalArgumentException()66
val url = scope.url(call).resolve(reference)67
if (url.httpOrigin() == call.request.origin) paths.add(url.httpRequestTarget())68
}69
return paths.toArray()70
} catch (error: Throwable) {71
throw HttpAuthenticationException(HttpAuthenticationFailure.InvalidChallenge, scope.url(call), Some(head), Some(error))72
}73
}