Scoop API

scoop:scoop.http.client:0.1.0

src/authentication/digest/state.scoop

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

1package scoop.http.client
3import scoop.crypto.secureRandomBytes
4import scoop.http.HttpAuthChallenge
5import scoop.http.HttpAuthenticationInfo
6import scoop.text.startsWith
7import scoop.text.toHexString
9internal class ClientDigestChallenge(
10 val original: HttpAuthChallenge, val algorithm: HttpDigestAlgorithm, val utf8: Boolean,
11 val realm: Array<Byte>, val nonce: Array<Byte>, val qop: String?, val opaque: Array<Byte>?,
12 val userhash: Boolean, val stale: Boolean, val paths: Array<String>
15internal class ClientDigestNumbers {
16 val cnonce: String = secureRandomBytes(16L).toHexString()
17 var count: Long = 0L
20internal class ClientDigestNonce(val challenge: ClientDigestChallenge, val numbers: ClientDigestNumbers = ClientDigestNumbers()) {
21 val cnonce: String get() = numbers.cnonce
24internal class ClientDigestEntry(val scope: ClientAuthenticationScope, val authenticator: HttpAuthenticator,
25 var nonce: ClientDigestNonce) {
26 var discard: Boolean = false
29internal class ClientDigestSecret(val username: String, val ha1: String)
31internal class ClientDigestSession(val entry: ClientDigestEntry, val nonce: ClientDigestNonce) {
32 var secret: ClientDigestSecret? = None
35internal class ClientDigestUse(
36 val session: ClientDigestSession, val uri: Array<Byte>, val ha1: String, val nc: String
37) {
38 val scope: ClientAuthenticationScope get() = session.entry.scope
39 val nonce: ClientDigestNonce = session.nonce
40 var responseInfo: HttpAuthenticationInfo? = None
43internal fun sameClientDigestKey(first: ClientDigestChallenge, second: ClientDigestChallenge): Boolean =
44 first.algorithm == second.algorithm && first.utf8 == second.utf8 &&
45 clientBytesEqual(first.realm, second.realm)
47internal fun clientDigestCovers(scope: ClientAuthenticationScope, challenge: ClientDigestChallenge, request: HttpRequest): Boolean {
48 if (scope.isProxy) return true
49 val target = request.url.httpRequestTarget()
50 for (path in challenge.paths) if (target.startsWith(path)) return true
51 return false
54internal fun cachedClientDigest(call: ClientCall, scope: ClientAuthenticationScope, authenticator: HttpAuthenticator,
55 provider: ClientDigestAuthentication): ClientAuthenticationSession? = call.client.lock.withLock {
56 for (algorithm in provider.algorithms) {
57 var index = call.client.digestEntries.size
58 while (index > 0L) {
59 index -= 1L
60 val entry = call.client.digestEntries[index]
61 if (entry.authenticator !== authenticator || !sameClientAuthenticationScope(entry.scope, scope) ||
62 entry.nonce.challenge.algorithm != algorithm || !clientDigestCovers(scope, entry.nonce.challenge, call.originalRequest)) continue
63 return@withLock Some(ClientAuthenticationSession(scope, authenticator, None, "/",
64 digest = Some(ClientDigestSession(entry, entry.nonce))))
65 }
66 }
67 None
70internal fun clearClientDigestCache(state: ClientState, authenticator: HttpAuthenticator) {
71 state.lock.withLock {
72 var index = state.digestEntries.size
73 while (index > 0L) {
74 index -= 1L
75 val entry = state.digestEntries[index]
76 if (entry.authenticator === authenticator) { entry.discard = true; state.digestEntries.removeAt(index) }
77 }
78 }