Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 37bfeae

Browse files
authored
Merge pull request #5 from jhellscream/master
feat: api key authentication v2
2 parents 405c994 + 7db4606 commit 37bfeae

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

.env.tpl.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export default {
44
logFolder: '',
55
stdout: false,
66
},
7-
key: '[你的 api key]',
8-
secret: '[你的 api secret]',
9-
passphrase: '[你的 api 密码]',
7+
key: '[Your api key]',
8+
secret: '[Your api secret]',
9+
passphrase: '[Your api passphrase]',
10+
keyVersion: 2
1011
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ copy .env.tpl.js .env.js
1717
export default {
1818
key: '[Your api key]',
1919
secret: '[Your api secret]',
20-
passphrase: '[Your api passphrase]'
20+
passphrase: '[Your api passphrase]',
21+
keyVersion: '[Api key version]', // For v2 API-KEY, not required for v1 version
2122
}
2223
```
2324

src/lib/http.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import request from 'request';
44
import CryptoJS from 'crypto';
55
import codes from './codes';
66
import log from './log';
7+
import { keyVersion2 } from './utils';
78

89
const IS_PRODUCT = global._USE_KUMEX_ONLINE_ || process.env.PRODUCTION === 'true';
910
const baseUrl = IS_PRODUCT ? 'https://api.kumex.com' : 'https://sandbox-api.kumex.com';
@@ -34,6 +35,8 @@ class Http {
3435
//
3536
static auth(configs, data = '', secret = '') {
3637

38+
const isKeyVersion2 = keyVersion2(HttpConfig.signatureConfig.keyVersion);
39+
3740
const timestamp = Date.now();
3841

3942
const signature = Http.sign(timestamp + configs.method.toUpperCase() + configs.url + data, secret);
@@ -44,14 +47,21 @@ class Http {
4447
if (!HttpConfig.signatureConfig.passphrase) {
4548
log('KC-API-PASSPHRASE is not specified');
4649
}
47-
return {
50+
const headers = {
4851
// ...(configs.headers || {}),
4952
'KC-API-KEY': HttpConfig.signatureConfig.key || '',
5053
'KC-API-SIGN': signature,
5154
'KC-API-TIMESTAMP': timestamp,
5255
'KC-API-PASSPHRASE': HttpConfig.signatureConfig.passphrase || '',
5356
'Content-Type': 'application/json',
5457
};
58+
59+
if (isKeyVersion2) {
60+
headers['KC-API-PASSPHRASE'] = Http.sign(HttpConfig.signatureConfig.passphrase || '', secret);
61+
headers['KC-API-KEY-VERSION'] = HttpConfig.signatureConfig.keyVersion;
62+
};
63+
64+
return headers;
5565
}
5666

5767

src/lib/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export const targetTypesMap = {
66
buy: 'bids',
77
};
88

9+
export const keyVersion2 = (version) => {
10+
return version && version === 2;
11+
};
12+
913
export const genUUID = (prefix = '')=> {
1014
return prefix + '__' +uuid();
1115
};

0 commit comments

Comments
 (0)