git_next_core/config/webhook/
auth.rs

1//
2use crate::newtype;
3
4newtype!(
5    WebhookAuth,
6    ulid::Ulid,
7    derive_more::Display,
8    r#"The unique token authorisation for the webhook.
9
10Each monitored repository has it's own unique token, and it is different each time `git-next` runs."#
11);
12impl WebhookAuth {
13    /// Parses the authorisation string as a `Ulid` to create a `WebhookAuth`.
14    ///
15    /// # Errors
16    ///
17    /// Will return an `Err` if the authorisation string is not a valid `Ulid`.
18    pub fn try_new(authorisation: &str) -> Result<Self, ulid::DecodeError> {
19        use std::str::FromStr as _;
20        let id = ulid::Ulid::from_str(authorisation)?;
21        tracing::info!("Parse auth token: {}", id);
22        Ok(Self(id))
23    }
24
25    #[must_use]
26    pub fn generate() -> Self {
27        Self(ulid::Ulid::new())
28    }
29
30    #[must_use]
31    pub fn header_value(&self) -> String {
32        format!("Basic {self}")
33    }
34
35    #[cfg(test)]
36    pub(crate) const fn to_bytes(&self) -> [u8; 16] {
37        self.0.to_bytes()
38    }
39}