1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
use crate::newtype;

newtype!(
    WebhookAuth,
    ulid::Ulid,
    derive_more::Display,
    r#"The unique token authorisation for the webhook.

Each monitored repository has it's own unique token, and it is different each time `git-next` runs."#
);
impl WebhookAuth {
    /// Parses the authorisation string as a `Ulid` to create a `WebhookAuth`.
    ///
    /// # Errors
    ///
    /// Will return an `Err` if the authorisation string is not a valid `Ulid`.
    pub fn try_new(authorisation: &str) -> Result<Self, ulid::DecodeError> {
        use std::str::FromStr as _;
        let id = ulid::Ulid::from_str(authorisation)?;
        tracing::info!("Parse auth token: {}", id);
        Ok(Self(id))
    }

    #[must_use]
    pub fn generate() -> Self {
        Self(ulid::Ulid::new())
    }

    #[must_use]
    pub fn header_value(&self) -> String {
        format!("Basic {self}")
    }

    #[cfg(test)]
    pub(crate) const fn to_bytes(&self) -> [u8; 16] {
        self.0.to_bytes()
    }
}