Skip to content

feat(auth): log in through the DualEntry OAuth server - #48

Open
maciejdziag wants to merge 1 commit into
dualentry:mainfrom
maciejdziag:maciejd/eops-635-cli-oauth-login
Open

maciejdziag wants to merge 1 commit into
dualentry:mainfrom
maciejdziag:maciejd/eops-635-cli-oauth-login

Conversation

@maciejdziag

Copy link
Copy Markdown

Summary

dualentry auth login now signs in through the DualEntry OAuth server instead of creating an API key. It reads the server metadata (RFC 8414), opens the browser with client_id=dualentry-cli, PKCE (S256) and a 127.0.0.1 loopback redirect, and keeps the access and refresh tokens in the keychain. Requests send Authorization: Bearer, refresh before expiry and once on a 401. X_API_KEY from the environment still comes first. auth logout revokes the login on the server.

Changes

  • Discovery: reads /.well-known/oauth-authorization-server/auth/oauth (the issuer {api_url}/auth/oauth), then the bare well-known path. Metadata is accepted only when its issuer is the one the URL names (RFC 8414 §3.3), so the metadata of another server on the same host is refused. S256 must be supported.
  • Login: the loopback server binds 127.0.0.1 on a port the system picks before the browser opens. The callback checks state and the iss parameter (RFC 9207), and shows a failure page on an error. The token request is a form post with client_id, code_verifier, redirect_uri and resource={api_url}/public (RFC 8707), so the token serves the public API only. The wait now really ends after 5 minutes.
  • Storage: one JSON entry oauth_tokens in the keychain; the owner-only file fallback is created with mode 600. A login replaces an older one: an older OAuth login is revoked, an older stored API key is removed. A stored API key of an older version still works until the next login.
  • Requests: OAuthBearerAuth (an httpx.Auth) refreshes 60 s before expiry and once on a 401, then retries the request once. A refreshed pair is saved. When another CLI process refreshed first, it uses the saved pair instead of a spent refresh token. A refused refresh asks to log in again.
  • Logout: posts the refresh token to the revocation endpoint (RFC 7009), then clears the local credentials. When the server does not confirm, it says so and exits with 1.
  • Messages: 401 and 403 no longer say "API key"; a 403 shows the server's reason.
  • Changelog: three entries under Unreleased.

Test plan

  • Unit tests pass (uv run pytest): 490 passed, 140 skipped (tests/test_integration.py needs an API key). tests/test_auth.py has 30 tests: discovery, the authorize URL, a full login against a real loopback server, refused callbacks (state, issuer, error), timeout, refresh, revocation, the bearer client, and the auth commands.
  • Linter passes (uv run ruff check ., uv run ruff format --check .)
  • Manually tested against the real API (see Artifacts)

Artifacts

The new code against the production OAuth server, with values that cannot sign anyone in: it reads the metadata, sends the authorize request of a login, and calls the token and revocation endpoints with unknown values. The full browser sign-in needs a person at the keyboard and is not shown.

$ uv run python probe.py   # discover(), build_authorization_url(), _post_form() of dualentry_cli.auth
issuer: https://api.dualentry.com/auth/oauth
token_endpoint: https://api.dualentry.com/auth/oauth/token/
revocation_endpoint: https://api.dualentry.com/auth/oauth/revoke/
authorize: 302 -> https://api.workos.com/user_management/authorize (error=none)
token (unknown code): 400 {'error': 'invalid_grant'}
refresh (unknown token): 400 {'error': 'invalid_grant'}
revoke (unknown token): 200 {}

The authorize request goes on to the sign-in page, with no error: the server accepts client_id=dualentry-cli, the 127.0.0.1 redirect on any port, S256 and the resource. The token endpoint reads the form and refuses only the unknown code.

$ uv run pytest -q tests/test_auth.py
..............................                                           [100%]
30 passed

Visual Proof

Not required — CLI only, no web UI.

Change-set: EOPS-635
Depends on: dualentry/backend#15565

Refs EOPS-635

auth login reads the authorization server metadata, signs in with PKCE
and a 127.0.0.1 loopback redirect for the dualentry-cli client, and keeps
the access and refresh tokens in the keychain. Requests send a bearer
token, refresh it before it expires and once more on a 401. X_API_KEY
from the environment still comes first. auth logout revokes the login
on the server.

Refs EOPS-635
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant