Refactor token handling and introduce token revocation logic
Updated `decode_token` for stricter validation of token claims and explicit error handling. Added utilities for token revocation and verification, improving
This commit is contained in:
16
backend/app/auth/utlis.py
Normal file
16
backend/app/auth/utlis.py
Normal file
@@ -0,0 +1,16 @@
|
||||
# auth/utils.py
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from app.models.token import RevokedToken
|
||||
|
||||
|
||||
async def revoke_token(jti: str, token_type: str, user_id: str, db: AsyncSession):
|
||||
"""Revoke a token by adding its `jti` to the database."""
|
||||
revoked_token = RevokedToken(jti=jti, token_type=token_type, user_id=user_id)
|
||||
db.add(revoked_token)
|
||||
await db.commit()
|
||||
|
||||
|
||||
async def is_token_revoked(jti: str, db: AsyncSession):
|
||||
"""Check if a token with the given `jti` is revoked."""
|
||||
result = await db.get(RevokedToken, jti)
|
||||
return result is not None
|
||||
Reference in New Issue
Block a user