51 lines
1.1 KiB
Rust
51 lines
1.1 KiB
Rust
|
use std::str::FromStr as _;
|
||
|
|
||
|
#[derive(Clone, Debug, PartialEq, Eq, derive_more::Deref, derive_more::Display)]
|
||
|
pub struct WebhookAuth(ulid::Ulid);
|
||
|
impl WebhookAuth {
|
||
|
pub fn new(authorisation: &str) -> Result<Self, ulid::DecodeError> {
|
||
|
let id = ulid::Ulid::from_str(authorisation)?;
|
||
|
tracing::info!("Parse auth token: {}", id);
|
||
|
Ok(Self(id))
|
||
|
}
|
||
|
|
||
|
pub fn generate() -> Self {
|
||
|
Self(ulid::Ulid::new())
|
||
|
}
|
||
|
|
||
|
pub fn header_value(&self) -> String {
|
||
|
format!("Basic {}", self)
|
||
|
}
|
||
|
|
||
|
pub const fn to_bytes(&self) -> [u8; 16] {
|
||
|
self.0.to_bytes()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use crate::WebhookAuth;
|
||
|
|
||
|
#[test]
|
||
|
fn bytes() -> Result<(), Box<dyn std::error::Error>> {
|
||
|
let ulid = ulid::Ulid::new();
|
||
|
|
||
|
let wa = WebhookAuth::new(ulid.to_string().as_str())?;
|
||
|
|
||
|
assert_eq!(ulid.to_bytes(), wa.to_bytes());
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn string() -> Result<(), Box<dyn std::error::Error>> {
|
||
|
let ulid = ulid::Ulid::new();
|
||
|
|
||
|
let wa = WebhookAuth::new(ulid.to_string().as_str())?;
|
||
|
|
||
|
assert_eq!(ulid.to_string(), wa.to_string());
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|