fix(typing): resolve mypy errors in services.authentication

This commit is contained in:
Mark Paronyan committed 2026-09-28 21:48:40 +03:00
1 parent 8df25dec32
commit 9f36fb4500
2 files changed
+7 -6

No files matched your search

+7 -5
View File
@@ -73,21 +73,23 @@ class AuthService:
encoded_jwt = jwt.encode({"alg": algorithm}, to_encode, key)
return encoded_jwt
def create_access_token(self, username, token_version: int = 0, secret_key: str = None, expires_in: int = 0) -> str:
def create_access_token(
self, username, token_version: int = 0, secret_key: Optional[str] = None, expires_in: int = 0
) -> str:
if not expires_in:
expires_in = Config.instance().settings.Controller.jwt_access_token_expire_minutes
return self._create_token(username, token_version, "access", expires_in, secret_key)
def create_refresh_token(
self, username, token_version: int = 0, secret_key: str = None, expires_in: int = 0
self, username, token_version: int = 0, secret_key: Optional[str] = None, expires_in: int = 0
) -> str:
if not expires_in:
expires_in = Config.instance().settings.Controller.jwt_refresh_token_expire_minutes
return self._create_token(username, token_version, "refresh", expires_in, secret_key)
def get_token_data(self, token: str, secret_key: str = None) -> TokenData:
def get_token_data(self, token: str, secret_key: Optional[str] = None) -> TokenData:
def auth_error(detail: str) -> HTTPException:
return HTTPException(
@@ -105,7 +107,7 @@ class AuthService:
key = OctKey.import_key(secret_key)
try:
payload = jwt.decode(token, key, algorithms=[algorithm])
username: str = payload.claims.get("sub")
username: Optional[str] = payload.claims.get("sub")
if username is None:
raise auth_error("Invalid token: missing subject claim")
# Validate the exp claim — joserfc does not validate time-based claims by default
@@ -123,5 +125,5 @@ class AuthService:
raise auth_error(f"Invalid token ({type(e).__name__})")
return token_data
def get_username_from_token(self, token: str, secret_key: str = None) -> Optional[str]:
def get_username_from_token(self, token: str, secret_key: Optional[str] = None) -> Optional[str]:
return self.get_token_data(token, secret_key).username