Skip to main content

irtt_client/
auth.rs

1use std::{fmt, sync::Arc};
2
3/// Shared HMAC key bytes. Debug output never includes the bytes or a fingerprint.
4///
5/// An empty key is still an explicitly configured HMAC key.
6#[derive(Clone, PartialEq, Eq)]
7pub struct HmacKey(Arc<[u8]>);
8
9impl HmacKey {
10    /// Store key bytes without interpreting textual key syntax.
11    pub fn new(bytes: impl Into<Arc<[u8]>>) -> Self {
12        Self(bytes.into())
13    }
14
15    /// Explicitly borrow the key material for authentication.
16    pub fn as_bytes(&self) -> &[u8] {
17        &self.0
18    }
19}
20
21impl fmt::Debug for HmacKey {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        f.write_str("HmacKey([REDACTED])")
24    }
25}
26
27/// Concrete authentication for a low-level client session.
28#[derive(Clone, Debug, Default, PartialEq, Eq)]
29pub enum Authentication {
30    /// Send and accept packets without HMAC authentication.
31    #[default]
32    Unauthenticated,
33    /// Authenticate packets with this key, including when it is empty.
34    Hmac(HmacKey),
35}
36
37impl Authentication {
38    pub(crate) fn hmac_key(&self) -> Option<&[u8]> {
39        match self {
40            Self::Unauthenticated => None,
41            Self::Hmac(key) => Some(key.as_bytes()),
42        }
43    }
44}