46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
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
|
|
|
|
|
|
class OAuthProviderRegistry:
|
|
"""Registry for OAuth providers."""
|
|
|
|
def __init__(self, oauth: OAuth):
|
|
self.oauth = oauth
|
|
self._providers: Dict[str, OAuthProvider] = {}
|
|
self._initialize_providers()
|
|
|
|
def _initialize_providers(self):
|
|
"""Initialize available providers based on environment variables."""
|
|
# Google OAuth
|
|
google_client_id = os.getenv("GOOGLE_CLIENT_ID")
|
|
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
|
|
)
|
|
|
|
# GitHub OAuth
|
|
github_client_id = os.getenv("GITHUB_CLIENT_ID")
|
|
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
|
|
)
|
|
|
|
def get_provider(self, name: str) -> Optional[OAuthProvider]:
|
|
"""Get OAuth provider by name."""
|
|
return self._providers.get(name)
|
|
|
|
def get_available_providers(self) -> Dict[str, OAuthProvider]:
|
|
"""Get all available providers."""
|
|
return self._providers.copy()
|
|
|
|
def is_provider_available(self, name: str) -> bool:
|
|
"""Check if provider is available."""
|
|
return name in self._providers
|