fix problems
This commit is contained in:
+27
-22
@@ -36,6 +36,18 @@ class CurrentUserService:
|
||||
return user
|
||||
|
||||
|
||||
def _token_record_create(self, jti:UUID,user_id:UUID,token:str, request:Request)->RefreshTokensCreate:
|
||||
|
||||
return RefreshTokensCreate(
|
||||
id=jti,
|
||||
user_id=user_id,
|
||||
token_hash=self.hash.token_to_hash(token),
|
||||
device_info=request.headers.get("user-agent", "unknown"),
|
||||
ip_address=request.headers.get("x-forwarded-for", "").split(",")[0].strip() or (request.client.host if request.client else "unknown"),
|
||||
expires_at=datetime.now(UTC)+timedelta(days=env_settings.REFRESH_TOKEN_EXPIRE_DAYS)
|
||||
)
|
||||
|
||||
|
||||
def get_current_user(self, token:str)->UserOut:
|
||||
|
||||
payload=self.jwt_service.jwt_decode(token)
|
||||
@@ -46,6 +58,8 @@ class CurrentUserService:
|
||||
except (ValueError, TypeError) as e:
|
||||
raise self.error.credentials_error(detail="Jwt token is incorrect") from e
|
||||
|
||||
if not (payload.get("token_type")=="access"):
|
||||
raise self.error.credentials_error(detail="Jwt token type is incorrect")
|
||||
|
||||
user=self.crud_db_actions.get_user_by_id(sub)
|
||||
if user is None:
|
||||
@@ -74,14 +88,9 @@ class CurrentUserService:
|
||||
raise self.error.credentials_error(detail="Jwt token is incorrect") from e
|
||||
|
||||
'''create new refresh token if all the checks are successful'''
|
||||
token_record=RefreshTokensCreate(
|
||||
id=jti,
|
||||
user_id=user_id,
|
||||
token_hash=self.hash.token_to_hash(token),
|
||||
device_info=request.headers.get("user-agent", "unknown"),
|
||||
ip_address=request.headers.get("x-forwarded-for", "").split(",")[0].strip() or (request.client.host if request.client else "unknown"),
|
||||
expires_at=datetime.now(UTC)+timedelta(days=env_settings.REFRESH_TOKEN_EXPIRE_DAYS)
|
||||
)
|
||||
token_record=self._token_record_create(jti=jti, user_id=user_id, token=token, request=request)
|
||||
|
||||
|
||||
self.jwt_db_actions.create_token(RefreshTokensCreate.model_dump(token_record))
|
||||
|
||||
return token
|
||||
@@ -104,12 +113,14 @@ class CurrentUserService:
|
||||
|
||||
|
||||
'''old refresh token check'''
|
||||
|
||||
if (old_refresh_token.get("token_type")=="access"):
|
||||
raise self.error.credentials_error(detail="Jwt token type is incorrect")
|
||||
|
||||
|
||||
old_record=self.jwt_db_actions.get_token_by_id(old_jti)
|
||||
if old_record is None:
|
||||
raise self.error.not_found_error(detail="Token not found")
|
||||
if old_record.is_revoked:
|
||||
self.jwt_db_actions.revoke_all(old_record.user_id)
|
||||
raise self.error.credentials_error(detail="Reuse token detected")
|
||||
|
||||
|
||||
'''sqlite constraints about timezone'''
|
||||
@@ -133,19 +144,12 @@ class CurrentUserService:
|
||||
|
||||
try:
|
||||
new_jti=UUID(new_jti)
|
||||
except (ValueError, TypeError):
|
||||
raise self.error.credentials_error(detail="Jwt token is incorrect")
|
||||
except (ValueError, TypeError) as e:
|
||||
raise self.error.credentials_error(detail="Jwt token is incorrect") from e
|
||||
|
||||
|
||||
'''create database record with the new token'''
|
||||
new_token_record=RefreshTokensCreate(
|
||||
id=new_jti,
|
||||
user_id=sub,
|
||||
token_hash=self.hash.token_to_hash(new_refresh_token),
|
||||
device_info=request.headers.get("user-agent", "unknown"),
|
||||
ip_address=request.headers.get("x-forwarded-for", "").split(",")[0].strip() or (request.client.host if request.client else "unknown"),
|
||||
expires_at=datetime.now(UTC)+timedelta(days=env_settings.REFRESH_TOKEN_EXPIRE_DAYS),
|
||||
)
|
||||
new_token_record=self._token_record_create(jti=new_jti, user_id=sub, token=new_refresh_token, request=request)
|
||||
|
||||
success = self.jwt_db_actions.create_and_update_token(RefreshTokensCreate.model_dump(new_token_record), old_jti, new_jti)
|
||||
|
||||
@@ -188,4 +192,5 @@ class CurrentUserService:
|
||||
|
||||
return (access_token, refresh_token)
|
||||
|
||||
auth=CurrentUserService()
|
||||
def auth_service()->CurrentUserService:
|
||||
return CurrentUserService()
|
||||
Reference in New Issue
Block a user