1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
use crate::errors::AppError;
use chrono::{Duration, Local};
use jsonwebtoken::{encode, DecodingKey, EncodingKey, Header, Validation};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
struct Keys {
encoding: EncodingKey,
decoding: DecodingKey,
}
#[derive(Serialize, Deserialize)]
pub struct Claims {
user_id: i32,
exp: usize,
}
#[derive(Serialize)]
pub struct AuthBody {
access_token: String,
token_type: String,
}
static KEYS: Lazy<Keys> = Lazy::new(|| {
let secret = std::env::var("JWT_SECRET").expect("JWT_SECRET must be set");
Keys::new(secret.as_bytes())
});
impl Keys {
fn new(secret: &[u8]) -> Self {
Self {
encoding: EncodingKey::from_secret(secret),
decoding: DecodingKey::from_secret(secret),
}
}
}
impl Claims {
pub fn new(user_id: i32) -> Self {
let expiration = Local::now() + Duration::days(2);
Self {
user_id,
exp: expiration.timestamp() as usize,
}
}
pub fn get_token(&self) -> Result<String, AppError> {
let token = encode(&Header::default(), &self, &KEYS.encoding)
.map_err(|_| AppError::TokenCreation)?;
Ok(token)
}
}
impl AuthBody {
pub fn new(access_token: String) -> Self {
Self {
access_token,
token_type: "Bearer".to_string(),
}
}
}
|