-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-hawkclient.js
More file actions
68 lines (53 loc) · 2.06 KB
/
service-hawkclient.js
File metadata and controls
68 lines (53 loc) · 2.06 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var HawkClient = (function () {
function generateHawkHeader(method, uri, host, port, credentials) {
var ts = Math.floor(new Date().getTime() / 1000).toString();
var nonce = Math.random().toString(36).substring(2, 8);
var normalized = "hawk.1.header\n" +
ts + "\n" +
nonce + "\n" +
method.toUpperCase() + "\n" +
uri + "\n" +
host.toLowerCase() + "\n" +
port + "\n" +
"\n" +
"\n";
//log("Normalized string for Hawk MAC:\n" + normalized);
var macRaw = Crypto.hmac("SHA256", credentials.key)
.update(normalized)
.final(true);
var mac = Encoding.binToBase64(macRaw);
//log("Calculated MAC: " + mac);
return 'Hawk id="' + credentials.id + '", ts="' + ts + '", nonce="' + nonce + '", mac="' + mac + '"';
}
function parseUrl(url) {
var m = url.match(/(https?):\/\/([^\/:]+)(?::(\d+))?(\/[^?]*)?(?:\?(.*))?/);
return {
scheme: m[1],
host: m[2],
port: m[3] ? parseInt(m[3], 10) : (m[1] === "https" ? 443 : 80),
path: m[4] || "/",
query: m[5] || ""
};
}
function sendHawkRequest(method, url, credentials, callback) {
var parts = parseUrl(url);
var path = parts.path + (parts.query ? "?" + parts.query : "");
var req = HttpClient.request(method.toUpperCase(), url);
req.header("Accept", "application/json");
req.header("User-Agent", "hawk-client-lib/1.0");
var hawkHeader = generateHawkHeader(method, path, parts.host, parts.port, credentials);
req.header("Authorization", hawkHeader);
var text = "";
req.onrecv(function (req, data, last) {
text += new TextDecoder("utf-8").decode(data);
if (!last) req.recv();
});
req.oncomplete(function (req, success) {
callback(success ? 200 : req.status, text);
});
req.recv();
}
return {
request: sendHawkRequest
};
})();