-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (47 loc) · 1.54 KB
/
index.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const {
provideUTCDate,
provideHMAC,
} = require('./providers');
function generateCosmosDBHeaders(method, resourceType, resourceLink, masterKey) {
const utcDate = provideUTCDate();
const authToken = _generateAuthorizationToken(method, resourceType, resourceLink, utcDate, masterKey);
return {
'Content-Type': 'application/json',
'Authorization': authToken,
'x-ms-version': '2017-02-22',
'x-ms-date': utcDate,
};
}
function _generateAuthorizationToken(verb, resourceType, resourceLink, date, masterKey) {
var payload = verb.toLowerCase() + "\n" +
resourceType.toLowerCase() + "\n" +
resourceLink + "\n" +
date.toLowerCase() + "\n\n";
var signature = _createBase64Signature(masterKey, payload);
var MasterToken = "master";
var TokenVersion = "1.0";
return encodeURIComponent("type=" + MasterToken + "&ver=" + TokenVersion + "&sig=" + signature);
}
function _createBase64Signature(b64Key, rawBody) {
let body_buf = _strToArrayBuffer(rawBody);
let key_buf = _strToArrayBuffer(atob(b64Key));
let sig_buf = provideHMAC(key_buf, body_buf);
let sig_raw = _bufferToString(sig_buf);
let sig_b64 = btoa(sig_raw);
return sig_b64;
}
function _bufferToString(wordbuf) {
return String.fromCharCode.apply(null, wordbuf);
}
function _strToArrayBuffer(str) {
let len = str.length;
let bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = str.charCodeAt(i);
}
return bytes;
}
module.exports = {
generateCosmosDBHeaders,
}
exports.generateCosmosDBHeaders = generateCosmosDBHeaders;