2025-02-07 22:26:32 +07:00
|
|
|
use axum::http::StatusCode;
|
|
|
|
|
use chrono::{Duration, TimeDelta, Utc};
|
2025-02-16 01:09:55 +07:00
|
|
|
use jsonwebtoken::{
|
|
|
|
|
decode, encode, DecodingKey, EncodingKey, Header, TokenData, Validation,
|
|
|
|
|
};
|
2025-02-07 22:26:32 +07:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use std::env;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
|
pub struct Claims {
|
2025-02-16 01:09:55 +07:00
|
|
|
pub exp: usize,
|
|
|
|
|
pub iat: usize,
|
|
|
|
|
pub email: String,
|
2025-02-07 22:26:32 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn encode_access_token(email: &str) -> Result<String, StatusCode> {
|
2025-02-16 01:09:55 +07:00
|
|
|
let secret: String = env::var("ACCESS_TOKEN_SECRET")
|
|
|
|
|
.unwrap_or("this-is-secret".to_string())
|
|
|
|
|
.to_string();
|
|
|
|
|
let now = Utc::now();
|
|
|
|
|
let expire: TimeDelta = Duration::minutes(15);
|
|
|
|
|
let exp: usize = (now + expire).timestamp() as usize;
|
|
|
|
|
let iat: usize = now.timestamp() as usize;
|
|
|
|
|
let claim = Claims {
|
|
|
|
|
iat,
|
|
|
|
|
exp,
|
|
|
|
|
email: email.to_string(),
|
|
|
|
|
};
|
|
|
|
|
encode(
|
|
|
|
|
&Header::default(),
|
|
|
|
|
&claim,
|
|
|
|
|
&EncodingKey::from_secret(secret.as_ref()),
|
|
|
|
|
)
|
|
|
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
2025-02-07 22:26:32 +07:00
|
|
|
}
|
|
|
|
|
|
2025-02-16 01:09:55 +07:00
|
|
|
pub fn decode_access_token(
|
|
|
|
|
jwt_token: &str,
|
|
|
|
|
) -> Result<TokenData<Claims>, StatusCode> {
|
|
|
|
|
let secret = env::var("ACCESS_TOKEN_SECRET")
|
|
|
|
|
.unwrap_or("this-is-secret".to_string())
|
|
|
|
|
.to_string();
|
|
|
|
|
let result: Result<TokenData<Claims>, StatusCode> = decode(
|
|
|
|
|
&jwt_token,
|
|
|
|
|
&DecodingKey::from_secret(secret.as_ref()),
|
|
|
|
|
&Validation::default(),
|
|
|
|
|
)
|
|
|
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
|
result
|
2025-02-07 22:26:32 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn encode_refresh_token(email: &str) -> Result<String, StatusCode> {
|
2025-02-16 01:09:55 +07:00
|
|
|
let secret: String = env::var("REFRESH_TOKEN_SECRET")
|
|
|
|
|
.unwrap_or("this-is-secret".to_string())
|
|
|
|
|
.to_string();
|
|
|
|
|
let now = Utc::now();
|
|
|
|
|
let expire: TimeDelta = Duration::days(1);
|
|
|
|
|
let exp: usize = (now + expire).timestamp() as usize;
|
|
|
|
|
let iat: usize = now.timestamp() as usize;
|
|
|
|
|
let claim = Claims {
|
|
|
|
|
iat,
|
|
|
|
|
exp,
|
|
|
|
|
email: email.to_string(),
|
|
|
|
|
};
|
|
|
|
|
encode(
|
|
|
|
|
&Header::default(),
|
|
|
|
|
&claim,
|
|
|
|
|
&EncodingKey::from_secret(secret.as_ref()),
|
|
|
|
|
)
|
|
|
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
2025-02-07 22:26:32 +07:00
|
|
|
}
|
|
|
|
|
|
2025-02-16 01:09:55 +07:00
|
|
|
pub fn decode_refresh_token(
|
|
|
|
|
jwt_token: &str,
|
|
|
|
|
) -> Result<TokenData<Claims>, StatusCode> {
|
|
|
|
|
let secret = env::var("REFRESH_TOKEN_SECRET")
|
|
|
|
|
.unwrap_or("this-is-secret".to_string())
|
|
|
|
|
.to_string();
|
|
|
|
|
let result: Result<TokenData<Claims>, StatusCode> = decode(
|
|
|
|
|
&jwt_token,
|
|
|
|
|
&DecodingKey::from_secret(secret.as_ref()),
|
|
|
|
|
&Validation::default(),
|
|
|
|
|
)
|
|
|
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
|
result
|
2025-02-07 22:26:32 +07:00
|
|
|
}
|