修复店务端个人信息保存签名路径
This commit is contained in:
@@ -94,6 +94,37 @@ describe('request signature', () => {
|
|||||||
expect(config.headers['X-Signature']).toBe(expectedSignature)
|
expect(config.headers['X-Signature']).toBe(expectedSignature)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('signs user api requests with backend path instead of dev proxy prefix', 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 data = { nickname: '店务', signature: '正常保存' }
|
||||||
|
const config = await attachRequestSignature({
|
||||||
|
headers: {},
|
||||||
|
method: 'put',
|
||||||
|
baseURL: '/api',
|
||||||
|
url: '/user/info',
|
||||||
|
data
|
||||||
|
} as InternalAxiosRequestConfig, 'token-secret', { authFree: false })
|
||||||
|
const payloadHash = createHash('sha256').update(JSON.stringify(data)).digest('hex')
|
||||||
|
const canonical = [
|
||||||
|
'PUT',
|
||||||
|
'/user/info',
|
||||||
|
'1789999999000',
|
||||||
|
'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('skips signature for auth-free requests but still attaches device id', async () => {
|
it('skips signature for auth-free requests but still attaches device id', async () => {
|
||||||
vi.spyOn(crypto, 'randomUUID').mockReturnValue('00000000-0000-4000-8000-000000000001')
|
vi.spyOn(crypto, 'randomUUID').mockReturnValue('00000000-0000-4000-8000-000000000001')
|
||||||
|
|
||||||
|
|||||||
@@ -222,7 +222,15 @@ const toBackendRequestPath = (config: InternalAxiosRequestConfig): string => {
|
|||||||
const url = String(config.url || '')
|
const url = String(config.url || '')
|
||||||
const basePath = extractPath(baseURL).replace(/\/+$/, '')
|
const basePath = extractPath(baseURL).replace(/\/+$/, '')
|
||||||
const urlPath = extractPath(url)
|
const urlPath = extractPath(url)
|
||||||
return `${basePath}${urlPath.startsWith('/') ? urlPath : `/${urlPath}`}`.replace(/^\/admin\/api/, '/admin')
|
const path = `${basePath}${urlPath.startsWith('/') ? urlPath : `/${urlPath}`}`
|
||||||
|
.replace(/^\/admin\/api/, '/admin')
|
||||||
|
if (path === '/api') {
|
||||||
|
return '/'
|
||||||
|
}
|
||||||
|
if (path.startsWith('/api/')) {
|
||||||
|
return `/${path.slice('/api/'.length)}`
|
||||||
|
}
|
||||||
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
const appendParam = (target: Record<string, string[]>, key: string, value: unknown): void => {
|
const appendParam = (target: Record<string, string[]>, key: string, value: unknown): void => {
|
||||||
|
|||||||
Reference in New Issue
Block a user