baptistegh opened a new pull request, #77443:
URL: https://github.com/apache/airflow/pull/77443

   Browser login for command-line clients (#75913) needs a browser on the 
machine that runs the client, because Keycloak redirects back to `127.0.0.1`. 
Over SSH, in a container or on a remote host there is none. This adds the OAuth 
2.0 device authorization grant (RFC 8628): the client prints a URL and a code, 
and the user approves the login on any device.
   
   This is the provider side. The airflowctl side (`auth login --device-code`) 
is in #CTL_PR. The two PRs are independent and can be merged in either order.
   
   related: #75560
   
   ### Changes
   
   - New option `[keycloak_auth_manager] cli_device_code_enabled` (default 
`False`). When it is on, `get_cli_login_methods()` also advertises 
`urn:ietf:params:oauth:grant-type:device_code` and 
`device_authorization_endpoint: /auth/token/cli/device`. It can be on with or 
without browser login.
   - `POST /auth/token/cli/device` (no body) starts the login through 
Keycloak's device endpoint with `scope=openid` and returns the RFC 8628 fields.
   - `/auth/token/cli` accepts `{"grant_type": 
"urn:ietf:params:oauth:grant-type:device_code", "device_code": ...}`. 
`/auth/token` does not.
     - While the login is not approved, it returns `400` with the RFC 8628 
error code as `detail`: `authorization_pending`, `slow_down`, `access_denied` 
or `expired_token`.
     - An unknown or already used code gets `403`, and a Keycloak outage gets 
`503`.
     - Once approved, the CLI token is minted the same way as for the other 
grants.
   - If Keycloak refuses to start the login, usually because the client does 
not have "OAuth 2.0 Device Authorization Grant" on, the route logs why and 
returns `500`.
   - Docs: a "Device code login" section in `token.rst` with the setup, the 
request flow, the error codes and a phishing warning. The client settings table 
in `manage/login.rst` says when to turn the grant on.
   
   The option is off by default because anyone can start a device login and 
send the code to someone else, who would log them in by approving it.
   
   ### Trying it without airflowctl
   
   With `cli_device_code_enabled = True` and "OAuth 2.0 Device Authorization 
Grant" on in the Keycloak client:
   
   ```bash
   # 1. Start the login, then open verification_uri_complete in a browser and 
log in
   curl -X POST "${AIRFLOW_URL}/auth/token/cli/device"
   
   # 2. Poll until the login is approved (400 authorization_pending until then)
   curl -X POST "${AIRFLOW_URL}/auth/token/cli" -H 'Content-Type: 
application/json' \
     -d '{"grant_type": "urn:ietf:params:oauth:grant-type:device_code", 
"device_code": "<device_code>"}'
   ```
   
   Tested end to end in Breeze with Keycloak and #CTL_PR: approved, refused and 
timed-out logins, the token on the API and its refresh, and a Keycloak outage 
while polling.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code (Opus 5.5) following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


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