Skip to main content

nextcloud_auth/
anonymous.rs

1use nextcloud_sdk_core::{AuthProvider, Headers};
2
3/// Applies no credentials at all.
4///
5/// Used for public endpoints such as shared links and the
6/// capabilities/login-flow endpoints, which must not carry credentials even
7/// when the client holds them. Nextcloud's OCS API requires the
8/// `OCS-APIRequest: true` marker header, so it is set here to keep every
9/// unauthenticated OCS call valid.
10#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
11pub struct Anonymous;
12
13impl Anonymous {
14    /// Builds the provider.
15    pub const fn new() -> Self {
16        Self
17    }
18}
19
20impl AuthProvider for Anonymous {
21    fn authenticate(&self, headers: &mut Headers) {
22        headers.remove("Authorization");
23        headers.set("OCS-APIRequest", "true");
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use nextcloud_sdk_core::{AuthProvider, Header, Headers};
30
31    use super::Anonymous;
32
33    #[test]
34    fn authenticate_removes_the_authorization_header() {
35        let mut headers = Headers::new().with(Header::new("authorization", "Basic c2VjcmV0"));
36        Anonymous.authenticate(&mut headers);
37        assert_eq!(headers.get("Authorization"), None);
38    }
39
40    #[test]
41    fn authenticate_sets_the_ocs_marker_header() {
42        let mut headers = Headers::new();
43        Anonymous.authenticate(&mut headers);
44        assert_eq!(headers.get("OCS-APIRequest"), Some("true"));
45    }
46
47    #[test]
48    fn authenticate_drops_credentials_without_touching_other_headers() {
49        let mut headers = Headers::new()
50            .with(Header::new("Authorization", "Bearer token"))
51            .with(Header::new("Accept", "application/json"));
52        Anonymous.authenticate(&mut headers);
53        assert_eq!(headers.get("Authorization"), None);
54        assert_eq!(headers.get("Accept"), Some("application/json"));
55        assert_eq!(headers.len(), 2);
56    }
57}