diff options
author | metamuffin <metamuffin@disroot.org> | 2024-11-19 02:08:52 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-11-19 02:08:52 +0100 |
commit | cbc111f90b5facc1f2a9dd79ced216279d6260af (patch) | |
tree | fa5a1d2d67874413d8e66673825c6789e8cc0945 /sip/src/transaction/auth.rs | |
parent | 2d9a31244eab6d3a9871369d3148de253e902d36 (diff) | |
download | sip-rs-cbc111f90b5facc1f2a9dd79ced216279d6260af.tar sip-rs-cbc111f90b5facc1f2a9dd79ced216279d6260af.tar.bz2 sip-rs-cbc111f90b5facc1f2a9dd79ced216279d6260af.tar.zst |
move files + rtp parser
Diffstat (limited to 'sip/src/transaction/auth.rs')
-rw-r--r-- | sip/src/transaction/auth.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/sip/src/transaction/auth.rs b/sip/src/transaction/auth.rs new file mode 100644 index 0000000..4c46f9b --- /dev/null +++ b/sip/src/transaction/auth.rs @@ -0,0 +1,51 @@ +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<Authorization> { + let challenge = failed_response.headers.get_res::<WWWAuthenticate>()?; + + 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; +} |