-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
37 lines (30 loc) · 1.04 KB
/
Copy pathauth.js
File metadata and controls
37 lines (30 loc) · 1.04 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
const crypto = require('crypto');
const url = require('url');
function getHeaders(method, fullUrl, accessKey, secretKey) {
const urlObj = url.parse(fullUrl);
const urlPath = urlObj.pathname;
const urlQuery = urlObj.query ? urlObj.query : '';
const authDate = new Date().toUTCString();
const nonce = crypto.randomBytes(25).toString('base64').substring(0, 25);
const contentType = 'application/json';
// Build the string to sign according to Onshape's format
const stringToSign = (
method + '\n' +
nonce + '\n' +
authDate + '\n' +
contentType + '\n' +
urlPath + '\n' +
urlQuery + '\n'
).toLowerCase();
const hmac = crypto.createHmac('sha256', secretKey)
.update(stringToSign)
.digest('base64');
const signature = 'On ' + accessKey + ':HmacSHA256:' + hmac;
return {
'Authorization': signature,
'Date': authDate,
'On-Nonce': nonce,
'Content-Type': contentType
};
}
module.exports = { getHeaders };