use crate::encoding::{ headers::{Authorization, WWWAuthenticate}, method::Method, request::Request, response::Response, uri::Uri, }; use anyhow::Result; impl Authorization { pub fn construct( request: &Request, failed_response: &Response, username: &str, password: &str, ) -> Result { let challenge = failed_response.headers.get_res::()?; Ok(Authorization { response: response_digest( username.to_string(), challenge.realm.clone(), password.to_string(), request.method, challenge.nonce.clone(), request.uri.clone(), ), nonce: challenge.nonce, realm: challenge.realm, uri: request.uri.to_string(), username: username.to_string(), }) } } fn response_digest( username: String, realm: String, password: String, method: Method, nonce: String, uri: Uri, ) -> String { let h = |s: String| hex::encode(md5::compute(s.as_bytes()).0); let kd = |secret, data| h(format!("{secret}:{data}")); let a1 = format!("{username}:{realm}:{password}"); let a2 = format!("{method}:{uri}"); let response_digest = kd(h(a1), format!("{nonce}:{}", h(a2))); return response_digest; }