baptistegh commented on code in PR #75913:
URL: https://github.com/apache/airflow/pull/75913#discussion_r4616611978
##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/services/token.py:
##########
@@ -194,19 +248,90 @@ def create_client_credentials_token(
detail="Client credentials authentication failed",
)
- # For client_credentials, get the service account user info
- # The token represents the service account associated with the client
- userinfo_raw: dict | bytes = client.userinfo(tokens["access_token"])
- # Decode bytes to dict if necessary
- userinfo: dict = json.loads(userinfo_raw) if isinstance(userinfo_raw,
bytes) else userinfo_raw
-
- user = KeycloakAuthManagerUser(
- user_id=userinfo["sub"],
- name=userinfo.get("preferred_username", userinfo.get("clientId",
"service-account")),
- access_token=tokens["access_token"],
- refresh_token=tokens.get(
- "refresh_token"
- ), # client_credentials may not return refresh_token (RFC6749 section
4.4.3)
+ return _create_api_jwt_from_keycloak_tokens(
+ client, tokens, expiration_time_in_seconds, is_service_account=True
)
- return get_auth_manager().generate_api_jwt(user,
expiration_time_in_seconds=expiration_time_in_seconds)
+
+def create_token_from_authorization_code(
+ code: str,
+ code_verifier: str,
+ redirect_uri: str,
+ expiration_time_in_seconds: int,
+) -> str:
+ """
+ Create a CLI token from an authorization code obtained through
``/login/cli``.
+
+ Keycloak checks ``code_verifier`` against the challenge ``/login/cli``
forwarded, so
+ the code is useless to anyone who intercepted it on the loopback redirect.
+ """
+ if not is_cli_authorization_code_enabled():
+ raise HTTPException(
+ status_code=status.HTTP_400_BAD_REQUEST,
+ detail="The authorization_code grant is not enabled for
command-line clients",
+ )
+ if not is_cli_loopback_redirect_uri(redirect_uri):
+ raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid redirect_uri")
+
+ client = KeycloakAuthManager.get_keycloak_client()
+ try:
+ tokens = client.token(
+ grant_type="authorization_code",
+ code=code,
+ redirect_uri=redirect_uri,
+ code_verifier=code_verifier,
+ scope="openid",
+ )
+ except KeycloakConnectionError:
+ raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Keycloak is unavailable")
+ except KeycloakError:
+ raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
detail="Invalid authorization code")
+
+ return _create_api_jwt_from_keycloak_tokens(client, tokens,
expiration_time_in_seconds)
Review Comment:
I handled the two other places an outage still gave a `500`:
- The password grant, which `/auth/token` and `/auth/token/cli` share.
`/auth/token` now documents the `503` too.
- `/auth/login/cli`, where `auth_url` fetches Keycloak's OpenID
configuration. Its OpenAPI spec now also lists the `307` redirect.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]