Origin-scoped Digest authentication for gaato/mttp_core.
moon add gaato/mttp_digest///|
/// A client that answers the Digest challenges of api.example.com.
fn api_client(user : String, password : String) -> @mttp.Client raise {
@mttp.client(
backend=@mttp_digest.DigestScheme::new(
@mttp.backend(),
user,
password,
origin="https://api.example.com",
random=secure_random,
),
)
}///|
/// A sample in [0, 1) with 32 bits from the platform's secure random source.
fn secure_random() -> Double {
guard @env.rand(4) is Some(bytes) else {
abort("no secure random source on this platform")
}
let mut value = 0UL
for byte in bytes {
value = (value << 8) | byte.to_uint64()
}
value.to_double() / 4294967296.0
}
///|
test "secure_random stays in [0, 1)" {
if @env.rand(0) is Some(_) {
let sample = secure_random()
assert_true(sample >= 0.0 && sample < 1.0)
}
}pub struct DigestScheme {
// private fields
}impl Backend for DigestSchemeasync fn send(self : DigestScheme, request : Request, options : SendOptions) -> Response raise TransportErrorfn DigestScheme::new(inner : &Backend, username : String, password : String, origin~ : String, random~ : () -> Double) -> DigestScheme raise TransportErrorasync fn DigestScheme::send(self : DigestScheme, request : Request, options : SendOptions) -> Response raise TransportError