@fy-/fws-vue 2.3.61 → 2.3.63
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/composables/rest.ts +29 -5
- package/package.json +1 -1
package/composables/rest.ts
CHANGED
|
@@ -87,6 +87,30 @@ function computeRequestHash(url: string, method: RestMethod, params?: RestParams
|
|
|
87
87
|
return hash
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
function str2ab(str) {
|
|
91
|
+
const encoder = new TextEncoder()
|
|
92
|
+
return encoder.encode(str)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Create HMAC signature
|
|
96
|
+
async function createHMACSignature(secret, data) {
|
|
97
|
+
const key = await crypto.subtle.importKey(
|
|
98
|
+
'raw',
|
|
99
|
+
str2ab(secret),
|
|
100
|
+
{ name: 'HMAC', hash: 'SHA-256' },
|
|
101
|
+
false,
|
|
102
|
+
['sign'],
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
const signature = await crypto.subtle.sign(
|
|
106
|
+
'HMAC',
|
|
107
|
+
key,
|
|
108
|
+
str2ab(data),
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
return btoa(String.fromCharCode(...new Uint8Array(signature)))
|
|
112
|
+
}
|
|
113
|
+
|
|
90
114
|
export function useRest(): <ResultType extends APIResult>(
|
|
91
115
|
url: string,
|
|
92
116
|
method: RestMethod,
|
|
@@ -137,14 +161,14 @@ params?: RestParams,
|
|
|
137
161
|
// Create the actual request function
|
|
138
162
|
const performRequest = async (): Promise<ResultType> => {
|
|
139
163
|
const h: { [key: string]: string } = {}
|
|
140
|
-
const timestamp = Date.now()
|
|
141
164
|
|
|
142
165
|
if (secret && secret !== '') {
|
|
143
|
-
const
|
|
144
|
-
|
|
166
|
+
const timestamp = Date.now()
|
|
167
|
+
const dataToSign = `${method}${url}${timestamp}`
|
|
168
|
+
const signature = await createHMACSignature(secret, dataToSign)
|
|
169
|
+
|
|
145
170
|
h['X-Timestamp'] = timestamp.toString()
|
|
146
|
-
h['X-Signature'] =
|
|
147
|
-
console.debug('Using secret for request signature:', `${method}${url}${timestamp}${secret}`)
|
|
171
|
+
h['X-Signature'] = signature
|
|
148
172
|
}
|
|
149
173
|
|
|
150
174
|
try {
|