Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable re-using digest auth headers by default #132

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion onvif/src/soap/auth/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Digest {
creds: Option<Credentials>,
uri: Url,
state: State,
reuse_headers: bool,
}

enum State {
Expand All @@ -31,18 +32,25 @@ enum State {
}

impl Digest {
pub fn new(uri: &Url, creds: &Option<Credentials>) -> Self {
pub fn new(uri: &Url, creds: &Option<Credentials>, reuse_headers: bool) -> Self {
Self {
creds: creds.clone(),
uri: uri.clone(),
state: State::Default,
reuse_headers,
}
}
}

impl Digest {
/// Call this when the authentication was successful.
pub fn set_success(&mut self) {
if !self.reuse_headers {
FSMaxB marked this conversation as resolved.
Show resolved Hide resolved
// Since we don't need to preserve the headers, reset all the state to default.
*self = Self::new(&self.uri, &self.creds, self.reuse_headers);
return;
}

if let State::Got401 { count, .. } = &mut self.state {
// We always store at least one request, so it's never zero.
*count = nonzero!(1_u8);
Expand Down
13 changes: 12 additions & 1 deletion onvif/src/soap/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl ClientBuilder {
credentials: None,
response_patcher: None,
auth_type: AuthType::Any,
reuse_digest_auth_headers: false,
timeout: ClientBuilder::DEFAULT_TIMEOUT,
fix_time_gap: None,
},
Expand All @@ -67,6 +68,11 @@ impl ClientBuilder {
self
}

pub fn reuse_digest_auth_headers(mut self, reuse_digest_auth_headers: bool) -> Self {
self.config.reuse_digest_auth_headers = reuse_digest_auth_headers;
self
}

pub fn timeout(mut self, timeout: Duration) -> Self {
self.config.timeout = timeout;
self
Expand All @@ -87,7 +93,11 @@ impl ClientBuilder {
.unwrap()
};

let digest = Digest::new(&self.config.uri, &self.config.credentials);
let digest = Digest::new(
&self.config.uri,
&self.config.credentials,
self.config.reuse_digest_auth_headers,
);

Client {
client,
Expand Down Expand Up @@ -121,6 +131,7 @@ struct Config {
credentials: Option<Credentials>,
response_patcher: Option<ResponsePatcher>,
auth_type: AuthType,
reuse_digest_auth_headers: bool,
timeout: Duration,
fix_time_gap: Option<chrono::Duration>,
}
Expand Down
22 changes: 21 additions & 1 deletion schema/src/tests/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
use xml::reader::XmlEvent;

pub fn assert_xml_eq(actual: &str, expected: &str) {
for (a, e) in without_whitespaces(actual).zip(without_whitespaces(expected)) {
assert_eq!(a, e);
match (a, e) {
(
Ok(XmlEvent::StartDocument {
version,
encoding,
standalone,
}),
Ok(XmlEvent::StartDocument {
version: version_expected,
encoding: encoding_expected,
standalone: standalone_expected,
}),
) => {
assert_eq!(version, version_expected);
assert_eq!(encoding.to_lowercase(), encoding_expected.to_lowercase());
assert_eq!(standalone, standalone_expected);
}
(a, e) => assert_eq!(a, e),
}
}
}

Expand Down