vincbeck commented on code in PR #77443:
URL: https://github.com/apache/airflow/pull/77443#discussion_r4668782772
##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/services/token.py:
##########
@@ -310,6 +316,60 @@ def create_token_from_authorization_code(
return _create_api_jwt_from_keycloak_tokens(client, tokens,
expiration_time_in_seconds)
+def _require_cli_device_code_enabled() -> None:
+ if not is_cli_device_code_enabled():
+ raise HTTPException(
+ status_code=status.HTTP_400_BAD_REQUEST,
+ detail="Device code login is not enabled for command-line clients",
+ )
+
+
+def _find_oauth_error(error: KeycloakError) -> str | None:
+ """Return the RFC 6749 ``error`` code of a Keycloak error response, or
``None``."""
+ try:
+ body = json.loads(error.response_body or b"")
+ except (TypeError, ValueError):
+ return None
+ code = body.get("error") if isinstance(body, dict) else None
+ return code if isinstance(code, str) else None
+
+
+def start_device_authorization() -> dict:
+ """Start a device code login of a command-line client and return
Keycloak's RFC 8628 response."""
+ _require_cli_device_code_enabled()
+ client = KeycloakAuthManager.get_keycloak_client()
+ try:
+ with translate_keycloak_outage():
+ return client.device(scope="openid")
+ except KeycloakError as e:
+ # Usually the Keycloak client does not have the OAuth 2.0 Device
Authorization Grant turned on.
+ log.error("Keycloak refused the device authorization request: %s",
_find_oauth_error(e) or e)
+ raise HTTPException(
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
+ detail="Keycloak refused the device authorization request",
+ )
+
+
+def create_token_from_device_code(device_code: str,
expiration_time_in_seconds: int) -> str:
+ """
+ Create a CLI token from a device code that the user approved.
+
+ While the login is not approved, this raises a ``400`` whose ``detail`` is
the RFC 8628 error code.
+ """
+ _require_cli_device_code_enabled()
+ client = KeycloakAuthManager.get_keycloak_client()
+ try:
+ with translate_keycloak_outage():
+ tokens = client.token(grant_type=DEVICE_CODE_GRANT_TYPE,
device_code=device_code, scope="openid")
+ except KeycloakError as e:
+ error = _find_oauth_error(e)
+ if error in DEVICE_CODE_CLIENT_ERRORS:
+ raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail=error)
+ raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
detail="Invalid device code")
Review Comment:
nit: any other Keycloak error ends up here without being logged, including
server-side issues like a wrong client secret (`invalid_client`) or the device
grant turned off in Keycloak (`unauthorized_client`). The user is told their
code is invalid and the admin has nothing in the logs. Can you log the error
like `start_device_authorization` does?
--
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]