Skip to main content

matomo/
auth.rs

1use secrecy::SecretString;
2
3/// How the client authenticates against the Matomo instance.
4///
5/// `token` works on all Matomo 5.x; `bearer` requires Matomo 5.4+.
6#[derive(Clone)]
7pub enum Auth {
8    /// Sent as the `token_auth` form field in the POST body.
9    Token(SecretString),
10    /// Sent as an `Authorization: Bearer` header (Matomo 5.4+).
11    Bearer(SecretString),
12}
13
14impl Auth {
15    pub fn token(token: impl Into<String>) -> Self {
16        Auth::Token(SecretString::from(token.into()))
17    }
18
19    pub fn bearer(token: impl Into<String>) -> Self {
20        Auth::Bearer(SecretString::from(token.into()))
21    }
22}
23
24impl std::fmt::Debug for Auth {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            Auth::Token(_) => f.write_str("Auth::Token(<redacted>)"),
28            Auth::Bearer(_) => f.write_str("Auth::Bearer(<redacted>)"),
29        }
30    }
31}