feat: JWTs expire after 6 months
This commit is contained in:
parent
eb41bfb899
commit
f47388015d
1 changed files with 7 additions and 3 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use std::sync::Arc;
|
||||
use std::{sync::Arc, time::{SystemTime, UNIX_EPOCH}};
|
||||
|
||||
use axum::{extract::{FromRequestParts, Request, State}, http::{request::Parts, StatusCode}, middleware::Next, response::{IntoResponse, Response}, Json, RequestPartsExt};
|
||||
use axum_extra::{headers::{authorization::Bearer, Authorization}, TypedHeader};
|
||||
|
|
@ -9,6 +9,9 @@ use serde_json::json;
|
|||
|
||||
use crate::{entities::user, AppState, KEYS};
|
||||
|
||||
//const TOKEN_EXPIRY_TIME: u64 = 15_778_476; // 6 Months
|
||||
const TOKEN_EXPIRY_TIME: u64 = 120; // 2 minutes
|
||||
|
||||
pub async fn auth_middleware(
|
||||
_claims: Claims,
|
||||
request: Request,
|
||||
|
|
@ -43,10 +46,11 @@ pub async fn auth(State(state): State<Arc<AppState>>, Json(payload): Json<AuthPa
|
|||
Err(_) | Ok(None) => return Err(AuthError::WrongCredentials),
|
||||
Ok(Some(user)) => {
|
||||
user.verify_password(payload.password);
|
||||
let unix_timestamp = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time should go forward").as_secs();
|
||||
|
||||
let claims = Claims {
|
||||
sub: user.username,
|
||||
exp: 2000000000,
|
||||
exp: unix_timestamp + TOKEN_EXPIRY_TIME,
|
||||
user_id: user.id
|
||||
};
|
||||
let token = encode(&Header::default(), &claims, &KEYS.encoding)
|
||||
|
|
@ -139,7 +143,7 @@ impl Keys {
|
|||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Claims {
|
||||
sub: String,
|
||||
exp: usize,
|
||||
exp: u64,
|
||||
pub user_id: u32,
|
||||
}
|
||||
|
||||
|
|
|
|||
Reference in a new issue