Scoop API

scoop:scoop.http.client:0.1.0

src/authentication/digest/response.scoop

Selected production source · src/authentication/digest/response.scoop

1package scoop.http.client
3import scoop.charset.Charset
4import scoop.collections.emptyList
5import scoop.crypto.digest
6import scoop.http.*
7import scoop.text.hexToByteArrayOrNone
8import scoop.text.lowercaseAscii
9import scoop.text.toHexString
11internal fun prepareClientDigestResponse(call: ClientCall, head: HttpResponseHead) {
12 val origin = call.authentication.digestUse
13 if (clientPresent(origin)) prepareClientDigestUse(call, head, origin!!)
14 val proxy = clientForwardProxyDigest(call)
15 if (clientPresent(proxy)) prepareClientDigestUse(call, head, proxy!!)
18internal fun clientForwardProxyDigest(call: ClientCall): ClientDigestUse? =
19 if (clientUsesForwardProxy(call)) call.proxyAuthentication!!.digestUse else None
21internal fun verifyClientConnectDigest(call: ClientCall, head: HttpResponseHead) {
22 val use = call.proxyAuthentication!!.digestUse ?: return
23 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)
29private 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)
35internal 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 None
38 if (!clientPresent(extra) && use.nonce.challenge.qop != Some("auth-int")) return
39 var info = use.responseInfo
40 if (clientPresent(extra)) {
41 if (!clientPresent(info)) info = extra
42 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)
53private fun parseClientDigestInfo(call: ClientCall, head: HttpResponseHead, use: ClientDigestUse,
54 fields: List<HttpField>): HttpAuthenticationInfo? {
55 val name = use.scope.infoField
56 if (fields.findAll(name.toByteArray()).size == 0L) return None
57 try { return Some(HttpAuthenticationInfo.parseFields(fields, name)) }
58 catch (error: HttpFieldParseException) { throw digestResponseError(call, head, use, Some(error)) }
61private 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 }
89private fun advanceClientDigestNonce(call: ClientCall, use: ClientDigestUse, nonce: Array<Byte>) {
90 if (clientBytesEqual(use.nonce.challenge.nonce, nonce)) return
91 call.client.lock.withLock {
92 val entry = use.session.entry
93 if (entry.discard || entry.nonce.numbers !== use.nonce.numbers) return@withLock
94 val old = entry.nonce.challenge
95 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 }
103private fun digestResponseError(call: ClientCall, head: HttpResponseHead, use: ClientDigestUse,
104 cause: Throwable? = None): HttpAuthenticationException =
105 HttpAuthenticationException(HttpAuthenticationFailure.InvalidResponse, use.scope.url(call), Some(head), cause)