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

@@ -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()