Skip to content

feat(auth): add site transfer token scopes - #3339

Merged
ascorbic merged 3 commits into
mainfrom
transfer/01-auth-scopes
Sep 24, 2026
Merged

ascorbic merged 3 commits into
mainfrom
transfer/01-auth-scopes

Conversation

@ascorbic

@ascorbic ascorbic commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Part 1 of 8 of the site transfer stack; based on main. (The redirect pattern compiler hardening it once sat on now ships in #3326.) Site transfer exports a whole EmDash site as a portable .emdash package and imports it into an empty site. Design and approval: RFC #3240.

This layer adds the authorization pieces the rest of the stack checks against. Nothing calls a transfer route yet.

  • @emdash-cms/auth: new token scopes transfer:export, transfer:analyze, and transfer:execute, and admin-only RBAC permissions transfer:export and transfer:import. admin grants all three scopes. Each transfer scope grants only itself, so a token (an agent's, say) can be limited to one transfer action. Scope clamping drops them for every role below admin. Exports TRANSFER_SCOPES and isTransferScope().
  • Token scope rules (astro/middleware/auth.ts): a rule can now name a list of scopes, satisfied by holding any one of them. /_emdash/api/admin/transfer requires any transfer scope. The rule sits before the generic /_emdash/api/admin rule, so a token that holds only transfer:analyze reaches the route, and the route then checks the specific scope it needs.
  • requireAnyScope() / requireAnyPerm(): any-of counterparts of requireScope() / requirePerm(), used by the transfer routes in part 4.
  • locals.tokenId: resolving a bearer token now also returns an id for the credential. For a PAT it is the token row id. For an OAuth token it is a one-way id of the grant: it stays the same across access-token refreshes, and it is never a token hash or lookup key. Transfer approvals (parts 4 and 6) bind to it.
  • OAuth consent labels and the admin API token scope picker list the three scopes. The admin change adds three entries to the existing scope list. No layout changes.

Key decisions for review:

  • The RFC left open whether admin should imply the transfer scopes. We settled on yes: existing admin tokens and emdash login credentials can use transfer without re-issuing. The narrow scopes exist for delegating less than admin.
  • The OAuth tokenId hashes the stored refresh-token hash again, so what gets persisted in approval rows cannot be used to look up a token.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Do not include messages.po changes except in translation PRs — a workflow extracts catalogs on merge to main.
  • I have added and reviewed the user-facing changeset (if this PR changes a published package) — n/a in this layer: release notes for the whole feature are in part 8
  • New features link to an approved Discussion: RFC: Portable EmDash site export and import #3240
  • I have included screenshots below if this PR changes the UI — the only UI change is three entries added to the existing API token scope list

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Opus 5.5

Screenshots / test output

Not applicable. The admin change adds three entries (Site Export, Site Import Analysis, Site Import) to the API token scope list. The labels and descriptions go through Lingui msg, and catalogs are extracted after merge.

Verified on this branch:

  • Root pnpm build, pnpm typecheck, and pnpm --silent lint:json | jq '.diagnostics | length' → 0
  • oxfmt --check on every touched file
  • packages/auth tests: 97 passed
  • Core unit and integration suite: 605 files, 7378 tests passed. New tests cover the transfer scope rule, requireAnyScope, OAuth consent labels, tokenId for PATs and OAuth grants (stable across refresh, never a token hash), and device-flow clamping of transfer scopes.
  • Admin tests: 181 files, 2470 tests passed

@changeset-bot

changeset-bot Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b2db3ef

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
@emdash-cms/admin Minor
emdash Minor
@emdash-cms/cloudflare Minor
@emdash-cms/plugin-test Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/auth Minor
@emdash-cms/blocks Minor
create-emdash Minor
@emdash-cms/gutenberg-to-portable-text Minor
@emdash-cms/x402 Minor
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@3339

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@3339

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@3339

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@3339

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@3339

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@3339

emdash

npm i https://pkg.pr.new/emdash@3339

create-emdash

npm i https://pkg.pr.new/create-emdash@3339

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@3339

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@3339

@emdash-cms/plugin-test

npm i https://pkg.pr.new/@emdash-cms/plugin-test@3339

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@3339

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@3339

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@3339

@emdash-cms/registry-loader

npm i https://pkg.pr.new/@emdash-cms/registry-loader@3339

@emdash-cms/registry-moderation

npm i https://pkg.pr.new/@emdash-cms/registry-moderation@3339

@emdash-cms/registry-verification

npm i https://pkg.pr.new/@emdash-cms/registry-verification@3339

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@3339

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@3339

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@3339

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@3339

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@3339

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@3339

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@3339

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@3339

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@3339

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@3339

commit: b2db3ef

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview URL: https://transfer-01-auth-scopes.try.emdashcms.com, https://transfer-01-auth-scopes-emdash-playground.emdash-cms.workers.dev (commit b2db3ef)

This URL reflects your latest Preview deployment

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://c0b300bf.try.emdashcms.com, https://c0b300bf-emdash-playground.emdash-cms.workers.dev b2db3ef 2026-09-24T06:59:40.726Z Visit the dashboard ↗
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://b0a01ae5.try.emdashcms.com, https://b0a01ae5-emdash-playground.emdash-cms.workers.dev 7775201 2026-09-23T17:15:08.429Z Visit the dashboard ↗

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
docs 7775201 Sep 23 2026, 05:12 PM

Adds the transfer:export, transfer:analyze, and transfer:execute token
scopes and the admin-only transfer:export and transfer:import
permissions that the site transfer feature authorizes against.

The admin scope grants all three transfer scopes. Each transfer scope
grants only itself, so a token (for example an agent's) can be limited
to a single transfer action. Scope clamping drops them for every role
below admin. TRANSFER_SCOPES and isTransferScope() identify them.
Wires the transfer scopes into core's token handling ahead of the site
transfer API:

- Token scope rules accept a list of scopes satisfied by holding any
  one of them. /_emdash/api/admin/transfer requires any transfer scope
  and precedes the generic admin rule, so a token holding only one
  transfer scope reaches the route, which then checks its specific one.
- requireAnyScope() and requireAnyPerm() are the any-of counterparts of
  requireScope() and requirePerm().
- Resolving a bearer token now also returns tokenId, exposed as
  locals.tokenId: the row id for a personal access token, and for an
  OAuth token a one-way id of its grant that stays stable across
  refreshes and is never a token lookup key. Transfer approvals bind
  to it.
- The OAuth consent page labels the transfer scopes.
Lists Site Export, Site Import Analysis, and Site Import in the API
token scope picker, so an admin can give a token narrower transfer
access than Admin, which includes all three.
@ascorbic
ascorbic force-pushed the transfer/01-auth-scopes branch from 7775201 to b2db3ef Compare September 24, 2026 06:53
@ascorbic
ascorbic removed this pull request from stack #3347 September 24, 2026 06:54
@ascorbic
ascorbic changed the base branch from fix/redirect-pattern-literal-escaping to main September 24, 2026 06:55
@ascorbic
ascorbic added this pull request to stack #3378 September 24, 2026 06:55
@ascorbic
ascorbic merged commit 8579a6a into main Sep 24, 2026
51 checks passed
@ascorbic
ascorbic deleted the transfer/01-auth-scopes branch September 24, 2026 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant