修复店务前端请求签名兼容性
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { InternalAxiosRequestConfig } from 'axios'
|
||||
import { createHash, createHmac } from 'node:crypto'
|
||||
import { attachRequestSignature } from '@/utils/request-signature'
|
||||
|
||||
const buildConfig = (): InternalAxiosRequestConfig => ({
|
||||
@@ -15,6 +16,7 @@ describe('request signature', () => {
|
||||
beforeEach(() => {
|
||||
window.localStorage.clear()
|
||||
vi.restoreAllMocks()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
it('adds device and signature headers for protected unsafe requests', async () => {
|
||||
@@ -36,6 +38,32 @@ describe('request signature', () => {
|
||||
expect(String(config.headers['X-Signature'])).toHaveLength(64)
|
||||
})
|
||||
|
||||
it('signs unsafe requests when Web Crypto subtle is unavailable', 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(buildConfig(), 'token-secret', { authFree: false })
|
||||
const payloadHash = createHash('sha256').update(JSON.stringify({ name: 'test' })).digest('hex')
|
||||
const canonical = [
|
||||
'POST',
|
||||
'/admin/manager/planning/appointments',
|
||||
'a=1&b=2',
|
||||
payloadHash,
|
||||
'1789999999000',
|
||||
'1789999999000-00000000-0000-4000-8000-000000000001-ak'
|
||||
].join('\n')
|
||||
const expectedSignature = createHmac('sha256', 'token-secret').update(canonical).digest('hex')
|
||||
|
||||
expect(config.headers['X-Signature']).toBe(expectedSignature)
|
||||
})
|
||||
|
||||
it('skips signature for auth-free requests but still attaches device id', async () => {
|
||||
vi.spyOn(crypto, 'randomUUID').mockReturnValue('00000000-0000-4000-8000-000000000001')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user