Files
zeekay 19fdf55ceb feat(auth): one key scheme, minted and read in one place
A key now opens with what it is allowed to touch. `pk-` is publishable and
safe in a browser; `sk-` is secret and never leaves a server. The five marks
it replaces all began `ph` -- the upstream's initials -- and they were the
last of its brand a customer read back to us, in every SDK snippet. No word
rule could reach them, because `ph` is not a word.

The marks were also not telling the truth. The constant said `phc_` while
every live project token said `hi_`, so the middleware that routes on the
constant matched no real token at all. Two spellings existed and one was
fiction. Minting now happens in exactly one function and reading in exactly
one, so a second spelling has nowhere to live.

`phx_` (personal) and `phs_` (project secret) both become `sk-`. The mark
now says only that a value is secret; which store holds it is settled by
asking. That was already how personal keys worked -- keys minted before the
marks carry none, so the store always decided -- and both secret resolvers
already returned None on a miss and fell through, so one shared extractor
replaces the two that could drift apart.

OAuth's access and refresh tokens stay distinguishable as `at-` and `rt-`,
because the parser tells them apart before either is looked up, and the
domain-verification token becomes `vt-` rather than keeping a sixth mint of
its own.

`bin/debrand --check` now refuses the marks as a shape, so a seventh cannot
be coined. Two rules that were about to rename things that are not ours to
spell are bounded: `Lemon` no longer eats `LemonSqueezy`, and the `hog_flow`
rules no longer reach a Postgres table, a Datastore table or a route.

Keys are stored hashed, so a mark cannot be rewritten in place: the three
live project tokens have to be reminted.
2026-08-08 19:42:44 -07:00

63 lines
2.8 KiB
TypeScript

import { randomString } from '../utils'
import { PlaywrightWorkspaceSetupResult, expect, test } from '../utils/workspace-test-base'
test.describe('Personal API keys', () => {
let workspace: PlaywrightWorkspaceSetupResult | null = null
test.beforeAll(async ({ playwrightSetup }) => {
workspace = await playwrightSetup.createWorkspace({ skip_onboarding: true, no_demo_data: true })
})
test.beforeEach(async ({ page, playwrightSetup }) => {
await playwrightSetup.login(page, workspace!)
})
test('key created in the UI authorizes the API and stops working after deletion', async ({ page, request }) => {
const label = randomString('e2e-key')
let apiKey = ''
await test.step('create a key with the all-access preset', async () => {
await page.goto('/settings/user-api-keys')
await page.getByRole('button', { name: 'Create personal API key' }).click()
await page.getByPlaceholder('For example "Reports bot" or "Zapier"').fill(label)
await page.getByRole('button', { name: 'All access', exact: true }).click()
await page.getByText('Select preset').click()
await page.locator('.Popover__content').getByText('All access').click()
await page.getByRole('button', { name: 'Create key' }).click()
})
await test.step('capture the one-time key value', async () => {
const dialog = page.locator('.Modal').filter({ hasText: 'Personal API key ready' })
await expect(dialog).toBeVisible()
const snippet = (await dialog.locator('code').textContent()) ?? ''
apiKey = snippet.match(/sk-\w+/)?.[0] ?? ''
expect(apiKey).toMatch(/^sk-/)
await page.keyboard.press('Escape')
await expect(dialog).not.toBeVisible()
})
await test.step('the key authorizes an API request', async () => {
const resp = await request.get('/v1/users/@me/', {
headers: { Authorization: `Bearer ${apiKey}` },
})
expect(resp.status()).toBe(200)
expect((await resp.json()).email).toBe(workspace!.user_email)
})
await test.step('delete the key via the row menu', async () => {
const row = page.locator('tr', { hasText: label })
await row.locator('td').last().getByRole('button').click()
await page.getByRole('menuitem', { name: 'Delete' }).click()
await page.getByRole('button', { name: 'Permanently delete' }).click()
await expect(row).not.toBeVisible()
})
await test.step('the deleted key no longer authorizes', async () => {
const resp = await request.get('/v1/users/@me/', {
headers: { Authorization: `Bearer ${apiKey}` },
})
expect(resp.status()).toBe(401)
})
})
})