scoop:scoop.http.client:0.1.0
src/authentication/digest/state.scoop
Selected production source · src/authentication/digest/state.scoop
1
package scoop.http.client3
import scoop.crypto.secureRandomBytes4
import scoop.http.HttpAuthChallenge5
import scoop.http.HttpAuthenticationInfo6
import scoop.text.startsWith7
import scoop.text.toHexString9
internal 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>13
)15
internal class ClientDigestNumbers {16
val cnonce: String = secureRandomBytes(16L).toHexString()17
var count: Long = 0L18
}20
internal class ClientDigestNonce(val challenge: ClientDigestChallenge, val numbers: ClientDigestNumbers = ClientDigestNumbers()) {21
val cnonce: String get() = numbers.cnonce22
}24
internal class ClientDigestEntry(val scope: ClientAuthenticationScope, val authenticator: HttpAuthenticator,25
var nonce: ClientDigestNonce) {26
var discard: Boolean = false27
}29
internal class ClientDigestSecret(val username: String, val ha1: String)31
internal class ClientDigestSession(val entry: ClientDigestEntry, val nonce: ClientDigestNonce) {32
var secret: ClientDigestSecret? = None33
}35
internal class ClientDigestUse(36
val session: ClientDigestSession, val uri: Array<Byte>, val ha1: String, val nc: String37
) {38
val scope: ClientAuthenticationScope get() = session.entry.scope39
val nonce: ClientDigestNonce = session.nonce40
var responseInfo: HttpAuthenticationInfo? = None41
}43
internal fun sameClientDigestKey(first: ClientDigestChallenge, second: ClientDigestChallenge): Boolean =44
first.algorithm == second.algorithm && first.utf8 == second.utf8 &&45
clientBytesEqual(first.realm, second.realm)47
internal fun clientDigestCovers(scope: ClientAuthenticationScope, challenge: ClientDigestChallenge, request: HttpRequest): Boolean {48
if (scope.isProxy) return true49
val target = request.url.httpRequestTarget()50
for (path in challenge.paths) if (target.startsWith(path)) return true51
return false52
}54
internal 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.size58
while (index > 0L) {59
index -= 1L60
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)) continue63
return@withLock Some(ClientAuthenticationSession(scope, authenticator, None, "/",64
digest = Some(ClientDigestSession(entry, entry.nonce))))65
}66
}67
None68
}70
internal fun clearClientDigestCache(state: ClientState, authenticator: HttpAuthenticator) {71
state.lock.withLock {72
var index = state.digestEntries.size73
while (index > 0L) {74
index -= 1L75
val entry = state.digestEntries[index]76
if (entry.authenticator === authenticator) { entry.discard = true; state.digestEntries.removeAt(index) }77
}78
}79
}