forked from colinmutter/superagent-hawk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·106 lines (84 loc) · 2.83 KB
/
index.js
File metadata and controls
executable file
·106 lines (84 loc) · 2.83 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var hawk = require('hawk');
var extend = require('./lib/extend');
var qs = require('qs');
exports =
module.exports = function (superagent) {
var RequestProto = superagent.Test
? superagent.Test.prototype
: superagent.Request.prototype;
RequestProto.hawk = function (credential, options) {
this._enable_hawk_signing = true;
this._hawk_credential = credential;
this._hawk_options = options;
return this;
}
RequestProto._do_hawk_sign = function () {
var contentType;
var querystring = qs.stringify(this.qs);
var method = this.method;
var url = this.url;
url += querystring.length
? '?' + querystring
: '';
if (this.getHeader && this.getHeader instanceof Function)
contentType = this.getHeader('content-type');
else if (this.get && this.get instanceof Function)
contentType = this.get('content-type');
var isJSON = this._data &&
this._data instanceof Object &&
contentType === 'application/json';
var data = (isJSON) ? JSON.stringify(this._data) : this._data;
var options = {
credentials: this._hawk_credential,
contentType: contentType,
payload: data
};
if (options && typeof options == 'object')
options = extend(options, this._hawk_options);
if ('verifyResponse' in options && options['verifyResponse'])
this._enable_hawk_response_verification = true;
var hawk_header = hawk.client.header(url, method, options);
this.set('Authorization', hawk_header.field);
return hawk_header.artifacts;
};
var verify_hawk_response = function(err, res, credential, artifacts) {
var hawk_options = {
payload: res.text,
required: true
}
var verified = hawk.client.authenticate(
res,
credential,
artifacts,
hawk_options);
if (!verified) {
res.error = new Error(
'Hawk response signature verification failed');
}
}
var oldEnd = RequestProto.end;
RequestProto.end = function (response_handler) {
this.end = oldEnd;
if (this._enable_hawk_signing) {
var artifacts = this._do_hawk_sign();
if (this._enable_hawk_response_verification) {
var hawk_credential = this._hawk_credential;
var wrapped_response_handler = function(err, res) {
if (res) {
verify_hawk_response(err, res, hawk_credential, artifacts);
}
if (2 == response_handler.length) return response_handler(err, res);
if (err) return this.emit('error', err);
return response_handler(res);
}
return this.end(wrapped_response_handler);
}
}
return this.end.apply(this, arguments);
};
RequestProto.bewit = function(bewit) {
this.query({ bewit: bewit });
return this;
};
return superagent;
}