use nextcloud_sdk_core::{AuthProvider, Headers};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Anonymous;
impl Anonymous {
pub const fn new() -> Self {
Self
}
}
impl AuthProvider for Anonymous {
fn authenticate(&self, headers: &mut Headers) {
tracing::trace!("Applying Anonymous authentication marker");
headers.remove("Authorization");
headers.set("OCS-APIRequest", "true");
}
}
#[cfg(test)]
mod tests {
use nextcloud_sdk_core::{AuthProvider, Header, Headers};
use super::Anonymous;
#[test]
fn authenticate_removes_the_authorization_header() {
let mut headers = Headers::new().with(Header::new("authorization", "Basic c2VjcmV0"));
Anonymous.authenticate(&mut headers);
assert_eq!(headers.get("Authorization"), None);
}
#[test]
fn authenticate_sets_the_ocs_marker_header() {
let mut headers = Headers::new();
Anonymous.authenticate(&mut headers);
assert_eq!(headers.get("OCS-APIRequest"), Some("true"));
}
#[test]
fn authenticate_drops_credentials_without_touching_other_headers() {
let mut headers = Headers::new()
.with(Header::new("Authorization", "Bearer token"))
.with(Header::new("Accept", "application/json"));
Anonymous.authenticate(&mut headers);
assert_eq!(headers.get("Authorization"), None);
assert_eq!(headers.get("Accept"), Some("application/json"));
assert_eq!(headers.len(), 2);
}
}