|
| 1 | +""" |
| 2 | +OIDC Authentication Provider Implementation |
| 3 | +
|
| 4 | +This module provides a generic OIDC authentication provider implementation using fastapi-oidc. |
| 5 | +It can work with any OIDC-compliant provider (Fief, Keycloak, Auth0, etc.). |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | +from typing import Any, Dict, List, Optional, Tuple |
| 10 | +from urllib.parse import urlencode |
| 11 | + |
| 12 | +import httpx |
| 13 | +from fastapi_oidc import discovery |
| 14 | +from jose import jwt |
| 15 | + |
| 16 | +DEFAULT_SIGNATURE_CACHE_TTL = 3600 # seconds |
| 17 | + |
| 18 | + |
| 19 | +class OIDCAuthProvider: |
| 20 | + """ |
| 21 | + Generic OIDC authentication provider implementation. |
| 22 | +
|
| 23 | + This class uses OIDC discovery and validation (via fastapi-oidc) to interact with |
| 24 | + any OIDC-compliant authentication server (such as Fief, Keycloak, Auth0, etc.). |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + base_url: str, |
| 30 | + client_id: str, |
| 31 | + client_secret: str, |
| 32 | + *, |
| 33 | + signature_cache_ttl: int = DEFAULT_SIGNATURE_CACHE_TTL, |
| 34 | + openid_configuration: Optional[Dict[str, Any]] = None, |
| 35 | + ): |
| 36 | + """ |
| 37 | + Initialize the OIDC authentication provider. |
| 38 | +
|
| 39 | + Args: |
| 40 | + base_url: The OIDC issuer URL (base URL of the authentication server) |
| 41 | + client_id: The OAuth2 client ID |
| 42 | + client_secret: The OAuth2 client secret |
| 43 | + signature_cache_ttl: Seconds to cache the OIDC discovery/JWKS responses |
| 44 | + openid_configuration: Optional pre-loaded OIDC configuration (used mainly for testing) |
| 45 | + """ |
| 46 | + self.base_url = base_url.rstrip("/") |
| 47 | + self.client_id = client_id |
| 48 | + self.client_secret = client_secret |
| 49 | + self._discovery = discovery.configure(cache_ttl=signature_cache_ttl) |
| 50 | + self._openid_configuration = openid_configuration |
| 51 | + |
| 52 | + async def _get_openid_configuration(self) -> Dict[str, Any]: |
| 53 | + if self._openid_configuration is None: |
| 54 | + self._openid_configuration = await asyncio.to_thread( |
| 55 | + self._discovery.auth_server, base_url=self.base_url |
| 56 | + ) |
| 57 | + return self._openid_configuration |
| 58 | + |
| 59 | + async def _get_jwks(self) -> Dict[str, Any]: |
| 60 | + oidc_config = await self._get_openid_configuration() |
| 61 | + return await asyncio.to_thread(self._discovery.public_keys, oidc_config) |
| 62 | + |
| 63 | + async def _get_algorithms(self) -> List[str]: |
| 64 | + oidc_config = await self._get_openid_configuration() |
| 65 | + return await asyncio.to_thread(self._discovery.signing_algos, oidc_config) |
| 66 | + |
| 67 | + async def _decode_token( |
| 68 | + self, token: str, *, audience: Optional[str] = None |
| 69 | + ) -> Dict[str, Any]: |
| 70 | + oidc_config = await self._get_openid_configuration() |
| 71 | + jwks = await self._get_jwks() |
| 72 | + algorithms = await self._get_algorithms() |
| 73 | + return jwt.decode( |
| 74 | + token, |
| 75 | + jwks, |
| 76 | + algorithms=algorithms, |
| 77 | + audience=audience or self.client_id, |
| 78 | + issuer=oidc_config.get("issuer", self.base_url), |
| 79 | + options={"verify_at_hash": False}, |
| 80 | + ) |
| 81 | + |
| 82 | + async def get_auth_url( |
| 83 | + self, redirect_uri: str, scope: List[str], state: Optional[str] = None |
| 84 | + ) -> str: |
| 85 | + """ |
| 86 | + Generate the authorization URL for the OAuth2 flow. |
| 87 | +
|
| 88 | + Args: |
| 89 | + redirect_uri: The URI to redirect to after authentication |
| 90 | + scope: List of OAuth2 scopes to request |
| 91 | + state: Optional state parameter for CSRF protection |
| 92 | +
|
| 93 | + Returns: |
| 94 | + The authorization URL to redirect the user to |
| 95 | + """ |
| 96 | + oidc_config = await self._get_openid_configuration() |
| 97 | + authorize_endpoint = oidc_config.get( |
| 98 | + "authorization_endpoint", f"{self.base_url}/authorize" |
| 99 | + ) |
| 100 | + params = { |
| 101 | + "response_type": "code", |
| 102 | + "client_id": self.client_id, |
| 103 | + "redirect_uri": redirect_uri, |
| 104 | + "scope": " ".join(scope), |
| 105 | + } |
| 106 | + if state is not None: |
| 107 | + params["state"] = state |
| 108 | + |
| 109 | + return f"{authorize_endpoint}?{urlencode(params)}" |
| 110 | + |
| 111 | + async def handle_auth_callback( |
| 112 | + self, code: str, redirect_uri: str |
| 113 | + ) -> Tuple[Dict[str, Any], Optional[Dict[str, Any]]]: |
| 114 | + """ |
| 115 | + Handle the OAuth2 callback and exchange the code for tokens. |
| 116 | +
|
| 117 | + Args: |
| 118 | + code: The authorization code from the OAuth2 provider |
| 119 | + redirect_uri: The redirect URI used in the initial auth request |
| 120 | +
|
| 121 | + Returns: |
| 122 | + A tuple of (tokens, user_info) where: |
| 123 | + - tokens: Dict containing access_token, refresh_token, expires_in, etc. |
| 124 | + - user_info: Optional dict containing user information |
| 125 | + """ |
| 126 | + oidc_config = await self._get_openid_configuration() |
| 127 | + token_endpoint = oidc_config.get("token_endpoint", f"{self.base_url}/api/token") |
| 128 | + async with httpx.AsyncClient() as client: |
| 129 | + response = await client.post( |
| 130 | + token_endpoint, |
| 131 | + data={ |
| 132 | + "grant_type": "authorization_code", |
| 133 | + "code": code, |
| 134 | + "redirect_uri": redirect_uri, |
| 135 | + "client_id": self.client_id, |
| 136 | + "client_secret": self.client_secret, |
| 137 | + }, |
| 138 | + headers={"accept": "application/json"}, |
| 139 | + ) |
| 140 | + response.raise_for_status() |
| 141 | + tokens: Dict[str, Any] = response.json() |
| 142 | + |
| 143 | + user_info: Optional[Dict[str, Any]] = None |
| 144 | + if "id_token" in tokens: |
| 145 | + user_info = await self._decode_token(tokens["id_token"]) |
| 146 | + elif "access_token" in tokens: |
| 147 | + try: |
| 148 | + user_info = await self.get_user_info(tokens["access_token"]) |
| 149 | + except Exception: |
| 150 | + # If userinfo fails we still return tokens |
| 151 | + user_info = None |
| 152 | + |
| 153 | + return (tokens, user_info) |
| 154 | + |
| 155 | + async def validate_access_token(self, token: str) -> bool: |
| 156 | + """ |
| 157 | + Validate an access token. |
| 158 | +
|
| 159 | + Args: |
| 160 | + token: The access token to validate |
| 161 | +
|
| 162 | + Returns: |
| 163 | + True if the token is valid |
| 164 | +
|
| 165 | + Raises: |
| 166 | + Exception if validation fails |
| 167 | + """ |
| 168 | + await self._decode_token(token) |
| 169 | + return True |
| 170 | + |
| 171 | + async def get_user_info(self, access_token: str) -> Dict[str, Any]: |
| 172 | + """ |
| 173 | + Get user information from the OIDC provider. |
| 174 | +
|
| 175 | + Args: |
| 176 | + access_token: The access token for the user |
| 177 | +
|
| 178 | + Returns: |
| 179 | + Dict containing user information (sub, email, name, etc.) |
| 180 | + """ |
| 181 | + oidc_config = await self._get_openid_configuration() |
| 182 | + userinfo_endpoint = oidc_config.get( |
| 183 | + "userinfo_endpoint", f"{self.base_url}/api/userinfo" |
| 184 | + ) |
| 185 | + headers = {"Authorization": f"Bearer {access_token}"} |
| 186 | + async with httpx.AsyncClient() as client: |
| 187 | + response = await client.get(userinfo_endpoint, headers=headers) |
| 188 | + response.raise_for_status() |
| 189 | + return response.json() |
| 190 | + |
| 191 | + def get_token_endpoint(self) -> str: |
| 192 | + """ |
| 193 | + Get the token endpoint URL. |
| 194 | +
|
| 195 | + Returns: |
| 196 | + The token endpoint URL |
| 197 | + """ |
| 198 | + if ( |
| 199 | + self._openid_configuration |
| 200 | + and "token_endpoint" in self._openid_configuration |
| 201 | + ): |
| 202 | + return self._openid_configuration["token_endpoint"] |
| 203 | + return f"{self.base_url}/api/token" |
| 204 | + |
| 205 | + def get_authorize_endpoint(self) -> str: |
| 206 | + """ |
| 207 | + Get the authorization endpoint URL. |
| 208 | +
|
| 209 | + Returns: |
| 210 | + The authorization endpoint URL |
| 211 | + """ |
| 212 | + if ( |
| 213 | + self._openid_configuration |
| 214 | + and "authorization_endpoint" in self._openid_configuration |
| 215 | + ): |
| 216 | + return self._openid_configuration["authorization_endpoint"] |
| 217 | + return f"{self.base_url}/authorize" |
| 218 | + |
| 219 | + def get_client_credentials(self) -> Tuple[str, str]: |
| 220 | + """ |
| 221 | + Get the client ID and client secret. |
| 222 | +
|
| 223 | + Returns: |
| 224 | + A tuple of (client_id, client_secret) |
| 225 | + """ |
| 226 | + return (self.client_id, self.client_secret) |
0 commit comments