mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 21:09:27 +00:00
feat(api-token): store API tokens as SHA-256 hashes (#492)
New tokens are 32 random bytes with a shumai_ prefix and are returned only once at creation. The list endpoint returns a 4-character hint instead of the plaintext. A migration hashes existing tokens in place so they keep authenticating.
This commit is contained in:
@@ -493,27 +493,27 @@ describe('team api', () => {
|
||||
vi.mocked(apiTokenService.listTokens).mockResolvedValue([
|
||||
{
|
||||
id: 'token1',
|
||||
token: 'ulid1',
|
||||
tokenHint: 'a1b2',
|
||||
name: 'cli-key',
|
||||
userId: 'user1',
|
||||
createdAt: mockDate,
|
||||
updatedAt: mockDate,
|
||||
},
|
||||
// Mocking return value of listTokens is simpler with any casting
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
] as any)
|
||||
])
|
||||
|
||||
const res = await app.request('/teams/t1/api-tokens')
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data).toHaveLength(1)
|
||||
// The plaintext token is never returned when listing
|
||||
expect(data[0]).toEqual({
|
||||
id: 'token1',
|
||||
token: 'ulid1',
|
||||
tokenHint: 'a1b2',
|
||||
name: 'cli-key',
|
||||
createdAt: '2026-06-21T00:00:00.000Z',
|
||||
})
|
||||
expect(data[0]).not.toHaveProperty('token')
|
||||
expect(apiTokenService.listTokens).toHaveBeenCalledWith('user1')
|
||||
})
|
||||
|
||||
@@ -521,15 +521,16 @@ describe('team api', () => {
|
||||
const { apiTokenService } = await import('@shumai/core/src/user/api-token')
|
||||
const mockDate = new Date('2026-06-21T00:00:00.000Z')
|
||||
vi.mocked(apiTokenService.createToken).mockResolvedValue({
|
||||
id: 'token1',
|
||||
token: 'ulid1',
|
||||
name: 'cli-key',
|
||||
userId: 'user1',
|
||||
createdAt: mockDate,
|
||||
updatedAt: mockDate,
|
||||
// Mocking return value of createToken is simpler with any casting
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any)
|
||||
record: {
|
||||
id: 'token1',
|
||||
tokenHint: 'a1b2',
|
||||
name: 'cli-key',
|
||||
userId: 'user1',
|
||||
createdAt: mockDate,
|
||||
updatedAt: mockDate,
|
||||
},
|
||||
token: 'shumai_plaintext-a1b2',
|
||||
})
|
||||
|
||||
const res = await app.request('/teams/t1/api-tokens', {
|
||||
method: 'POST',
|
||||
@@ -539,9 +540,11 @@ describe('team api', () => {
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
// The plaintext is returned exactly once, at creation
|
||||
expect(data).toEqual({
|
||||
id: 'token1',
|
||||
token: 'ulid1',
|
||||
token: 'shumai_plaintext-a1b2',
|
||||
tokenHint: 'a1b2',
|
||||
name: 'cli-key',
|
||||
createdAt: '2026-06-21T00:00:00.000Z',
|
||||
})
|
||||
|
||||
@@ -24,6 +24,7 @@ import {
|
||||
getTeamAiUsageQuerySchema,
|
||||
AuditAction,
|
||||
} from '@shumai/dtos'
|
||||
import type { ApiTokenResponse, CreateApiTokenResponse } from '@shumai/dtos'
|
||||
import { apiTokenService } from '@shumai/core/src/user/api-token'
|
||||
import { aiUsageService } from '@shumai/core/src/ai-usage/ai-usage'
|
||||
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
||||
@@ -433,10 +434,10 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
|
||||
const tokens = await apiTokenService.listTokens(user.id)
|
||||
return c.json(
|
||||
tokens.map((t) => ({
|
||||
tokens.map((t): ApiTokenResponse => ({
|
||||
id: t.id,
|
||||
token: t.token,
|
||||
name: t.name,
|
||||
tokenHint: t.tokenHint,
|
||||
createdAt: t.createdAt.toISOString(),
|
||||
})),
|
||||
)
|
||||
@@ -453,13 +454,15 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
id: teamId,
|
||||
})
|
||||
|
||||
const token = await apiTokenService.createToken(user.id, req.name)
|
||||
return c.json({
|
||||
id: token.id,
|
||||
token: token.token,
|
||||
name: token.name,
|
||||
createdAt: token.createdAt.toISOString(),
|
||||
})
|
||||
const { record, token } = await apiTokenService.createToken(user.id, req.name)
|
||||
const response: CreateApiTokenResponse = {
|
||||
id: record.id,
|
||||
name: record.name,
|
||||
tokenHint: record.tokenHint,
|
||||
createdAt: record.createdAt.toISOString(),
|
||||
token,
|
||||
}
|
||||
return c.json(response)
|
||||
})
|
||||
.delete('/teams/:teamId/api-tokens/:tokenId', async (c) => {
|
||||
const user = c.get('user')
|
||||
|
||||
Reference in New Issue
Block a user