fix(auth): Token 验证和撤销添加租户隔离
This commit is contained in:
@@ -195,7 +195,7 @@ impl AuthService {
|
|||||||
TokenService::validate_refresh_token(refresh_token_str, db, jwt.secret).await?;
|
TokenService::validate_refresh_token(refresh_token_str, db, jwt.secret).await?;
|
||||||
|
|
||||||
// Revoke the old token (rotation)
|
// Revoke the old token (rotation)
|
||||||
TokenService::revoke_token(old_token_id, db).await?;
|
TokenService::revoke_token(old_token_id, claims.sub, db).await?;
|
||||||
|
|
||||||
// Fetch fresh roles and permissions
|
// Fetch fresh roles and permissions
|
||||||
let roles: Vec<String> = TokenService::get_user_roles(claims.sub, claims.tid, db).await?;
|
let roles: Vec<String> = TokenService::get_user_roles(claims.sub, claims.tid, db).await?;
|
||||||
|
|||||||
@@ -131,6 +131,7 @@ impl TokenService {
|
|||||||
let hash = sha256_hex(token);
|
let hash = sha256_hex(token);
|
||||||
let token_row = user_token::Entity::find()
|
let token_row = user_token::Entity::find()
|
||||||
.filter(user_token::Column::TokenHash.eq(hash))
|
.filter(user_token::Column::TokenHash.eq(hash))
|
||||||
|
.filter(user_token::Column::TenantId.eq(claims.tid))
|
||||||
.filter(user_token::Column::RevokedAt.is_null())
|
.filter(user_token::Column::RevokedAt.is_null())
|
||||||
.one(db)
|
.one(db)
|
||||||
.await
|
.await
|
||||||
@@ -151,8 +152,10 @@ impl TokenService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Revoke a specific refresh token by database ID.
|
/// Revoke a specific refresh token by database ID.
|
||||||
pub async fn revoke_token(token_id: Uuid, db: &DatabaseConnection) -> AuthResult<()> {
|
/// Verifies that the token belongs to the specified user for security.
|
||||||
|
pub async fn revoke_token(token_id: Uuid, user_id: Uuid, db: &DatabaseConnection) -> AuthResult<()> {
|
||||||
let token_row = user_token::Entity::find_by_id(token_id)
|
let token_row = user_token::Entity::find_by_id(token_id)
|
||||||
|
.filter(user_token::Column::UserId.eq(user_id))
|
||||||
.one(db)
|
.one(db)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| AuthError::Validation(e.to_string()))?
|
.map_err(|e| AuthError::Validation(e.to_string()))?
|
||||||
|
|||||||
Reference in New Issue
Block a user