skillhub/cli/test/integration/token-capabilities.test.ts
小妍儿 ✨ 3ee80f996b
feat(auth): split API token skill capabilities (#1049) (#1078)
* docs(openspec): propose split API token skill capabilities

Define five token capabilities (read, submit, publish-private, manage,
delete), the route mapping, combined-operation limits, token minting
limits, legacy token and CLI compatibility, and negative test design.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <xiaoyaner0201@users.noreply.github.com>

* docs(openspec): clarify token compatibility and bundle snapshot boundaries

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* docs(auth): inventory Bundle authorization provenance

Record persisted evidence and fail-closed recovery rules before implementing snapshots.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* feat(auth): split token capabilities and classify route policies

Preserve Suite deletion authorization and verify legacy route compatibility against the original ordered catalog.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* feat(auth): enforce token ceilings on combined skill operations

Persist Bundle initiator capability snapshots and fail closed on unknown historical authority during recovery and retry.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* feat(auth): bound token issuance and expose effective capabilities

Validate requested scopes and expiry before rotation or extension. Expose capability discovery and machine-readable authorization errors without broadening token privileges.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* feat(tokens): add Web capability presets and CLI authorization guidance

Issue candidate-only login tokens, display effective capabilities and legacy compatibility, and require explicit delete authorization without automatic reissuance. Verify real HTTP candidate and legacy publication flows.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* docs(auth): document token capabilities and upgrade boundaries

Describe bounded issuance, compatibility aliases, CLI delete authorization, administrator upload changes, fail-closed Bundle recovery and the omitted-scopes deprecation schedule.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* docs(auth): align capability action boundaries

Document manage authorization for review withdrawal and keep DTO imports formatted with backend conventions.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* fix(publish): limit visibility ceiling check to token calls

Session uploads keep their previous path, and a missing locked row no longer raises a null pointer before the existing visibility checks run.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* fix(auth): preserve rerelease ceilings and explain bundle blocks

Carry token capabilities through rerelease and resolve visibility under the publication lock. Localize permanent Bundle authorization failures and hide ineffective retry actions.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* fix(auth): classify main's public routes and renumber bundle snapshot migration

main added public GET site-presentation routes and its own V122 migration. Record the new routes in the catalog baseline as unchanged public routes and move the Bundle authorization snapshot to V123.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* test(site): pin presentation upgrade check to V122

The upgrade test counted every migration after V121; with the Bundle snapshot now at V123 it saw two. Target V122 so it keeps checking only the presentation seed.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* fix(auth): compare token expiry ceilings at stored precision

Token expiry is stored at microsecond precision. On Linux CI Instant.now() carries nanoseconds, so a child token asking for exactly its parent's expiry compared as later and was rejected. Truncate requested expiry to microseconds before the ceiling check.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* fix(auth): renumber bundle snapshot migration after main's V123

main 22409aba added V123__homepage_recommendations. Move the Bundle authorization snapshot to V124 and record main's public site-recommendations routes in the token catalog baseline as unchanged public routes.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* test(auth): keep built-in skill sync out of token flow side-effect checks

Since main 22409aba the built-in skill sync can publish after startup, asynchronously, and its storage writes landed inside the 'denied rerelease writes nothing' assertion on CI. Disable built-in sync for this test class, as other integration tests already do.

Refs #1049

Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>

* fix(auth): block bundle recovery with unknown authorization

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
(cherry picked from commit cb8fb05d77d4b455032cbc2d3a1fdc2dd7bce894)
Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>

* feat(web): redesign token credentials and compact permission controls

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
(cherry picked from commit acd3615ee57a4bb432980601bb883a1a86906d1a)
Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>

* chore(web): regenerate API types from integrated backend

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>

* fix(web): complete token locales and static analytics markers

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>

* test(auth): preserve operation-level bundle authorization errors

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>

---------

Signed-off-by: 千乘妍 (Xiaoyaner) <xiaoyaner0201@users.noreply.github.com>
Signed-off-by: 千乘妍 (Xiaoyaner) <258399167+xiaoyaner0201@users.noreply.github.com>
Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
Co-authored-by: 千乘妍 (Xiaoyaner) <xiaoyaner0201@users.noreply.github.com>
Co-authored-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
2026-11-16 11:02:38 +08:00

72 lines
3.5 KiB
TypeScript

import { afterEach, describe, expect, test } from 'bun:test'
import { readFile } from 'node:fs/promises'
import { runCli } from '../helpers/run-cli'
import { createTempHome } from '../helpers/temp-env'
let server: ReturnType<typeof Bun.serve> | undefined
afterEach(() => {
server?.stop()
server = undefined
})
describe('token capability CLI compatibility', () => {
test('whoami exposes capabilities in human and JSON output', async () => {
const env = await createTempHome()
const identity = {
handle: 'candidate', displayName: 'Candidate', capabilities: ['skill:read', 'skill:submit'],
legacyCompat: false, expiresAt: '2027-01-01T00:00:00Z'
}
server = Bun.serve({ port: 0, hostname: '127.0.0.1', fetch: () => Response.json({ data: identity }) })
const args = ['whoami', '--registry', `http://127.0.0.1:${server.port}`, '--token', 'test-token']
const human = await runCli(args, { HOME: env.home, USERPROFILE: env.home })
expect(human.exitCode).toBe(0)
expect(human.stdout).toContain('Capabilities: skill:read, skill:submit')
const json = await runCli([...args, '--json'], { HOME: env.home, USERPROFILE: env.home })
expect(JSON.parse(json.stdout)).toMatchObject(identity)
})
test('localized missing-delete error provides explicit authorization without token reissuance', async () => {
const env = await createTempHome()
const paths: string[] = []
server = Bun.serve({
port: 0, hostname: '127.0.0.1',
fetch(req) {
paths.push(`${req.method} ${new URL(req.url).pathname}`)
if (req.method === 'DELETE') {
return Response.json({
code: 403, msg: '缺少删除能力', errorCode: 'error.apiToken.scope.missing',
errorArgs: ['skill:delete'], requestId: 'capability-test'
}, { status: 403 })
}
return Response.json({ data: { handle: 'candidate', displayName: 'Candidate' } })
}
})
const registry = `http://127.0.0.1:${server.port}`
const runtime = { HOME: env.home, USERPROFILE: env.home }
await runCli(['login', '--registry', registry, '--token', 'test-token'], runtime)
const before = await readFile(`${env.home}/.skillhub/credentials.json`, 'utf8')
for (const output of [[], ['--json']]) {
const denied = await runCli(['remove', 'sample', '--remote', '--hard', '--registry', registry, ...output], runtime)
expect(denied.exitCode).toBe(2)
expect(denied.stdout).toBe('')
expect(denied.stderr).toContain('skill:delete')
expect(denied.stderr).toContain('skillhub login --token')
if (output.length) expect(JSON.parse(denied.stderr).details.errorCode).toBe('error.apiToken.scope.missing')
}
expect(await readFile(`${env.home}/.skillhub/credentials.json`, 'utf8')).toBe(before)
expect(paths.filter(path => path.startsWith('POST'))).toEqual([])
expect(paths.filter(path => path.startsWith('DELETE'))).toHaveLength(2)
})
test('unrelated denials do not claim missing delete capability', async () => {
const env = await createTempHome()
server = Bun.serve({ port: 0, hostname: '127.0.0.1', fetch: () => Response.json({
msg: 'not the owner', errorCode: 'other.reason', errorArgs: ['skill:delete']
}, { status: 403 }) })
const result = await runCli([
'remove', 'sample', '--remote', '--hard', '--registry', `http://127.0.0.1:${server.port}`, '--token', 'test-token'
], { HOME: env.home, USERPROFILE: env.home })
expect(result.stderr).not.toContain('Explicit authorization')
})
})