Skip to content

Commit cd44a33

Browse files
committed
backend/jwt_refresh: reject tokens based on their expiration before hitting the database
1 parent 2918484 commit cd44a33

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

backend/src/routes/auth/jwt_refresh.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ async fn validate_token(req: &HttpRequest) -> actix_web::Result<User> {
4747

4848
let (token_id, exp) = extract_token(token, &state.jwt_secret)?;
4949

50+
// Check expiration BEFORE doing any database operations
51+
if exp < Utc::now().timestamp() as usize {
52+
return Err(ErrorUnauthorized("Session expired"));
53+
}
54+
5055
let mut conn = get_connection(state)?;
5156
let refresh_token: RefreshToken = web_block_unpacked(move || {
5257
use db_connector::schema::refresh_tokens::dsl::*;
@@ -59,10 +64,8 @@ async fn validate_token(req: &HttpRequest) -> actix_web::Result<User> {
5964
})
6065
.await?;
6166

67+
// Only delete the token after we've confirmed it exists and is valid
6268
delete_refresh_token(token_id, state).await?;
63-
if exp < Utc::now().timestamp() as usize {
64-
return Err(ErrorUnauthorized("Session expired"));
65-
}
6669

6770
let mut conn = get_connection(state)?;
6871
let user: User = web_block_unpacked(move || {

0 commit comments

Comments
 (0)