|
| 1 | +# SPDX-FileCopyrightText: 2023-2024 MTS PJSC |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +import logging |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from fastapi import Request |
| 7 | + |
| 8 | +from syncmaster.db.models import User |
| 9 | +from syncmaster.exceptions import EntityNotFoundError |
| 10 | +from syncmaster.exceptions.auth import AuthorizationError |
| 11 | +from syncmaster.server.providers.auth.keycloak_provider import ( |
| 12 | + KeycloakAuthProvider, |
| 13 | + KeycloakOperationError, |
| 14 | +) |
| 15 | + |
| 16 | +log = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +class OAuth2GatewayProvider(KeycloakAuthProvider): |
| 20 | + async def get_current_user(self, access_token: str | None, request: Request) -> User: # noqa: WPS231, WPS217 |
| 21 | + |
| 22 | + if not access_token: |
| 23 | + log.debug("No access token found in request") |
| 24 | + raise AuthorizationError("Missing auth credentials") |
| 25 | + |
| 26 | + try: |
| 27 | + token_info = await self.keycloak_openid.a_introspect(access_token) |
| 28 | + except KeycloakOperationError as e: |
| 29 | + log.info("Failed to introspect token: %s", e) |
| 30 | + raise AuthorizationError("Invalid token payload") |
| 31 | + |
| 32 | + if token_info["active"] is False: |
| 33 | + raise AuthorizationError("Token is not active") |
| 34 | + |
| 35 | + # these names are hardcoded in keycloak: |
| 36 | + # https://github.com/keycloak/keycloak/blob/3ca3a4ad349b4d457f6829eaf2ae05f1e01408be/core/src/main/java/org/keycloak/representations/IDToken.java |
| 37 | + # TODO: make sure which fields are guaranteed |
| 38 | + login = token_info["preferred_username"] |
| 39 | + email = token_info.get("email") |
| 40 | + first_name = token_info.get("given_name") |
| 41 | + middle_name = token_info.get("middle_name") |
| 42 | + last_name = token_info.get("family_name") |
| 43 | + |
| 44 | + async with self._uow: |
| 45 | + try: |
| 46 | + user = await self._uow.user.read_by_username(login) |
| 47 | + except EntityNotFoundError: |
| 48 | + user = await self._uow.user.create( |
| 49 | + username=login, |
| 50 | + email=email, |
| 51 | + first_name=first_name, |
| 52 | + middle_name=middle_name, |
| 53 | + last_name=last_name, |
| 54 | + ) |
| 55 | + return user |
| 56 | + |
| 57 | + async def get_token_authorization_code_grant( |
| 58 | + self, |
| 59 | + code: str, |
| 60 | + scopes: list[str] | None = None, |
| 61 | + client_id: str | None = None, |
| 62 | + client_secret: str | None = None, |
| 63 | + ) -> dict[str, Any]: |
| 64 | + raise NotImplementedError(f"Authorization code grant is not supported by {self.__class__.__name__}.") |
| 65 | + |
| 66 | + async def logout(self, user: User, refresh_token: str | None) -> None: |
| 67 | + raise NotImplementedError(f"Logout is not supported by {self.__class__.__name__}.") |
0 commit comments