Skip to content

feat(oauth): Add one-time setup code grant for headless CLI login - #129325

Merged
evanpurkhiser merged 12 commits into
masterfrom
feat/oauth-setup-code-grant
Nov 4, 2026
Merged

evanpurkhiser merged 12 commits into
masterfrom
feat/oauth-setup-code-grant

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Adds a short-lived, single-use setup code that a signed-in user can paste into a headless install, for example curl https://cli.sentry.dev/install | SENTRY_SETUP_CODE=sntryc_... bash. The CLI exchanges the code at /oauth/token/ for an OAuth token. A headless machine can then log in without the device flow, which needs a browser round trip during the install.

This PR adds the setup code model and the token grant that redeems it. Setup codes stay off until a client ID is added to oauth.setup-code.allowed-client-ids. The endpoint that mints codes, the UI, and the CLI support will follow in separate PRs.

Add a session-authenticated endpoint that mints a short-lived, single-use
setup code, and a urn:sentry:params:oauth:grant-type:setup_code grant on
/oauth/token/ that exchanges it for a normal OAuth token. Minting revokes
the user's unredeemed codes. Gated by oauth.setup-code.allowed-client-ids.

Co-Authored-By: Evan Purkhiser <[email protected]>
Co-Authored-By: Burak Yigit Kaya <[email protected]>
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 30, 2026
…de-grant

# Conflicts:
#	migrations_lockfile.txt

Co-Authored-By: Evan Purkhiser <[email protected]>
@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

decision-model-linter

api

src/sentry/web/frontend/oauth_token.py

  • warn readsRequestBodyOutsideSerializer = TRUE (0.97): Request body values should be read from a serializer's validated_data. This file reads a value directly from request.data or request.POST.

decision-model-linter: 17 calls on 7 files, 40,788 input tokens, $0.0017, model typesafe/jev-1.13-20260917

@github-actions github-actions Bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Oct 30, 2026
The new setup-code mint endpoint lives in the control silo, so it must be
listed in controlsiloUrlPatterns.ts for frontend request routing.
test_generate_controlsilo_urls::test_no_missing_urls enforces this.
@linear-code

linear-code Bot commented Nov 2, 2026

Copy link
Copy Markdown

VDY-336

evanpurkhiser
evanpurkhiser previously approved these changes Nov 3, 2026

@evanpurkhiser evanpurkhiser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's clean this up jr

@evanpurkhiser
evanpurkhiser dismissed their stale review November 3, 2026 18:42

Accidentally approved

@evanpurkhiser evanpurkhiser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See my comments jr

Setup codes will be minted outside the API for specific clients, so drop
POST /api/0/auth/oauth/setup-code/ along with its URL, tests, and the
frontend URL entries. ApiSetupCode.mint() and the setup_code grant stay.
@github-actions github-actions Bot removed the Scope: Frontend Automatically applied to PRs that change frontend components label Nov 3, 2026
@evanpurkhiser
evanpurkhiser marked this pull request as ready for review November 4, 2026 18:42
@evanpurkhiser
evanpurkhiser requested review from a team as code owners November 4, 2026 18:42
@evanpurkhiser
evanpurkhiser requested review from a team as code owners November 4, 2026 18:42
Comment thread src/sentry/models/apisetupcode.py Resolved
Comment thread src/sentry/models/apisetupcode.py Outdated
"""
code = generate_setup_code()
with transaction.atomic(router.db_for_write(cls)):
cls.objects.filter(user_id=user_id).delete()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should this be filtered to the specific org?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I kept it per user on purpose. The requester asked that minting a code for a user revoke every earlier unredeemed code for that user, so each user has at most one live code. For user-scoped apps there's also no org to filter on (organization_id is None). In 4f1ea67 the database now enforces this too (see the unique-constraint thread).

Comment thread src/sentry/models/apisetupcode.py Resolved
@evanpurkhiser

Copy link
Copy Markdown
Member

Jr can we rebase this migration please

@evanpurkhiser evanpurkhiser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

needs migration rebase

sentry-junior Bot and others added 2 commits November 4, 2026 19:17
Minting already deletes a user's earlier codes. A unique constraint on
user makes the database enforce that, so concurrent mints can't leave
two live codes.

Co-Authored-By: Claude <[email protected]>
…de-grant

Rebase migration onto 1250_add_query_priority_column as 1251_add_apisetupcode.

Co-Authored-By: Claude <[email protected]>
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

This PR has a migration; here is the generated SQL for src/sentry/migrations/1251_add_apisetupcode.py

for 1251_add_apisetupcode in sentry

--
-- Create model ApiSetupCode
--
CREATE TABLE "sentry_apisetupcode" ("id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, "hashed_code" varchar(64) NOT NULL UNIQUE, "organization_id" bigint NULL, "scope_list" text[] NOT NULL, "expires_at" timestamp with time zone NOT NULL, "date_added" timestamp with time zone NOT NULL, "application_id" bigint NOT NULL, "user_id" bigint NOT NULL, CONSTRAINT "sentry_apisetupcode_user_uniq" UNIQUE ("user_id"));
ALTER TABLE "sentry_apisetupcode" ADD CONSTRAINT "sentry_apisetupcode_application_id_f084a6a5_fk_sentry_ap" FOREIGN KEY ("application_id") REFERENCES "sentry_apiapplication" ("id") DEFERRABLE INITIALLY DEFERRED NOT VALID;
ALTER TABLE "sentry_apisetupcode" VALIDATE CONSTRAINT "sentry_apisetupcode_application_id_f084a6a5_fk_sentry_ap";
ALTER TABLE "sentry_apisetupcode" ADD CONSTRAINT "sentry_apisetupcode_user_id_b92da6ff_fk_auth_user_id" FOREIGN KEY ("user_id") REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED NOT VALID;
ALTER TABLE "sentry_apisetupcode" VALIDATE CONSTRAINT "sentry_apisetupcode_user_id_b92da6ff_fk_auth_user_id";
CREATE INDEX CONCURRENTLY "sentry_apisetupcode_hashed_code_85d52b3b_like" ON "sentry_apisetupcode" ("hashed_code" varchar_pattern_ops);
CREATE INDEX CONCURRENTLY "sentry_apisetupcode_organization_id_c4100783" ON "sentry_apisetupcode" ("organization_id");
CREATE INDEX CONCURRENTLY "sentry_apisetupcode_expires_at_4086b007" ON "sentry_apisetupcode" ("expires_at");
CREATE INDEX CONCURRENTLY "sentry_apisetupcode_application_id_f084a6a5" ON "sentry_apisetupcode" ("application_id");

Comment thread src/sentry/models/apisetupcode.py Resolved
Comment thread src/sentry/web/frontend/oauth_token.py Resolved
…exchange log

Two concurrent mints for the same user could both delete nothing and then
collide on the per-user unique constraint. Retry the delete+create so the
newest mint wins instead of raising IntegrityError.

Drop the client IP from the oauth.setup-code-exchanged log to match the
device-code exchange log.

Co-Authored-By: Claude <[email protected]>
evanpurkhiser added a commit to getsentry/toolkit that referenced this pull request Nov 4, 2026
The CLI needs to redeem Sentry's short-lived setup codes without opening
a browser. Add the setup-code OAuth exchange, validate the issuing
instance, and redact setup codes from diagnostics. Redemption sends the
code once and refuses redirects.

Backend dependency: getsentry/sentry#129325.
Codes are generated internally by Sentry for specific clients; the
generated onboarding instructions flow follows separately.

https://linear.app/getsentry/issue/VDY-337

Validation on the complete stack: typing, lint, and focused
authentication/setup tests pass. The full test run has 21 unrelated
failures reproduced on unchanged upstream main in the installer
musl-detection, time-range, and zip-writer suites.

Native GitHub Stack:
[#1874](https://github.com/getsentry/toolkit/stack/1874), ordered #1870
→ #1871 → #1872 → #1873.

Native bootstrap and shell installation passed against local OAuth
redemption using internally generated codes. The shell test used a local
artifact mirror for the unreleased binary.
@evanpurkhiser
evanpurkhiser self-requested a review November 4, 2026 20:11
@evanpurkhiser
evanpurkhiser merged commit 790e459 into master Nov 4, 2026
96 of 98 checks passed
@evanpurkhiser
evanpurkhiser deleted the feat/oauth-setup-code-grant branch November 4, 2026 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants