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        tracing::trace!("Applying Anonymous authentication marker");
23        headers.remove("Authorization");
24        headers.set("OCS-APIRequest", "true");
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use nextcloud_sdk_core::{AuthProvider, Header, Headers};
31
32    use super::Anonymous;
33
34    #[test]
35    fn authenticate_removes_the_authorization_header() {
36        let mut headers = Headers::new().with(Header::new("authorization", "Basic c2VjcmV0"));
37        Anonymous.authenticate(&mut headers);
38        assert_eq!(headers.get("Authorization"), None);
39    }
40
41    #[test]
42    fn authenticate_sets_the_ocs_marker_header() {
43        let mut headers = Headers::new();
44        Anonymous.authenticate(&mut headers);
45        assert_eq!(headers.get("OCS-APIRequest"), Some("true"));
46    }
47
48    #[test]
49    fn authenticate_drops_credentials_without_touching_other_headers() {
50        let mut headers = Headers::new()
51            .with(Header::new("Authorization", "Bearer token"))
52            .with(Header::new("Accept", "application/json"));
53        Anonymous.authenticate(&mut headers);
54        assert_eq!(headers.get("Authorization"), None);
55        assert_eq!(headers.get("Accept"), Some("application/json"));
56        assert_eq!(headers.len(), 2);
57    }
58}