Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /fief-dev_fief /fief /apps /auth /routers /oauth.py
| from datetime import UTC, datetime | |
| from fastapi import APIRouter, Depends, Query, Request, status | |
| from fastapi.responses import RedirectResponse | |
| from httpx_oauth.exceptions import GetIdEmailError | |
| from httpx_oauth.oauth2 import GetAccessTokenError | |
| from fief.dependencies.authentication_flow import get_authentication_flow | |
| from fief.dependencies.oauth import ( | |
| get_oauth_provider, | |
| get_oauth_session, | |
| get_optional_login_session, | |
| get_optional_login_session_with_tenant_query, | |
| get_tenant_by_query, | |
| ) | |
| from fief.dependencies.oauth_provider import get_oauth_providers | |
| from fief.dependencies.register import get_registration_flow | |
| from fief.dependencies.repositories import get_repository | |
| from fief.dependencies.session_token import get_session_token | |
| from fief.exceptions import OAuthException | |
| from fief.locale import gettext_lazy as _ | |
| from fief.models import ( | |
| LoginSession, | |
| OAuthAccount, | |
| OAuthProvider, | |
| OAuthSession, | |
| RegistrationSessionFlow, | |
| SessionToken, | |
| Tenant, | |
| ) | |
| from fief.repositories import OAuthAccountRepository, OAuthSessionRepository | |
| from fief.schemas.oauth import OAuthError | |
| from fief.services.authentication_flow import AuthenticationFlow | |
| from fief.services.oauth_provider import get_oauth_provider_service | |
| from fief.services.registration_flow import RegistrationFlow | |
| router = APIRouter(prefix="/oauth") | |
| async def authorize( | |
| request: Request, | |
| tenant: Tenant = Depends(get_tenant_by_query), | |
| oauth_provider: OAuthProvider = Depends(get_oauth_provider), | |
| oauth_session_repository: OAuthSessionRepository = Depends( | |
| get_repository(OAuthSessionRepository) | |
| ), | |
| ): | |
| redirect_uri = str(request.url_for("oauth:callback")) | |
| oauth_session = OAuthSession( | |
| redirect_uri=redirect_uri, | |
| oauth_provider=oauth_provider, | |
| tenant=tenant, | |
| ) | |
| oauth_session = await oauth_session_repository.create(oauth_session) | |
| state = oauth_session.token | |
| oauth_provider_service = get_oauth_provider_service(oauth_provider) | |
| scopes = set(oauth_provider_service.base_scopes or []) | set(oauth_provider.scopes) | |
| authorize_url = await oauth_provider_service.get_authorization_url( | |
| redirect_uri, state=state, scope=list(scopes) | |
| ) | |
| return RedirectResponse(authorize_url, status_code=status.HTTP_302_FOUND) | |
| async def callback( | |
| request: Request, | |
| code: str | None = Query(None), | |
| code_verifier: str | None = Query(None), | |
| oauth_session: OAuthSession = Depends(get_oauth_session), | |
| login_session: LoginSession | None = Depends(get_optional_login_session), | |
| oauth_providers: list[OAuthProvider] | None = Depends(get_oauth_providers), | |
| oauth_session_repository: OAuthSessionRepository = Depends( | |
| get_repository(OAuthSessionRepository) | |
| ), | |
| oauth_account_repository: OAuthAccountRepository = Depends( | |
| get_repository(OAuthAccountRepository) | |
| ), | |
| authentication_flow: AuthenticationFlow = Depends(get_authentication_flow), | |
| registration_flow: RegistrationFlow = Depends(get_registration_flow), | |
| session_token: SessionToken | None = Depends(get_session_token), | |
| ): | |
| assert code is not None | |
| tenant = oauth_session.tenant | |
| oauth_provider = oauth_session.oauth_provider | |
| oauth_provider_service = get_oauth_provider_service(oauth_provider) | |
| try: | |
| access_token_dict = await oauth_provider_service.get_access_token( | |
| code, oauth_session.redirect_uri, code_verifier | |
| ) | |
| except GetAccessTokenError as e: | |
| raise OAuthException( | |
| OAuthError.get_access_token_error( | |
| _("An error occurred while getting the access token.") | |
| ), | |
| oauth_providers=oauth_providers, | |
| tenant=tenant, | |
| ) from e | |
| access_token = access_token_dict["access_token"] | |
| refresh_token = access_token_dict.get("refresh_token") | |
| try: | |
| expires_at = datetime.fromtimestamp(access_token_dict["expires_at"], tz=UTC) | |
| except KeyError: | |
| expires_at = None | |
| try: | |
| account_id, account_email = await oauth_provider_service.get_id_email( | |
| access_token | |
| ) | |
| except GetIdEmailError as e: | |
| raise OAuthException( | |
| OAuthError.get_id_email_error( | |
| _( | |
| "An error occurred while querying the provider API. Original error message: %(message)s", | |
| message=str(e), | |
| ) | |
| ), | |
| oauth_providers=oauth_providers, | |
| tenant=tenant, | |
| ) from e | |
| oauth_account = await oauth_account_repository.get_by_provider_and_account_id( | |
| oauth_provider.id, account_id | |
| ) | |
| # Existing account | |
| if oauth_account is not None: | |
| user = oauth_account.user | |
| if user and not user.is_active: | |
| raise OAuthException( | |
| OAuthError.get_inactive_user(_("Your account is inactive.")), | |
| oauth_providers=oauth_providers, | |
| tenant=tenant, | |
| ) | |
| # Update tokens | |
| oauth_account.access_token = access_token | |
| oauth_account.refresh_token = refresh_token | |
| oauth_account.expires_at = expires_at | |
| await oauth_account_repository.update(oauth_account) | |
| if user: | |
| # Redirect to consent or profile | |
| if login_session is not None: | |
| response = RedirectResponse( | |
| tenant.url_path_for(request, "auth:consent"), | |
| status_code=status.HTTP_302_FOUND, | |
| ) | |
| else: | |
| response = RedirectResponse( | |
| tenant.url_path_for(request, "auth.dashboard:profile"), | |
| status_code=status.HTTP_302_FOUND, | |
| ) | |
| response = await authentication_flow.rotate_session_token( | |
| response, user.id, session_token=session_token | |
| ) | |
| response = await authentication_flow.set_login_hint( | |
| response, str(oauth_provider.id) | |
| ) | |
| return response | |
| # Redirect to register | |
| response = RedirectResponse( | |
| tenant.url_path_for(request, "register:register"), | |
| status_code=status.HTTP_302_FOUND, | |
| ) | |
| await registration_flow.create_registration_session( | |
| response, | |
| RegistrationSessionFlow.OAUTH, | |
| tenant=tenant, | |
| oauth_account=oauth_account, | |
| ) | |
| return response | |
| # New account to create | |
| oauth_account = OAuthAccount( | |
| access_token=access_token, | |
| expires_at=expires_at, | |
| refresh_token=refresh_token, | |
| account_id=account_id, | |
| account_email=account_email, | |
| oauth_provider=oauth_provider, | |
| tenant_id=tenant.id, | |
| ) | |
| oauth_account = await oauth_account_repository.create(oauth_account) | |
| oauth_session.oauth_account = oauth_account | |
| await oauth_session_repository.update(oauth_session) | |
| response = RedirectResponse( | |
| tenant.url_path_for(request, "register:register"), | |
| status_code=status.HTTP_302_FOUND, | |
| ) | |
| await registration_flow.create_registration_session( | |
| response, | |
| RegistrationSessionFlow.OAUTH, | |
| tenant=tenant, | |
| oauth_account=oauth_account, | |
| ) | |
| return response | |
Xet Storage Details
- Size:
- 7.46 kB
- Xet hash:
- 8c4d86954fd4e3c760ff7cd0da4ec3c9ba0da49edb9c462c2b9fdd2849feb636
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.