You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(auth): sign in command-line tools from the console - #351
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:
Approved:
Opened without a code:
Expired or used code:
390px:
An admin in several organizations picks the organization first (projects reload, the code stays):
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.
- 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.
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
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.
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.
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.
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.
- 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.
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.
- 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.
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.
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.
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
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.
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)api_keyauth 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.createon the organization, project in the admin's organization). A refused approval does not use up the code./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:
Approved:
Opened without a code:
Expired or used code:
390px:
An admin in several organizations picks the organization first (projects reload, the code stays):
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 buildboth 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 calledGET /api/admin/auth/keywith it.