Skip to main content

claw_crypto_interface/jwt/
models.rs

1use chrono::{DateTime, Duration, NaiveDateTime, Utc};
2use serde::{de::DeserializeOwned, Deserialize, Serialize};
3use serde_json;
4use std::marker::PhantomData;
5
6use super::{JwtError, JwtResult};
7
8// NOTE: See https://crates.io/crates/jsonwebtoken for the accepted fields
9#[derive(Serialize, Deserialize)]
10pub struct JwtClaims<S> {
11    iat: usize,
12    exp: usize,
13    sub: String,
14    marker: PhantomData<S>,
15}
16
17impl<S> JwtClaims<S>
18where
19    S: Serialize + DeserializeOwned,
20{
21    pub fn try_new(subject: &S, options: &JwtClaimsOptions) -> JwtResult<Self> {
22        let sub = serde_json::to_string(subject).map_err(|_| JwtError::Serde)?;
23        let this = Self {
24            iat: Utc::now().timestamp() as usize,
25            exp: options.expiration.and_utc().timestamp() as usize,
26            sub,
27            marker: PhantomData,
28        };
29        Ok(this)
30    }
31
32    pub fn issued_at(&self) -> NaiveDateTime {
33        DateTime::<Utc>::from_timestamp(self.iat as i64, 0)
34            .unwrap_or_default()
35            .naive_utc()
36    }
37
38    pub fn expires_at(&self) -> NaiveDateTime {
39        DateTime::<Utc>::from_timestamp(self.exp as i64, 0)
40            .unwrap_or_default()
41            .naive_utc()
42    }
43
44    pub fn subject(&self) -> S {
45        serde_json::from_str(&self.sub).unwrap()
46    }
47}
48
49pub struct JwtClaimsOptions {
50    pub expiration: NaiveDateTime,
51}
52
53impl JwtClaimsOptions {
54    pub fn new(expiration: NaiveDateTime) -> Self {
55        Self { expiration }
56    }
57
58    pub fn with_duration(duration: Duration) -> Self {
59        let expiration = (Utc::now() + duration).naive_utc();
60        Self::new(expiration)
61    }
62}
63
64impl Default for JwtClaimsOptions {
65    fn default() -> Self {
66        Self::with_duration(Duration::days(10))
67    }
68}