-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsuperagent-oauth.js
133 lines (107 loc) · 2.88 KB
/
superagent-oauth.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
module.exports = function (superagent) {
/**
* Module dependencies.
*/
var Request = superagent.Request;
/**
* Override .query() to collect the query values.
* XXX: it would be nice if superagent offered an API for this...
*
* @api public
*/
var oldQuery = Request.prototype.query;
Request.prototype.query = function (obj) {
if (!this._oauth_query) this._oauth_query = {};
// merge
var keys = Object.keys(obj), key;
for (var i = 0; i < keys.length; i++) {
key = keys[i];
this._oauth_query[key] = obj[key];
}
return oldQuery.call(this, obj);
};
/**
* Add sign method.
*
* Options:
* - accessField (`String`) defaults to `oauth_token`
*
* @param {OAuth|OAuth2} oa instance
* @param {String} token
* @param {String} secret (only for OAuth 1.0/1.0a)
* @api public
*/
Request.prototype.sign = function (oa, token, secret) {
this.oa = oa;
this.token = token;
this.secret = secret;
return this;
};
/**
* Signs the request for OAuth.
*
* @param {Function} callback
* @api private
*/
Request.prototype.signOAuth = function () {
var extra_params = this._oauth_query;
if ('application/x-www-form-urlencoded' == this.getHeader('content-type') && this.isObject(this._data)) {
if(extra_params) {
// merge
var keys = Object.keys(this._data), key;
for (var i = 0; i < keys.length; i++) {
key = keys[i];
extra_params[key] = this._data[key];
}
} else {
extra_params = this._data;
}
}
var params = this.oa._prepareParameters(
this.token
, this.secret
, this.method
, this.url
, this.extra_params
);
var header = this.oa._isEcho
? 'X-Verify-Credentials-Authorization'
: 'Authorization'
, signature = this.oa._buildAuthorizationHeaders(params)
this.set(header, signature);
};
/**
* Signs the request for OAuth2.
*
* @api private
*/
Request.prototype.signOAuth2 = function () {
var query = {};
query[this.oa._accessTokenName] = this.token;
this.query(query);
};
/**
* Overrides .end() to add the OAuth 1.0 "Authorization" header field.
*/
var oldEnd = Request.prototype.end;
Request.prototype.end = function () {
this.end = oldEnd;
if (this.oa && !this.oa._request) {
this.signOAuth();
}
return this.end.apply(this, arguments);
}
/**
* Overrides .request() to add the OAuth2 access token query param if needed.
* This cannot happen during .end() because the "query" params get processed
* before that here in the request() function.
*/
var oldRequest = Request.prototype.request;
Request.prototype.request = function () {
this.request = oldRequest;
if (this.oa && this.oa._request) {
this.signOAuth2();
}
return this.request();
};
}