-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumsubClient.js
More file actions
41 lines (37 loc) · 1.11 KB
/
sumsubClient.js
File metadata and controls
41 lines (37 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const axios = require('axios');
const crypto = require('crypto');
const BASE_URL = 'https://api.sumsub.com';
function signRequest({ method, urlPath, body = null, secret }) {
const ts = Math.floor(Date.now() / 1000);
const signature = crypto.createHmac('sha256', secret);
signature.update(ts + method.toUpperCase() + urlPath);
if (body) {
signature.update(body);
}
return {
'X-App-Access-Ts': ts,
'X-App-Access-Sig': signature.digest('hex')
};
}
async function createAccessToken({ appToken, secret, externalUserId, levelName, ttlInSecs = 600 }) {
const urlPath = '/resources/accessTokens/sdk';
const payload = JSON.stringify({ userId: externalUserId, levelName, ttlInSecs });
const sigHeaders = signRequest({ method: 'post', urlPath, body: payload, secret });
const headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-App-Token': appToken,
...sigHeaders
};
const response = await axios({
baseURL: BASE_URL,
method: 'post',
url: urlPath,
headers,
data: payload
});
return response.data;
}
module.exports = {
createAccessToken
};