Skip to content

feat(auth): sign in command-line tools from the console - #351

Open
jeroenrinzema wants to merge 6 commits into
mainfrom
feat/cli-login
Open

jeroenrinzema wants to merge 6 commits into
mainfrom
feat/cli-login

Conversation

@jeroenrinzema

@jeroenrinzema jeroenrinzema commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Why: a command-line tool (luno, the campaign/journey editor) has to be handed a project-scoped API key today: create it in Settings > Access, copy the key and the project ID, paste both into the terminal. API keys can't list projects, and a key only works on its own project, so the tool can't offer a choice either.

How: a device-authorization flow in the shape of RFC 8628.

sequenceDiagram
    participant T as Tool
    participant A as API
    participant C as Console /cli
    T->>A: POST /api/auth/cli {client_name}
    A-->>T: user_code, verification_url, device_token, interval
    T->>C: opens verification_url, shows user_code
    C->>A: GET /api/admin/cli-logins/{code}
    C->>A: POST /api/admin/cli-logins/{code}/approve {project_id}
    T->>A: POST /api/auth/cli/token {device_token} (every interval)
    A-->>T: approved + editor key for that project (once)
Loading
  • The login lives in Redis for ten minutes (same pattern as the OIDC flow store). The user code is taken with GETDEL, so it's single-use; the poll that collects an approval takes the login with GETDEL too, so the key is handed out once.
  • The key is minted at that poll, not at approval: no plaintext secret ever waits in Redis, and an approval nobody collects leaves no key behind. It is an ordinary api_key auth method with the editor role, named after the tool (client_name), with the approving admin in its description, so it shows up in Settings > Access and can be revoked there.
  • Approving takes the same check as creating a key by hand (create on the organization, project in the admin's organization). A refused approval does not use up the code.
  • The start endpoint is unauthenticated and claims a per-IP budget (20/hour) on top of the global rate limit. Without Redis every endpoint answers 404, so a tool can fall back to asking for a key.
  • Console: /cli?code=… sits inside the authenticated shell, so a signed-out visitor goes through login and comes back with the code. Nothing is redeemed on render; approving and declining are clicks.

Scope: management API (5 operations), console page, docs (Settings > Access). Non-goals: letting project editors (not org admins) approve, and other roles than editor for the minted key.

Screenshots

Code in the link, project picked:

ready

Approved:

approved

Opened without a code:

enter

Expired or used code:

expired

390px:

ready narrow

An admin in several organizations picks the organization first (projects reload, the code stays):

organizations

switched

organizations narrow

Tested: go test ./internal/http/controllers/v1/management -run 'CLILogin|NormalizeCLI' (approve → key works and is editor → second poll 404; decline; member can't approve and the code survives; other organization's project 404; API key actors refused; no Redis → 404), pnpm vitest run src/views/cli, go build both editions, golangci-lint on the package, tsc, ESLint. End to end against a local server: started a login with curl, approved it in the console, polled the key and called GET /api/admin/auth/key with it.

A command-line tool starts a login (POST /api/auth/cli), shows a short code and
opens /cli in the console. A signed-in admin compares the code, picks a project
and approves; the tool's next poll (POST /api/auth/cli/token) mints an editor
API key for that project and hands it over once. Shaped after the OAuth 2.0
device authorization grant (RFC 8628).

- Logins live in Redis for ten minutes; the user code is single-use (GETDEL) and
  the key is minted at the poll, so no plaintext secret waits in Redis.
- Approving needs the permission creating a key by hand needs.
- Without Redis the endpoints answer 404.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Changing URL codes during loading can cause an admin to approve a different tool than the one displayed, creating a critical authorization mismatch.

4 open issues
What changed in this PR

Adds console-approved sign-in for command-line tools, providing an alternative to manually copying project API keys into a terminal.

Changes:

  • Adds a Redis-backed login flow that delivers a project-scoped editor key once after approval.
  • Introduces an authenticated /cli page for code confirmation, project selection, approval, and decline.
  • Updates API bindings, tests, English translations, and access documentation.
File Description
internal/​http/​controllers/​v1/​management/​oapi/​resources.yml Defines five CLI login operations and their schemas.
internal/​http/​controllers/​v1/​management/​oapi/​resources_gen.go Adds generated Go models, clients, and routing.
internal/​http/​controllers/​v1/​management/​controller.go Registers the CLI login controller.
internal/​http/​controllers/​v1/​management/​cli_login.go Implements login storage, decisions, polling, and key creation.
internal/​http/​controllers/​v1/​management/​cli_login_test.go Tests permissions, decisions, and one-time key delivery.
docs/​content/​docs/​settings/​access.mdx Documents command-line login and key revocation.
console/​src/​views/​router.tsx Adds the authenticated /cli route.
console/​src/​views/​cli/​CliLogin.tsx Implements the login approval interface.
console/​src/​views/​cli/​CliLogin.test.tsx Tests code entry and approval-page states.
console/​src/​oapi/​management.generated.ts Adds generated TypeScript API definitions.
console/​public/​locales/​en.json Adds English text for CLI login states and actions.

Review effort: Balanced

Comment thread console/src/views/cli/CliLogin.tsx Resolved
Comment thread console/src/views/cli/CliLogin.tsx Resolved
Comment thread console/src/views/cli/CliLogin.tsx Resolved
- A decision and spending the user code are one Redis transaction (WATCH/MULTI),
  so a failed or cancelled approval leaves the login to decide again.
- The console approves or declines the login on screen, not whatever code the
  URL holds by then, and ignores a slower load for a code it moved away from.
- A network failure shows Retry instead of a spinner forever.
- The project picker reads every page of projects, not only the first 100.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The CLI login picker hides the active organization and offers no organization switching, preventing multi-organization admins from selecting other organizations’ projects and making identically named approval targets ambiguous.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Project picker hides organization context and switching

console/​src/​views/​cli/​CliLogin.tsx:268

For admins in multiple organizations, this picker lists only the active organization's projects (projects.go:105–108), but /cli neither identifies that organization nor includes the workspace switcher. They cannot choose another organization's project here, and matching project names can make the approval target ambiguous. Show the active organization and offer switching that preserves the login code, reloads projects, and clears the previous project selection.

4 resolved since last review

Review effort: Balanced

The project list is the active organization's. An admin in several now sees
which one and can switch there: the code stays, the projects reload and the
previous project selection is cleared.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The login flow can irreversibly consume an approval before credential creation succeeds, preventing retries, and organization switching leaves stale cached workspace state that can cause navigation failures.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

High severity Preserve approved login for retries when key provisioning fails

internal/​http/​controllers/​v1/​management/​cli_login.go:307

The approved login is permanently deleted before key creation succeeds. If the project lookup, database insert, or RBAC provisioning then fails transiently, this poll returns 500 but every retry returns 404, even though no key was delivered and the login still had time remaining. Preserve a retryable approved state on failures before credential creation (for example, claim it with a bounded processing lease and finalize consumption after successful provisioning), while retaining the concurrent-poll single-issuance guarantee. Simply moving GETDEL after minting would allow duplicate keys.

Review effort: Balanced

Comment thread console/src/views/cli/CliLogin.tsx Resolved
- The poll that collects an approval claims it with a short lease, creates the
  key, and only then ends the login; a failed key releases the claim so the
  tool's next poll tries again. A concurrent poll is told to keep waiting.
- Switching organization on /cli reloads the page, like the workspace switcher,
  so the rest of the console doesn't keep the previous organization's state.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

CLI login can issue multiple usable credentials from one approval after completion failures or lease expiry, while unconditional cleanup can delete another poll’s claim.

2 open issues
1 resolved since last review

Review effort: Balanced

- The claim on an approved login carries a token; releasing and finishing it
  check the token, so a poll that outlived its lease can't touch the next one's.
- The key goes out only after the login is recorded as finished. A poll whose
  lease ran out, or whose completion failed, withdraws the key it made instead,
  so one approval yields at most one usable key.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Ignored response-write errors can leave an active editor key inaccessible after the login is permanently consumed, causing retries to return 404.

1 open issue
2 resolved since last review

Review effort: Balanced

The login is finished before the key goes out, so a tool that hung up during
the minting, or a failed write, would leave a usable key nobody holds. Both are
now detected and the key is withdrawn.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because interrupted credential issuance can leave an active, undisclosed key and allow duplicate issuance after the claim expires.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
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.

2 participants