scoop:scoop.http.client:0.1.0
src/authentication/digest/response.scoop
Selected production source · src/authentication/digest/response.scoop
1
package scoop.http.client3
import scoop.charset.Charset4
import scoop.collections.emptyList5
import scoop.crypto.digest6
import scoop.http.*7
import scoop.text.hexToByteArrayOrNone8
import scoop.text.lowercaseAscii9
import scoop.text.toHexString11
internal fun prepareClientDigestResponse(call: ClientCall, head: HttpResponseHead) {12
val origin = call.authentication.digestUse13
if (clientPresent(origin)) prepareClientDigestUse(call, head, origin!!)14
val proxy = clientForwardProxyDigest(call)15
if (clientPresent(proxy)) prepareClientDigestUse(call, head, proxy!!)16
}18
internal fun clientForwardProxyDigest(call: ClientCall): ClientDigestUse? =19
if (clientUsesForwardProxy(call)) call.proxyAuthentication!!.digestUse else None21
internal fun verifyClientConnectDigest(call: ClientCall, head: HttpResponseHead) {22
val use = call.proxyAuthentication!!.digestUse ?: return23
prepareClientDigestUse(call, head, use)24
if (use.nonce.challenge.qop == Some("auth-int"))25
finishClientDigestResponse(call, head, use,26
Some(digest(clientDigestAlgorithm(use.nonce.challenge.algorithm), emptyList<Byte>()).toHexString()), None)27
}29
private fun prepareClientDigestUse(call: ClientCall, head: HttpResponseHead, use: ClientDigestUse) {30
use.responseInfo = parseClientDigestInfo(call, head, use, head.fields)31
if (use.nonce.challenge.qop != Some("auth-int") && clientPresent(use.responseInfo))32
verifyClientDigestInfo(call, head, use, use.responseInfo!!, None)33
}35
internal fun finishClientDigestResponse(call: ClientCall, head: HttpResponseHead, use: ClientDigestUse, entity: String?,36
trailers: Array<HttpField>?) {37
val extra = if (clientPresent(trailers)) parseClientDigestInfo(call, head, use, trailers!!) else None38
if (!clientPresent(extra) && use.nonce.challenge.qop != Some("auth-int")) return39
var info = use.responseInfo40
if (clientPresent(extra)) {41
if (!clientPresent(info)) info = extra42
else {43
val parameters = ArrayList<HttpParameter>()44
for (parameter in info!!.parameters) parameters.add(parameter)45
for (parameter in extra!!.parameters) parameters.add(parameter)46
info = try { Some(HttpAuthenticationInfo(parameters)) }47
catch (error: IllegalArgumentException) { throw digestResponseError(call, head, use, Some(error)) }48
}49
}50
if (clientPresent(info)) verifyClientDigestInfo(call, head, use, info!!, entity)51
}53
private fun parseClientDigestInfo(call: ClientCall, head: HttpResponseHead, use: ClientDigestUse,54
fields: List<HttpField>): HttpAuthenticationInfo? {55
val name = use.scope.infoField56
if (fields.findAll(name.toByteArray()).size == 0L) return None57
try { return Some(HttpAuthenticationInfo.parseFields(fields, name)) }58
catch (error: HttpFieldParseException) { throw digestResponseError(call, head, use, Some(error)) }59
}61
private fun verifyClientDigestInfo(call: ClientCall, head: HttpResponseHead, use: ClientDigestUse,62
info: HttpAuthenticationInfo, entity: String?) {63
val qop = info.parameter("qop")64
val cnonce = info.parameter("cnonce")65
val nc = info.parameter("nc")66
val proof = info.parameter("rspauth")67
if (clientPresent(qop)) {68
if (Some(Charset.Iso8859_1.decode(qop!!.value).lowercaseAscii()) != use.nonce.challenge.qop ||69
!clientPresent(proof) || !clientPresent(cnonce) || !clientPresent(nc)) throw digestResponseError(call, head, use)70
}71
if (clientPresent(cnonce) && !clientBytesEqual(cnonce!!.value, use.nonce.cnonce.toByteArray()))72
throw digestResponseError(call, head, use)73
if (clientPresent(nc) && (nc!!.value.size != 8L ||74
Charset.Iso8859_1.decode(nc!!.value).lowercaseAscii() != use.nc)) throw digestResponseError(call, head, use)75
if (clientPresent(proof)) {76
if (clientPresent(use.nonce.challenge.qop) && (!clientPresent(cnonce) || !clientPresent(nc)))77
throw digestResponseError(call, head, use)78
val actual = Charset.Iso8859_1.decode(proof!!.value).hexToByteArrayOrNone()79
val expected = clientDigestProof(use, emptyList<Byte>(), entity).hexToByteArrayOrNone()!!80
if (!clientPresent(actual) || !clientBytesEqual(actual!!, expected)) throw digestResponseError(call, head, use)81
}82
val next = info.parameter("nextnonce")83
if (clientPresent(next)) {84
if (next!!.value.size == 0L) throw digestResponseError(call, head, use)85
advanceClientDigestNonce(call, use, next!!.value)86
}87
}89
private fun advanceClientDigestNonce(call: ClientCall, use: ClientDigestUse, nonce: Array<Byte>) {90
if (clientBytesEqual(use.nonce.challenge.nonce, nonce)) return91
call.client.lock.withLock {92
val entry = use.session.entry93
if (entry.discard || entry.nonce.numbers !== use.nonce.numbers) return@withLock94
val old = entry.nonce.challenge95
val parameters = ArrayList<HttpParameter>()96
for (parameter in old.original.parameters) if (parameter.name != "nonce" && parameter.name != "stale") parameters.add(parameter)97
parameters.add(HttpParameter("nonce", nonce, quoted = true))98
entry.nonce = ClientDigestNonce(ClientDigestChallenge(HttpAuthChallenge("Digest", parameters), old.algorithm, old.utf8,99
old.realm, nonce, old.qop, old.opaque, old.userhash, false, old.paths))100
}101
}103
private fun digestResponseError(call: ClientCall, head: HttpResponseHead, use: ClientDigestUse,104
cause: Throwable? = None): HttpAuthenticationException =105
HttpAuthenticationException(HttpAuthenticationFailure.InvalidResponse, use.scope.url(call), Some(head), cause)