refactor: clean up code by adding missing commas and improving import order

This commit is contained in:
JSC
2025-07-02 10:46:53 +02:00
parent 171dbb9b63
commit 703212656f
20 changed files with 87 additions and 496 deletions

View File

@@ -89,10 +89,10 @@ class AuthService:
# Generate JWT tokens
access_token = self.token_service.generate_access_token(
jwt_user_data
jwt_user_data,
)
refresh_token = self.token_service.generate_refresh_token(
jwt_user_data
jwt_user_data,
)
# Create response and set HTTP-only cookies
@@ -100,7 +100,7 @@ class AuthService:
{
"message": "Login successful",
"user": jwt_user_data,
}
},
)
# Set JWT cookies
@@ -149,7 +149,7 @@ class AuthService:
return None
def register_with_password(
self, email: str, password: str, name: str
self, email: str, password: str, name: str,
) -> Any:
"""Register new user with email and password."""
try:
@@ -164,10 +164,10 @@ class AuthService:
# Generate JWT tokens
access_token = self.token_service.generate_access_token(
jwt_user_data
jwt_user_data,
)
refresh_token = self.token_service.generate_refresh_token(
jwt_user_data
jwt_user_data,
)
# Create response and set HTTP-only cookies
@@ -175,7 +175,7 @@ class AuthService:
{
"message": "Registration successful",
"user": jwt_user_data,
}
},
)
# Set JWT cookies
@@ -196,7 +196,7 @@ class AuthService:
if not user:
response = jsonify(
{"error": "Invalid email, password or disabled account"}
{"error": "Invalid email, password or disabled account"},
)
response.status_code = 401
return response
@@ -216,7 +216,7 @@ class AuthService:
{
"message": "Login successful",
"user": jwt_user_data,
}
},
)
# Set JWT cookies

View File

@@ -4,7 +4,7 @@ from functools import wraps
from typing import Any
from flask import jsonify, request
from flask_jwt_extended import get_jwt, get_jwt_identity, verify_jwt_in_request
from flask_jwt_extended import get_jwt_identity, verify_jwt_in_request
def get_user_from_jwt() -> dict[str, Any] | None:
@@ -109,7 +109,7 @@ def require_auth(f):
if not user:
return (
jsonify(
{"error": "Authentication required (JWT or API token)"}
{"error": "Authentication required (JWT or API token)"},
),
401,
)
@@ -133,8 +133,8 @@ def require_role(required_role: str):
return (
jsonify(
{
"error": f"Access denied. {required_role.title()} role required"
}
"error": f"Access denied. {required_role.title()} role required",
},
),
403,
)
@@ -152,8 +152,8 @@ def require_credits(credits_needed: int):
def decorator(f):
@wraps(f)
def wrapper(*args, **kwargs):
from app.models.user import User
from app.database import db
from app.models.user import User
# First check authentication
user_data = get_current_user()
@@ -170,8 +170,8 @@ def require_credits(credits_needed: int):
return (
jsonify(
{
"error": f"Insufficient credits. Required: {credits_needed}, Available: {user.credits}"
}
"error": f"Insufficient credits. Required: {credits_needed}, Available: {user.credits}",
},
),
402, # Payment Required status code
)

View File

@@ -1,5 +1,6 @@
from abc import ABC, abstractmethod
from typing import Dict, Any, Optional
from typing import Any
from authlib.integrations.flask_client import OAuth
@@ -16,23 +17,19 @@ class OAuthProvider(ABC):
@abstractmethod
def name(self) -> str:
"""Provider name (e.g., 'google', 'github')."""
pass
@property
@abstractmethod
def display_name(self) -> str:
"""Human-readable provider name (e.g., 'Google', 'GitHub')."""
pass
@abstractmethod
def get_client_config(self) -> Dict[str, Any]:
def get_client_config(self) -> dict[str, Any]:
"""Return OAuth client configuration."""
pass
@abstractmethod
def get_user_info(self, token: Dict[str, Any]) -> Dict[str, Any]:
def get_user_info(self, token: dict[str, Any]) -> dict[str, Any]:
"""Extract user information from OAuth token response."""
pass
def get_client(self):
"""Get or create OAuth client."""
@@ -52,14 +49,14 @@ class OAuthProvider(ABC):
return client.authorize_redirect(redirect_uri).location
def exchange_code_for_token(
self, code: str = None, redirect_uri: str = None
) -> Dict[str, Any]:
self, code: str = None, redirect_uri: str = None,
) -> dict[str, Any]:
"""Exchange authorization code for access token."""
client = self.get_client()
token = client.authorize_access_token()
return token
def normalize_user_data(self, user_info: Dict[str, Any]) -> Dict[str, Any]:
def normalize_user_data(self, user_info: dict[str, Any]) -> dict[str, Any]:
"""Normalize user data to common format."""
return {
"id": user_info.get("id"),

View File

@@ -1,4 +1,5 @@
from typing import Dict, Any
from typing import Any
from .base import OAuthProvider
@@ -13,7 +14,7 @@ class GitHubOAuthProvider(OAuthProvider):
def display_name(self) -> str:
return "GitHub"
def get_client_config(self) -> Dict[str, Any]:
def get_client_config(self) -> dict[str, Any]:
"""Return GitHub OAuth client configuration."""
return {
"access_token_url": "https://github.com/login/oauth/access_token",
@@ -22,7 +23,7 @@ class GitHubOAuthProvider(OAuthProvider):
"client_kwargs": {"scope": "user:email"},
}
def get_user_info(self, token: Dict[str, Any]) -> Dict[str, Any]:
def get_user_info(self, token: dict[str, Any]) -> dict[str, Any]:
"""Extract user information from GitHub OAuth token response."""
client = self.get_client()

View File

@@ -1,4 +1,4 @@
from typing import Any, Dict
from typing import Any
from .base import OAuthProvider
@@ -14,14 +14,14 @@ class GoogleOAuthProvider(OAuthProvider):
def display_name(self) -> str:
return "Google"
def get_client_config(self) -> Dict[str, Any]:
def get_client_config(self) -> dict[str, Any]:
"""Return Google OAuth client configuration."""
return {
"server_metadata_url": "https://accounts.google.com/.well-known/openid-configuration",
"client_kwargs": {"scope": "openid email profile"},
}
def get_user_info(self, token: Dict[str, Any]) -> Dict[str, Any]:
def get_user_info(self, token: dict[str, Any]) -> dict[str, Any]:
"""Extract user information from Google OAuth token response."""
client = self.get_client()
user_info = client.userinfo(token=token)

View File

@@ -1,9 +1,10 @@
import os
from typing import Dict, Optional
from authlib.integrations.flask_client import OAuth
from .base import OAuthProvider
from .google import GoogleOAuthProvider
from .github import GitHubOAuthProvider
from .google import GoogleOAuthProvider
class OAuthProviderRegistry:
@@ -11,7 +12,7 @@ class OAuthProviderRegistry:
def __init__(self, oauth: OAuth):
self.oauth = oauth
self._providers: Dict[str, OAuthProvider] = {}
self._providers: dict[str, OAuthProvider] = {}
self._initialize_providers()
def _initialize_providers(self):
@@ -21,7 +22,7 @@ class OAuthProviderRegistry:
google_client_secret = os.getenv("GOOGLE_CLIENT_SECRET")
if google_client_id and google_client_secret:
self._providers["google"] = GoogleOAuthProvider(
self.oauth, google_client_id, google_client_secret
self.oauth, google_client_id, google_client_secret,
)
# GitHub OAuth
@@ -29,14 +30,14 @@ class OAuthProviderRegistry:
github_client_secret = os.getenv("GITHUB_CLIENT_SECRET")
if github_client_id and github_client_secret:
self._providers["github"] = GitHubOAuthProvider(
self.oauth, github_client_id, github_client_secret
self.oauth, github_client_id, github_client_secret,
)
def get_provider(self, name: str) -> Optional[OAuthProvider]:
def get_provider(self, name: str) -> OAuthProvider | None:
"""Get OAuth provider by name."""
return self._providers.get(name)
def get_available_providers(self) -> Dict[str, OAuthProvider]:
def get_available_providers(self) -> dict[str, OAuthProvider]:
"""Get all available providers."""
return self._providers.copy()