修复店务前端请求签名

This commit is contained in:
2026-07-01 13:03:29 +08:00
parent 9f1d2dec77
commit 5756d14fbd
2 changed files with 92 additions and 13 deletions
@@ -54,10 +54,40 @@ describe('request signature', () => {
const canonical = [
'POST',
'/admin/manager/planning/appointments',
'a=1&b=2',
payloadHash,
'1789999999000',
'1789999999000-00000000-0000-4000-8000-000000000001-ak'
'1789999999000-00000000-0000-4000-8000-000000000001-ak',
payloadHash
].join('\n')
const expectedSignature = createHmac('sha256', 'token-secret').update(canonical).digest('hex')
expect(config.headers['X-Signature']).toBe(expectedSignature)
})
it('hashes canonical parameters as payload when unsafe request has no json body', async () => {
vi.spyOn(Date, 'now').mockReturnValue(1_789_999_999_000)
vi.stubGlobal('crypto', {
randomUUID: () => '00000000-0000-4000-8000-000000000001',
getRandomValues: (array: Uint32Array) => {
array[0] = 10
array[1] = 20
return array
}
})
const config = await attachRequestSignature({
headers: {},
method: 'post',
baseURL: '/admin/api',
url: '/manager/example',
params: { b: 2, a: 1 },
data: undefined
} as InternalAxiosRequestConfig, 'token-secret', { authFree: false })
const payloadHash = createHash('sha256').update('a=1&b=2').digest('hex')
const canonical = [
'POST',
'/admin/manager/example',
'1789999999000',
'1789999999000-00000000-0000-4000-8000-000000000001-ak',
payloadHash
].join('\n')
const expectedSignature = createHmac('sha256', 'token-secret').update(canonical).digest('hex')