vincbeck commented on code in PR #75913:
URL: https://github.com/apache/airflow/pull/75913#discussion_r4617401627


##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/routes/login.py:
##########
@@ -112,6 +111,48 @@ def login(request: Request) -> RedirectResponse:
     return response
 
 
+@login_router.get(
+    "/login/cli",
+    responses=create_openapi_http_exception_doc(
+        [
+            status.HTTP_307_TEMPORARY_REDIRECT,
+            status.HTTP_400_BAD_REQUEST,
+            status.HTTP_503_SERVICE_UNAVAILABLE,
+        ]
+    ),
+)
+def login_cli(
+    redirect_uri: str,
+    state: Annotated[str, Query(min_length=1, max_length=512)],
+    # An S256 challenge is 43 base64url characters. ``auth_url`` does not 
encode its parameters,
+    # so nothing else may reach it.
+    code_challenge: Annotated[str, Query(pattern=r"^[A-Za-z0-9_-]{43}$")],
+    code_challenge_method: Literal["S256"],
+) -> RedirectResponse:
+    """Start the browser login of a command-line client."""
+    if not is_cli_authorization_code_enabled():
+        raise HTTPException(
+            status_code=status.HTTP_400_BAD_REQUEST,
+            detail="Browser login 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:
+        # Fetches Keycloak's OpenID configuration.
+        auth_url = client.auth_url(
+            redirect_uri=quote(redirect_uri, safe=""),
+            scope="openid",
+            state=quote(state, safe=""),
+            code_challenge=code_challenge,
+            code_challenge_method=code_challenge_method,
+        )
+    except KeycloakConnectionError:

Review Comment:
   nit: `auth_url` calls `well_known`, which raises `KeycloakGetError` when 
Keycloak answers with a non-2xx (e.g. a 503 while it restarts). That ends up as 
a 500 here instead of the documented 503.



##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/routes/login.py:
##########
@@ -59,7 +51,14 @@ class AuthManagerRefreshTokenExpiredException(Exception):  # 
type: ignore[no-red
     COOKIE_NAME_OAUTH_STATE,
     COOKIE_NAME_REFRESH_TOKEN,
 )
-from airflow.providers.keycloak.auth_manager.keycloak_auth_manager import 
KeycloakAuthManager
+from airflow.providers.keycloak.auth_manager.keycloak_auth_manager import (
+    KeycloakAuthManager,
+    is_cli_authorization_code_enabled,
+)
+from airflow.providers.keycloak.auth_manager.services.token import (

Review Comment:
   nit: `login_callback` still decodes the userinfo and builds 
`KeycloakAuthManagerUser` by hand. Can it use `_build_user_from_userinfo` as 
well? We can do it as a follow-up.



-- 
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]

Reply via email to