Skip to main content

relaygate_protocol/
secret.rs

1/// Maximum UTF-8 byte length of an operation bearer token.
2pub const MAX_BEARER_TOKEN_BYTES: usize = 4096;
3
4/// Bounded operation credential carried only by `PUBLISH` and `DIAL`.
5#[derive(Clone, PartialEq, Eq)]
6pub struct BearerToken(String);
7
8impl BearerToken {
9    /// Creates a token when its UTF-8 byte length is within the wire limit.
10    ///
11    /// Returns [`ProtocolError::FieldTooLong`](crate::ProtocolError::FieldTooLong)
12    /// when `value` exceeds [`MAX_BEARER_TOKEN_BYTES`].
13    pub fn new(value: impl Into<String>) -> Result<Self, crate::ProtocolError> {
14        let value = value.into();
15        if value.len() > MAX_BEARER_TOKEN_BYTES {
16            return Err(crate::ProtocolError::FieldTooLong {
17                field: "access_token",
18                actual: value.len(),
19                maximum: MAX_BEARER_TOKEN_BYTES,
20            });
21        }
22        Ok(Self(value))
23    }
24
25    /// Returns the raw credential text for authorization or wire encoding.
26    ///
27    /// Do not log or persist the returned value.
28    #[must_use]
29    pub fn expose_secret(&self) -> &str {
30        &self.0
31    }
32}
33
34impl std::fmt::Debug for BearerToken {
35    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        formatter.write_str("BearerToken([REDACTED])")
37    }
38}