|
8 | 8 | import asyncio |
9 | 9 | from typing import Any, Dict, List, Optional, Tuple |
10 | 10 | from urllib.parse import urlencode |
| 11 | +from carbonserver.config import settings |
11 | 12 |
|
12 | 13 | import httpx |
13 | | -from fastapi_oidc import discovery |
| 14 | +from fastapi_oidc import discovery, get_auth |
14 | 15 | from jose import jwt |
15 | 16 |
|
16 | 17 | DEFAULT_SIGNATURE_CACHE_TTL = 3600 # seconds |
17 | | - |
| 18 | +OAUTH_SCOPES = ["openid", "email", "profile"] |
| 19 | + |
| 20 | +from authlib.integrations.starlette_client import OAuth |
| 21 | +oauth = OAuth() |
| 22 | +oauth.register( |
| 23 | + "client", |
| 24 | + client_id=settings.oidc_client_id, |
| 25 | + client_secret=settings.oidc_client_secret, |
| 26 | + server_metadata_url=settings.oidc_well_known_url, |
| 27 | + client_kwargs={"scope": "openid profile email"}, |
| 28 | +) |
18 | 29 |
|
19 | 30 | class OIDCAuthProvider: |
| 31 | + def __init__( |
| 32 | + self, |
| 33 | + base_url: str, |
| 34 | + client_id: str, |
| 35 | + client_secret: str, |
| 36 | + *, |
| 37 | + signature_cache_ttl: int = DEFAULT_SIGNATURE_CACHE_TTL, |
| 38 | + openid_configuration: Optional[Dict[str, Any]] = None, |
| 39 | + ): |
| 40 | + self.client = oauth._clients["client"] |
| 41 | + |
| 42 | + async def get_authorize_url(self, request, login_url): |
| 43 | + return self.client.authorize_redirect(request, str(login_url), scope=' '.join(OAUTH_SCOPES)) |
| 44 | + |
| 45 | + def get_client_credentials(self) -> Tuple[str, str]: |
| 46 | + return (self.client.client_id, self.client.client_secret) |
| 47 | + |
| 48 | + def validate_access_token(self, token): |
| 49 | + ... |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + # async def get_authorize_url(self, redirect_uri: str, **kwargs) -> str: |
| 54 | + # return (await self.client.create_authorization_url(redirect_uri=redirect_uri, **kwargs))["url"] |
| 55 | + |
| 56 | + # def get_token_endpoint(self) -> str: |
| 57 | + # if ( |
| 58 | + # self._openid_configuration |
| 59 | + # and "token_endpoint" in self._openid_configuration |
| 60 | + # ): |
| 61 | + # return self._openid_configuration["token_endpoint"] |
| 62 | + # return f"{self.base_url}/api/token" |
| 63 | + |
| 64 | + # def get_authorize_endpoint(self) -> str: |
| 65 | + # """ |
| 66 | + # Get the authorization endpoint URL. |
| 67 | + |
| 68 | + # Returns: |
| 69 | + # The authorization endpoint URL |
| 70 | + # """ |
| 71 | + # if ( |
| 72 | + # self._openid_configuration |
| 73 | + # and "authorization_endpoint" in self._openid_configuration |
| 74 | + # ): |
| 75 | + # return self._openid_configuration["authorization_endpoint"] |
| 76 | + # return f"{self.base_url}/authorize" |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +""" |
| 83 | +['OAUTH_APP_CONFIG', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_create_oauth2_authorization_url', '_fetch_token', '_format_state_params', '_get_oauth_client', '_on_update_token', '_server_metadata_url', '_update_token', '_user_agent', 'access_token_params', 'access_token_url', 'api_base_url', 'authorize_access_token', 'authorize_params', 'authorize_redirect', 'authorize_url', 'client_auth_methods', 'client_cls', 'client_id', 'client_kwargs', 'client_secret', 'compliance_fix', 'create_authorization_url', 'delete', 'fetch_access_token', 'fetch_jwk_set', 'framework', 'get', 'load_server_metadata', 'name', 'parse_id_token', 'patch', 'post', 'put', 'request', 'save_authorize_data', 'server_metadata', 'userinfo'] |
| 84 | +
|
| 85 | +""" |
| 86 | + |
| 87 | +class OffOIDCAuthProvider: |
20 | 88 | """ |
21 | 89 | Generic OIDC authentication provider implementation. |
22 | 90 |
|
@@ -185,39 +253,44 @@ async def get_user_info(self, access_token: str) -> Dict[str, Any]: |
185 | 253 | response.raise_for_status() |
186 | 254 | return response.json() |
187 | 255 |
|
188 | | - def get_token_endpoint(self) -> str: |
189 | | - """ |
190 | | - Get the token endpoint URL. |
| 256 | + # def get_token_endpoint(self) -> str: |
| 257 | + # """ |
| 258 | + # Get the token endpoint URL. |
191 | 259 |
|
192 | | - Returns: |
193 | | - The token endpoint URL |
194 | | - """ |
195 | | - if ( |
196 | | - self._openid_configuration |
197 | | - and "token_endpoint" in self._openid_configuration |
198 | | - ): |
199 | | - return self._openid_configuration["token_endpoint"] |
200 | | - return f"{self.base_url}/api/token" |
201 | | - |
202 | | - def get_authorize_endpoint(self) -> str: |
203 | | - """ |
204 | | - Get the authorization endpoint URL. |
| 260 | + # Returns: |
| 261 | + # The token endpoint URL |
| 262 | + # """ |
| 263 | + # if ( |
| 264 | + # self._openid_configuration |
| 265 | + # and "token_endpoint" in self._openid_configuration |
| 266 | + # ): |
| 267 | + # return self._openid_configuration["token_endpoint"] |
| 268 | + # return f"{self.base_url}/api/token" |
205 | 269 |
|
206 | | - Returns: |
207 | | - The authorization endpoint URL |
208 | | - """ |
209 | | - if ( |
210 | | - self._openid_configuration |
211 | | - and "authorization_endpoint" in self._openid_configuration |
212 | | - ): |
213 | | - return self._openid_configuration["authorization_endpoint"] |
214 | | - return f"{self.base_url}/authorize" |
| 270 | + # def get_authorize_endpoint(self) -> str: |
| 271 | + # """ |
| 272 | + # Get the authorization endpoint URL. |
215 | 273 |
|
216 | | - def get_client_credentials(self) -> Tuple[str, str]: |
217 | | - """ |
218 | | - Get the client ID and client secret. |
| 274 | + # Returns: |
| 275 | + # The authorization endpoint URL |
| 276 | + # """ |
| 277 | + # if ( |
| 278 | + # self._openid_configuration |
| 279 | + # and "authorization_endpoint" in self._openid_configuration |
| 280 | + # ): |
| 281 | + # return self._openid_configuration["authorization_endpoint"] |
| 282 | + # return f"{self.base_url}/authorize" |
219 | 283 |
|
220 | | - Returns: |
221 | | - A tuple of (client_id, client_secret) |
222 | | - """ |
223 | | - return (self.client_id, self.client_secret) |
| 284 | + |
| 285 | + |
| 286 | + |
| 287 | + |
| 288 | + |
| 289 | + # def get_client_credentials(self) -> Tuple[str, str]: |
| 290 | + # """ |
| 291 | + # Get the client ID and client secret. |
| 292 | + |
| 293 | + # Returns: |
| 294 | + # A tuple of (client_id, client_secret) |
| 295 | + # """ |
| 296 | + # return (self.client_id, self.client_secret) |
0 commit comments