Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Less dependencies from Express #78

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
docs/
reports/
.vscode/

# Mac OS X
.DS_Store
Expand Down
36 changes: 28 additions & 8 deletions lib/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var passport = require('passport-strategy')
, SessionStateStore = require('./state/session')
, AuthorizationError = require('./errors/authorizationerror')
, TokenError = require('./errors/tokenerror')
, InternalOAuthError = require('./errors/internaloautherror');
, InternalOAuthError = require('./errors/internaloautherror')
, querystring = require('querystring');


/**
Expand Down Expand Up @@ -123,12 +124,13 @@ util.inherits(OAuth2Strategy, passport.Strategy);
OAuth2Strategy.prototype.authenticate = function(req, options) {
options = options || {};
var self = this;
var query = getQuery(req);

if (req.query && req.query.error) {
if (req.query.error == 'access_denied') {
return this.fail({ message: req.query.error_description });
if (query && query.error) {
if (query.error == 'access_denied') {
return this.fail({ message: query.error_description });
} else {
return this.error(new AuthorizationError(req.query.error_description, req.query.error, req.query.error_uri));
return this.error(new AuthorizationError(query.error_description, query.error, query.error_uri));
}
}

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

if (req.query && req.query.code) {
if (query && query.code) {
function loaded(err, ok, state) {
if (err) { return self.error(err); }
if (!ok) {
return self.fail(state, 403);
}

var code = req.query.code;
var code = query.code;

var params = self.tokenParams(options);
params.grant_type = 'authorization_code';
Expand Down Expand Up @@ -201,7 +203,7 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
);
}

var state = req.query.state;
var state = query.state;
try {
var arity = this._stateStore.verify.length;
if (arity == 4) {
Expand Down Expand Up @@ -380,6 +382,24 @@ OAuth2Strategy.prototype._createOAuthError = function(message, err) {
return e;
};

/**
* Return query params
*
* @param {Object} req Node or Express request object
* @return {Object} Query params
*/
function getQuery(req) {
if (req.query) {
return req.query;
}

if (req.url) {
var parsedUrl = url.parse(req.url);
return querystring.parse(parsedUrl.query);
}

return {};
}

// Expose constructor.
module.exports = OAuth2Strategy;