Repository navigation
feat(oauth): Add one-time setup code grant for headless CLI login - #129325
Conversation
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]>
…de-grant # Conflicts: # migrations_lockfile.txt Co-Authored-By: Evan Purkhiser <[email protected]>
decision-model-linterapi
decision-model-linter: 17 calls on 7 files, 40,788 input tokens, $0.0017, model typesafe/jev-1.13-20260917 |
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.
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.
…de-grant # Conflicts: # migrations_lockfile.txt
| """ | ||
| code = generate_setup_code() | ||
| with transaction.atomic(router.db_for_write(cls)): | ||
| cls.objects.filter(user_id=user_id).delete() |
There was a problem hiding this comment.
Should this be filtered to the specific org?
There was a problem hiding this comment.
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).
|
Jr can we rebase this migration please |
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]>
|
This PR has a migration; here is the generated SQL for for --
-- 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"); |
…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]>
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.
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.