Add OAuth 2.0 Device Authorization Grant (device flow) authentication - #154
Merged
Conversation
New DeviceCodeAuth (fhir_pyrate/util/device_code_auth.py): RFC 8628 device flow with OpenID Connect discovery, proactive token refresh, a one-shot retry on 401 or on a redirect to the identity provider (how oauth2-proxy answers expired tokens), an opt-in owner-only token cache file, and pickling support for the multiprocessing of Pirate and DicomDownloader. Wired into Ahoy as auth_type="device_code" (client_id, client_secret, scope, token_cache); existing auth types unchanged. No new dependencies. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HjMC3NJgxxmoeCvqMKgdAy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds support for the OAuth 2.0 Device Authorization Grant (RFC 8628, "device flow") to FHIR-PYrate, so that scripts and notebooks can authenticate against FHIR servers and DICOMweb gateways that sit behind an OpenID Connect provider (e.g. Keycloak, commonly fronted by oauth2-proxy — the usual way to secure DICOM-RST deployments) — without ever putting a password into a script, shell history, or environment variable.
The script prints a verification URL, the user approves the sign-in once in any browser (institutional SSO/MFA applies), and the tokens are attached and refreshed automatically from then on. The resulting
Ahoyobject works unchanged with bothPirate(FHIR) andDicomDownloader(DICOMweb).How
fhir_pyrate/util/device_code_auth.pywithDeviceCodeAuth(requests.auth.AuthBase):authorization_pending,slow_down,expired_token,access_denied, device-code deadline).TokenAuth), using the refresh token.requests' ownHTTPDigestAuthvia a per-request response hook. Response hooks run before redirect resolution, so the redirect case is caught before the caller would receive an HTML login page. Two safety details: ifrequestsstripped theAuthorizationheader (cross-host redirect protection), the hook does not re-attach a fresh token (that 401 was not a rejection of our token, and re-sending one would leak it to a foreign host); and a consumed file-like/generator body is rewound before the retry (likerequestsdoes for redirects) or, if it cannot be rewound, the retry is skipped rather than sending a truncated body.session.authis copied to another session — which is exactly whatPiratedoes for itsrequests-cachesession.__setstate__), so they survive themultiprocessingfan-out ofPirateandDicomDownloader.token_cachefile (created with owner-only permissions0600) so new runs reuse the previous sign-in instead of opening the browser again; caches are ignored if they belong to a different client or server.scope="offline_access"for jobs that outlive the SSO session;allow_reauthentication=Falsefor unattended jobs that should fail loudly instead of waiting for a browser approval nobody will give.Ahoygains thedevice_codeauth type (client_id,client_secret,scope,token_cacheparameters). Existing auth types are untouched; all existing tests pass unchanged.Testing
tests/test_device_code_auth.py:Authorizationand body-rewind edge cases, and theAhoywiring — in the same mock style astests/test_auth.py.http.server): discovery → device flow → authenticated request → server-side token revocation → automatic refresh + retry → token-cache reuse, plus the oauth2-proxy-style redirect challenge.uv run pytest: 49 passed (on Python 3.12 and 3.10).uv run pre-commit run -a: all hooks green (ruff check/format, mypy, prettier, pyproject-fmt, markdownlint, codespell, misc)./authendpoint, not a 401 — which is what motivated the redirect interception).🤖 Generated with Claude Code
https://claude.ai/code/session_01HjMC3NJgxxmoeCvqMKgdAy