Skip to content

Commit d1f09e9

Browse files
authored
Fix API parameters signature
1 parent b1c1171 commit d1f09e9

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/api/upload/config/api-config.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const defaultResourceType = 'image';
44
export const defaultChunckSize = 20 * 1024 * 1024;
55
export const defaultTimeout = 60;
66
export const defaultUploadPrefix = 'https://api.cloudinary.com';
7+
export const defaultSignatureVersion = 2;
78

89
export
910

@@ -25,6 +26,7 @@ class APIConfig {
2526
cloudName: string = '';
2627
apiKey: string | null = '';
2728
apiSecret: string | null = '';
29+
signatureVersion: number = defaultSignatureVersion;
2830
}
2931

3032
export { APIConfig }

src/api/upload/utils/index.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,34 @@ export function present(value) {
1212
}
1313

1414
async function sign_request(apiConfig, params, options = {}) {
15-
let apiKey = apiConfig.apiKey
16-
let apiSecret = apiConfig.apiSecret
17-
params = clear_blank(params);
18-
params.signature = await api_sign_request(params, apiSecret);
19-
params.api_key = apiKey;
20-
return params;
15+
const apiKey = apiConfig.apiKey;
16+
const apiSecret = apiConfig.apiSecret;
17+
const signatureVersion = apiConfig.signatureVersion;
18+
19+
params = clear_blank(params);
20+
21+
if (signatureVersion === 2) {
22+
for (let key in params) {
23+
const value = params[key];
24+
25+
if (Array.isArray(value)) {
26+
params[key] = value.map(v =>
27+
typeof v === 'string' ? v.replace(/&/g, '%26') : v
28+
);
29+
} else if (typeof value === 'string') {
30+
params[key] = value.replace(/&/g, '%26');
31+
}
32+
}
2133
}
2234

35+
params.signature = await api_sign_request(params, apiSecret);
36+
params.api_key = apiKey;
37+
return params;
38+
}
39+
40+
41+
42+
2343
async function api_sign_request(params_to_sign, api_secret) {
2444
let to_sign = entries(params_to_sign).filter(
2545
([k, v]) => present(v)

0 commit comments

Comments
 (0)