systemprompt_identifiers/auth/
jwt_token.rs

1//! JWT token identifier type.
2
3use crate::{DbValue, ToDbValue};
4use serde::{Deserialize, Serialize};
5use std::fmt;
6
7#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
8#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
9#[cfg_attr(feature = "sqlx", sqlx(transparent))]
10#[serde(transparent)]
11pub struct JwtToken(String);
12
13impl JwtToken {
14    pub fn new(token: impl Into<String>) -> Self {
15        Self(token.into())
16    }
17
18    pub fn as_str(&self) -> &str {
19        &self.0
20    }
21
22    #[must_use]
23    pub fn redacted(&self) -> String {
24        let len = self.0.len();
25        if len <= 16 {
26            "*".repeat(len.min(8))
27        } else {
28            format!("{}...{}", &self.0[..8], &self.0[len - 4..])
29        }
30    }
31}
32
33impl fmt::Display for JwtToken {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        write!(f, "{}", self.redacted())
36    }
37}
38
39impl From<String> for JwtToken {
40    fn from(s: String) -> Self {
41        Self(s)
42    }
43}
44
45impl From<&str> for JwtToken {
46    fn from(s: &str) -> Self {
47        Self(s.to_string())
48    }
49}
50
51impl AsRef<str> for JwtToken {
52    fn as_ref(&self) -> &str {
53        &self.0
54    }
55}
56
57impl ToDbValue for JwtToken {
58    fn to_db_value(&self) -> DbValue {
59        DbValue::String(self.0.clone())
60    }
61}
62
63impl ToDbValue for &JwtToken {
64    fn to_db_value(&self) -> DbValue {
65        DbValue::String(self.0.clone())
66    }
67}