github-advanced-security[bot] commented on code in PR #77443:
URL: https://github.com/apache/airflow/pull/77443#discussion_r4686121252
##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/services/token.py:
##########
@@ -310,6 +316,67 @@
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)
Review Comment:
## CodeQL / Clear-text logging of sensitive information
This expression logs [sensitive data (password)](1) as clear text.
[Show more
details](https://github.com/apache/airflow/security/code-scanning/713)
##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/services/token.py:
##########
@@ -310,6 +316,67 @@
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)
+ if error == "invalid_grant":
+ raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
detail="Invalid device code")
+ # Not the user's fault: e.g. a wrong client secret or the device grant
turned off in Keycloak.
+ log.error("Keycloak refused the device code token request: %s", error
or e)
Review Comment:
## CodeQL / Clear-text logging of sensitive information
This expression logs [sensitive data (password)](1) as clear text.
[Show more
details](https://github.com/apache/airflow/security/code-scanning/714)
--
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]