Skip to content

Commit 5c57819

Browse files
committed
[DOP-29557] Raise proper error if Keycloak token is malformed
1 parent 7918c19 commit 5c57819

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

syncmaster/server/providers/auth/keycloak_provider.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ async def get_token_password_grant(
5252
client_id: str | None = None,
5353
client_secret: str | None = None,
5454
) -> dict[str, Any]:
55-
raise NotImplementedError(f"Password grant is not supported by {self.__class__.__name__}.")
55+
raise NotImplementedError(
56+
f"Password grant is not supported by {self.__class__.__name__}.", # noqa: WPS237
57+
)
5658

5759
async def get_token_authorization_code_grant(
5860
self,
@@ -109,7 +111,10 @@ async def get_current_user(self, access_token: str | None, request: Request) ->
109111
# these names are hardcoded in keycloak:
110112
# https://github.com/keycloak/keycloak/blob/3ca3a4ad349b4d457f6829eaf2ae05f1e01408be/core/src/main/java/org/keycloak/representations/IDToken.java
111113
# TODO: make sure which fields are guaranteed
112-
login = token_info["preferred_username"]
114+
login = token_info.get("preferred_username")
115+
if not login:
116+
raise AuthorizationError("Invalid token")
117+
113118
email = token_info.get("email")
114119
first_name = token_info.get("given_name")
115120
middle_name = token_info.get("middle_name")

syncmaster/server/providers/auth/oauth2_gateway_provider.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
class OAuth2GatewayProvider(KeycloakAuthProvider):
24-
def __init__(
24+
def __init__( # noqa: WPS612
2525
self,
2626
settings: Annotated[OAuth2GatewayProviderSettings, Depends(Stub(OAuth2GatewayProviderSettings))],
2727
unit_of_work: Annotated[UnitOfWork, Depends()],
@@ -38,8 +38,11 @@ def setup(cls, app: FastAPI) -> FastAPI:
3838
app.dependency_overrides[OAuth2GatewayProviderSettings] = lambda: settings
3939
return app
4040

41-
async def get_current_user(self, access_token: str | None, request: Request) -> User: # noqa: WPS231, WPS217
42-
41+
async def get_current_user( # noqa: WPS231, WPS217, WPS238
42+
self,
43+
access_token: str | None,
44+
request: Request,
45+
) -> User:
4346
if not access_token:
4447
log.debug("No access token found in request")
4548
raise AuthorizationError("Missing auth credentials")
@@ -56,7 +59,10 @@ async def get_current_user(self, access_token: str | None, request: Request) ->
5659
# these names are hardcoded in keycloak:
5760
# https://github.com/keycloak/keycloak/blob/3ca3a4ad349b4d457f6829eaf2ae05f1e01408be/core/src/main/java/org/keycloak/representations/IDToken.java
5861
# TODO: make sure which fields are guaranteed
59-
login = token_info["preferred_username"]
62+
login = token_info.get("preferred_username")
63+
if not login:
64+
raise AuthorizationError("Invalid token")
65+
6066
email = token_info.get("email")
6167
first_name = token_info.get("given_name")
6268
middle_name = token_info.get("middle_name")
@@ -82,7 +88,11 @@ async def get_token_authorization_code_grant(
8288
client_id: str | None = None,
8389
client_secret: str | None = None,
8490
) -> dict[str, Any]:
85-
raise NotImplementedError(f"Authorization code grant is not supported by {self.__class__.__name__}.")
91+
raise NotImplementedError(
92+
f"Authorization code grant is not supported by {self.__class__.__name__}.", # noqa: WPS237
93+
)
8694

8795
async def logout(self, user: User, refresh_token: str | None) -> None:
88-
raise NotImplementedError(f"Logout is not supported by {self.__class__.__name__}.")
96+
raise NotImplementedError(
97+
f"Logout is not supported by {self.__class__.__name__}.", # noqa: WPS237
98+
)

0 commit comments

Comments
 (0)