-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
150 lines (119 loc) · 2.63 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Module dependencies.
*/
var req = require('superagent');
var qs = require('querystring');
var debug = require('debug')('wpcom-oauth');
/**
* Default options
*/
var def = require('./default.json');
/**
* Options
*/
var globals;
/**
* Class constructor
*
* @param {Object} options
* @api public
*/
function WPOAuth(options){
if (!(this instanceof WPOAuth)) return new WPOAuth(options);
// Options
this.opts = globals = options || {};
for (var attr in def){
this.opts[attr] = this.opts[attr] || def[attr];
}
this._code = this.opts.code;
// Error exceptions
if (!this.opts.client_id) {
throw new Error('`client_id` is required');
}
if (!this.opts.client_secret) {
throw new Error('`client_secret` is required');
}
}
/**
* Get url to connect
*
* @param {Object} [resource]
* @param {Object} [param] query string vars
* @api public
*/
WPOAuth.prototype.urlToConnect = function(resource, params){
return WPOAuth.urlToConnect(resource, params);
};
/**
* Set code to build the request for the oauth
*
* @param {String} code
* @api public
*/
WPOAuth.prototype.code = function(code){
this._code = code;
debug('code: `%s`', this._code);
};
/**
* Send request for token
*
* @param {Function} fn callback
* @api public
*/
WPOAuth.prototype.requestAccessToken = function(fn){
if ('undefined' == typeof this._code) {
return fn(new Error('`code` is really needed'));
}
var data = {
"client_id": this.opts.client_id,
"client_secret": this.opts.client_secret,
"redirect_uri": this.opts.url.redirect,
"code": this._code,
"grant_type": this.opts.grant_type
};
var url = this.opts.endpoint.request_token;
req
.post(url)
.type('form')
.send(data)
.end(function (err, res) {
if (err) return fn(err);
// api error response
if (res.body && res.body.error) {
return fn(res.body);
}
fn(null, res.body);
});
};
/**
* Statics
*/
/**
* Return URL to connect
*
* @param {String} [resource]
* @param {Object} [param] query string vars
* @return {String} url link
* @api public
*/
WPOAuth.urlToConnect = function(resource, params){
if ('object' == typeof resource) {
params = resource;
resource = null;
}
params = params || {};
params.response_type = globals.response_type;
params.client_id = globals.client_id;
params.redirect_uri = globals.url.redirect;
if (resource && !params.blog) {
debug('get url: %s', resource);
params.blog = resource;
}
var link = def.endpoint.authorize + '?' + qs.stringify(params);
debug('url: `%s`', link);
return link;
};
/**
* expose `WPOAuth`
*/
module.exports = WPOAuth;