| 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197 |
1
1
1
10
10
10
10
10
8
10
9
9
9
1
1
1
1
1
1
1
4
4
4
1
3
3
1
2
2
2
2
2
1
| 'use strict';
/**
* Module dependencies.
*/
var util = require('util'),
OAuth2Strategy = require('passport-oauth2'),
Profile = require('./profile'),
InternalOAuthError = require('passport-oauth2').InternalOAuthError;
// Polyfills
require('string.prototype.endswith');
/**
* `Strategy` constructor.
*
* The WP-OAuth authentication strategy authenticates requests by delegating
* to the configured WP-OAuth Server using the OAuth 2.0 protocol.
*
* Applications must supply a `verify` callback which accepts an `accessToken`,
* `refreshToken` and the service-specific `profile`, an then calls the `done`
* callback supplying a `user`, which should be set to `false` if the
* credentials are not valid. If an exception/error occured, `err` is set.
*
* Options:
* - `clientID` the Client ID you created in WP-OAuth
* - `clientSecret` the corresponding Client Secret
* - `callbackURL` URL to which WP-OAuth will redirect the user after granting authorization.
*
* - `authorizationURL` the URL to the authorization endpoint, defaults to localhost.
* - `tokenURL` the URL to get token endpoint, defaults to localhost.
* - `userProfileURL` the URL to the profile information endpoint, defaults to localhost.
*
* Examples:
*
* passport.use(new WPOAuthStrategy({
* clientID: '123abc456def789ghi',
* clientSecret: 'shhh-its-a-secret',
* callbackURL: 'https://www.example.net/auth/wpoauth/callback',
* authorizationURL: 'https://www.example.net/blog/oauth/authorize',
* tokenURL: 'https://www.example.net/blog/oauth/token',
* userProfileURL: 'https://www.example.net/blog/oauth/me'
* }, function(accessToken, refreshToken, profile, done) {
* User.findOrCreate(..., function(err, user) {
* ...
* done(err, user);
* });
* }
* ));
*
* @param {Object} options
* @param {Function} verify
* @api public
*/
function Strategy(o, verify) {
var options = JSON.parse(JSON.stringify(o)) || {};
options.authorizationURL = options.authorizationURL || 'http://localhost/oauth/authorize';
options.tokenURL = options.tokenURL || 'http://localhost/oauth/token';
options.customHeaders = options.customHeaders || {};
if (!options.customHeaders['User-Agent']) {
options.customHeaders['User-Agent'] = options.userAgent || 'passport-wpoauth';
}
OAuth2Strategy.call(this, options, verify);
this.name = 'wpoauth';
this._userProfileURL = options.userProfileURL || 'http://localhost/oauth/me/';
// Otherwise we get a 301 which unforutunaly not handled by http/https
if (!this._userProfileURL.endsWith('/')) {
this._userProfileURL = this._userProfileURL + '/';
}
}
/**
* Inherit from `OAuth2Strategy`.
*/
util.inherits(Strategy, OAuth2Strategy);
/**
*
*/
Strategy.prototype.authenticate = function(req, options) {
options = options || {};
var self = this;
if (req && req.query && req.query.error) {
return self.error('Request not provided or has errors');
}
if (!req.body) {
return self.error();
}
if (!req.body.redirectUri) {
return self.error('You need to provide a redirectUri');
} else {
self._callbackURL = req.body.redirectUri;
}
var authCode = req.body.code || req.query.code;
if (!authCode) {
return self.error();
}
self._exchangeAuthCode(authCode,
function(error, accessToken, refreshToken, results) {
if (error) {
return self.error(error);
}
self.userProfile(accessToken, function(err, profile) {
if (err) {
return self.fail(err);
}
var verified = function(e, user, info) {
if (e) {
return self.error(e);
}
if (!user) {
return self.fail(info);
}
self.success(user, info);
};
if (self._passReqToCallback) {
self._verify(req, accessToken, refreshToken, profile, verified);
} else {
self._verify(accessToken, refreshToken, profile, verified);
}
});
});
};
/**
* Exchange authorization code for tokens
*
* @param {String} authCode
* @param {Function} done
* @api private
*/
Strategy.prototype._exchangeAuthCode = function(authCode, done) {
var params = {
'grant_type': 'authorization_code',
'redirect_uri': this._callbackURL
};
this._oauth2.getOAuthAccessToken(authCode, params, done);
};
/**
* Retrive user profile from WP OAuth.
*
* This function constructs a normalized profile with the following properties:
*
* - `provider` always set to `wpoauth`
* - `id` the user's WordPress ID
* - `username` the user's login name
* - `displayName` the user's preferred identification (can be username or full name or both)
* - `emails` the user's email address
*
* @param {String} accessToken
* @param {Function} done
* @api protected
*/
Strategy.prototype.userProfile = function(accessToken, done) {
this._oauth2.get(this._userProfileURL, accessToken, function(err, body, res) {
var json;
if (err) {
return done(new InternalOAuthError('Failed to fetch user profile', err));
}
try {
json = JSON.parse(body);
} catch (ex) {
return done(ex);
}
var profile = Profile.parse(json);
profile.provider = 'wpoauth';
profile._raw = body;
profile._json = json;
done(null, profile);
});
};
/**
* Expose `Strategy`.
*/
module.exports = Strategy;
|