refactor: modernize code quality tooling with Ruff
- Replace black, isort, and flake8 with Ruff (all-in-one linter and formatter) - Add comprehensive pyproject.toml configuration - Simplify Makefile code quality targets - Configure exclusions for venv/.venv in pyproject.toml - Auto-fix 1,359 linting issues across codebase Benefits: - Much faster builds (Ruff is written in Rust) - Single tool replaces multiple tools - More comprehensive rule set (UP, B, C4, SIM, PIE, RET, Q) - All configuration centralized in pyproject.toml - Better import sorting and formatting consistency 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -17,8 +17,9 @@ The module uses the following technologies:
|
||||
|
||||
import logging
|
||||
import os
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Any, Callable, Dict, Optional
|
||||
from collections.abc import Callable
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from fastapi import HTTPException
|
||||
from fastapi.security import HTTPAuthorizationCredentials
|
||||
@@ -26,10 +27,14 @@ from jose import jwt
|
||||
from passlib.context import CryptContext
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.exceptions import (AdminRequiredException,
|
||||
InsufficientPermissionsException,
|
||||
InvalidCredentialsException, InvalidTokenException,
|
||||
TokenExpiredException, UserNotActiveException)
|
||||
from app.exceptions import (
|
||||
AdminRequiredException,
|
||||
InsufficientPermissionsException,
|
||||
InvalidCredentialsException,
|
||||
InvalidTokenException,
|
||||
TokenExpiredException,
|
||||
UserNotActiveException,
|
||||
)
|
||||
from models.database.user import User
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -97,7 +102,7 @@ class AuthManager:
|
||||
|
||||
def authenticate_user(
|
||||
self, db: Session, username: str, password: str
|
||||
) -> Optional[User]:
|
||||
) -> User | None:
|
||||
"""Authenticate user credentials against the database.
|
||||
|
||||
Supports authentication using either username or email address.
|
||||
@@ -129,7 +134,7 @@ class AuthManager:
|
||||
# Authentication successful, return user object
|
||||
return user
|
||||
|
||||
def create_access_token(self, user: User) -> Dict[str, Any]:
|
||||
def create_access_token(self, user: User) -> dict[str, Any]:
|
||||
"""Create a JWT access token for an authenticated user.
|
||||
|
||||
The token includes user identity and role information in the payload.
|
||||
@@ -146,7 +151,7 @@ class AuthManager:
|
||||
"""
|
||||
# Calculate token expiration time
|
||||
expires_delta = timedelta(minutes=self.token_expire_minutes)
|
||||
expire = datetime.now(timezone.utc) + expires_delta
|
||||
expire = datetime.now(UTC) + expires_delta
|
||||
|
||||
# Build JWT payload with user information
|
||||
payload = {
|
||||
@@ -155,7 +160,7 @@ class AuthManager:
|
||||
"email": user.email, # User email address
|
||||
"role": user.role, # User role for authorization
|
||||
"exp": expire, # Expiration time (JWT standard claim)
|
||||
"iat": datetime.now(timezone.utc), # Issued at time (JWT standard claim)
|
||||
"iat": datetime.now(UTC), # Issued at time (JWT standard claim)
|
||||
}
|
||||
|
||||
# Encode the payload into a JWT token
|
||||
@@ -168,7 +173,7 @@ class AuthManager:
|
||||
"expires_in": self.token_expire_minutes * 60, # Convert minutes to seconds
|
||||
}
|
||||
|
||||
def verify_token(self, token: str) -> Dict[str, Any]:
|
||||
def verify_token(self, token: str) -> dict[str, Any]:
|
||||
"""Verify and decode a JWT token, returning the user data.
|
||||
|
||||
Validates the token signature, expiration, and required claims.
|
||||
@@ -199,8 +204,8 @@ class AuthManager:
|
||||
raise InvalidTokenException("Token missing expiration")
|
||||
|
||||
# Check if token has expired (additional check beyond jwt.decode)
|
||||
if datetime.now(timezone.utc) > datetime.fromtimestamp(
|
||||
exp, tz=timezone.utc
|
||||
if datetime.now(UTC) > datetime.fromtimestamp(
|
||||
exp, tz=UTC
|
||||
):
|
||||
raise TokenExpiredException()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user