use std::sync::atomic::Ordering;
use crate::error::{Aria2Error, Result};
use super::{AuthChallenge, DigestAlgorithm, DigestAuthProvider};
impl DigestAuthProvider {
pub fn compute_ha1(&self, realm: &str) -> String {
let input = format!(
"{}:{}:{}",
self.username.expose_secret(),
realm,
self.password.expose_secret()
);
self.hash(&input)
}
pub fn compute_ha2(
&self,
method: &str,
uri: &str,
qop: Option<&str>,
entity_body: Option<&[u8]>,
) -> String {
let input = if qop == Some("auth-int") {
let body_hash = match entity_body {
Some(body) => self.hash_from_bytes(body),
None => self.hash(""),
};
format!("{}:{}:{}", method, uri, body_hash)
} else {
format!("{}:{}", method, uri)
};
self.hash(&input)
}
pub fn compute_response(
&self,
ha1: &str,
nonce: &str,
nonce_count: &str,
cnonce: &str,
qop: Option<&str>,
ha2: &str,
) -> String {
let kd_input = match qop {
Some(q) => format!("{}:{}:{}:{}:{}", nonce, nonce_count, cnonce, q, ha2),
None => format!("{}:{}", nonce, ha2),
};
self.hash_kd(ha1, &kd_input)
}
pub fn build_authorization_header_with_method(
&self,
challenge: &AuthChallenge,
method: &str,
uri: &str,
entity_body: Option<&[u8]>,
) -> Result<String> {
let nonce = challenge
.nonce
.as_deref()
.ok_or_else(|| Aria2Error::Parse("Missing nonce in Digest challenge".to_string()))?;
let ha1 = self.compute_ha1(&challenge.realm);
let cnonce = format!("{:016x}", rand::random::<u64>());
let nc_raw = self.nc_count.fetch_add(1, Ordering::SeqCst);
let nc = format!("{:08x}", nc_raw);
let qop = challenge.qop.as_deref();
let ha2 = self.compute_ha2(method, uri, qop, entity_body);
let response = self.compute_response(&ha1, nonce, &nc, &cnonce, qop, &ha2);
let mut parts = vec![
format!("username=\"{}\"", self.username.expose_secret()),
format!("realm=\"{}\"", challenge.realm),
format!("nonce=\"{}\"", nonce),
format!("uri=\"{}\"", uri),
format!("algorithm={}", self.algorithm),
format!("response=\"{}\"", response),
];
if let Some(q) = qop {
parts.push(format!("qop={}", q));
parts.push(format!("nc={}", nc));
parts.push(format!("cnonce=\"{}\"", cnonce));
}
if let Some(ref opaque) = challenge.opaque {
parts.push(format!("opaque=\"{}\"", opaque));
}
Ok(format!("Digest {}", parts.join(", ")))
}
pub(crate) fn hash(&self, input: &str) -> String {
self.hash_from_bytes(input.as_bytes())
}
pub(crate) fn hash_from_bytes(&self, input: &[u8]) -> String {
match self.algorithm {
DigestAlgorithm::Md5 => {
use md5::Digest;
let mut hasher = md5::Md5::new();
hasher.update(input);
let digest = hasher.finalize();
format!("{:x}", digest)
}
DigestAlgorithm::Sha256 => {
use sha2::{Digest as _, Sha256};
let mut hasher = Sha256::new();
hasher.update(input);
let result = hasher.finalize();
hex::encode(result)
}
DigestAlgorithm::Sha512_256 => {
use sha2::{Digest as _, Sha512_256};
let mut hasher = Sha512_256::new();
hasher.update(input);
let result = hasher.finalize();
hex::encode(result)
}
}
}
pub(crate) fn hash_kd(&self, secret: &str, data: &str) -> String {
let kd_input = format!("{}:{}", secret, data);
self.hash(&kd_input)
}
}