Skip to content

Commit 31e9589

Browse files
Alex KAlex K
authored andcommitted
Less dependency from Express
1 parent 09317a1 commit 31e9589

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

lib/strategy.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ var passport = require('passport-strategy')
88
, SessionStateStore = require('./state/session')
99
, AuthorizationError = require('./errors/authorizationerror')
1010
, TokenError = require('./errors/tokenerror')
11-
, InternalOAuthError = require('./errors/internaloautherror');
11+
, InternalOAuthError = require('./errors/internaloautherror')
12+
, querystring = require('querystring');
1213

1314

1415
/**
@@ -123,12 +124,13 @@ util.inherits(OAuth2Strategy, passport.Strategy);
123124
OAuth2Strategy.prototype.authenticate = function(req, options) {
124125
options = options || {};
125126
var self = this;
127+
var query = getQuery(req);
126128

127-
if (req.query && req.query.error) {
128-
if (req.query.error == 'access_denied') {
129-
return this.fail({ message: req.query.error_description });
129+
if (query && query.error) {
130+
if (query.error == 'access_denied') {
131+
return this.fail({ message: query.error_description });
130132
} else {
131-
return this.error(new AuthorizationError(req.query.error_description, req.query.error, req.query.error_uri));
133+
return this.error(new AuthorizationError(query.error_description, query.error, query.error_uri));
132134
}
133135
}
134136

@@ -148,14 +150,14 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
148150
clientID: this._oauth2._clientId
149151
}
150152

151-
if (req.query && req.query.code) {
153+
if (query && query.code) {
152154
function loaded(err, ok, state) {
153155
if (err) { return self.error(err); }
154156
if (!ok) {
155157
return self.fail(state, 403);
156158
}
157159

158-
var code = req.query.code;
160+
var code = query.code;
159161

160162
var params = self.tokenParams(options);
161163
params.grant_type = 'authorization_code';
@@ -201,7 +203,7 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
201203
);
202204
}
203205

204-
var state = req.query.state;
206+
var state = query.state;
205207
try {
206208
var arity = this._stateStore.verify.length;
207209
if (arity == 4) {
@@ -380,6 +382,24 @@ OAuth2Strategy.prototype._createOAuthError = function(message, err) {
380382
return e;
381383
};
382384

385+
/**
386+
* Return query params
387+
*
388+
* @param {Object} req Node or Express request object
389+
* @return {Object} Query params
390+
*/
391+
function getQuery(req) {
392+
if (req.query) {
393+
return req.query;
394+
}
395+
396+
if (req.url) {
397+
var parsedUrl = url.parse(req.url);
398+
return querystring.parse(parsedUrl.query);
399+
}
400+
401+
return {};
402+
}
383403

384404
// Expose constructor.
385405
module.exports = OAuth2Strategy;

0 commit comments

Comments
 (0)