Skip to content

Commit

Permalink
exchanges: add scope type validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ziluvatar committed Aug 9, 2017
1 parent 0516025 commit abc35a2
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 253 deletions.
16 changes: 10 additions & 6 deletions lib/exchange/clientCredentials.js
Expand Up @@ -60,7 +60,7 @@ module.exports = function(options, issue) {
options = undefined;
}
options = options || {};

if (!issue) { throw new TypeError('oauth2orize.clientCredentials exchange requires an issue callback'); }

var userProperty = options.userProperty || 'user';
Expand All @@ -77,13 +77,17 @@ module.exports = function(options, issue) {

return function client_credentials(req, res, next) {
if (!req.body) { return next(new Error('OAuth2orize requires body parsing. Did you forget app.use(express.bodyParser())?')); }

// The 'user' property of `req` holds the authenticated user. In the case
// of the token endpoint, the property will contain the OAuth 2.0 client.
var client = req[userProperty]
, scope = req.body.scope;

if (scope) {
if (typeof scope !== 'string') {
return next(new TokenError('Invalid parameter: scope must be a string', 'invalid_scope'));
}

for (var i = 0, len = separators.length; i < len; i++) {
var separated = scope.split(separators[i]);
// only separate on the first matching separator. this allows for a sort
Expand All @@ -95,7 +99,7 @@ module.exports = function(options, issue) {
}
if (!Array.isArray(scope)) { scope = [ scope ]; }
}

function issued(err, accessToken, refreshToken, params) {
if (err) { return next(err); }
if (!accessToken) { return next(new TokenError('Invalid client credentials', 'invalid_grant')); }
Expand All @@ -109,14 +113,14 @@ module.exports = function(options, issue) {
if (refreshToken) { tok.refresh_token = refreshToken; }
if (params) { utils.merge(tok, params); }
tok.token_type = tok.token_type || 'Bearer';

var json = JSON.stringify(tok);
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'no-store');
res.setHeader('Pragma', 'no-cache');
res.end(json);
}

try {
var arity = issue.length;
if (arity == 5) {
Expand Down
16 changes: 10 additions & 6 deletions lib/exchange/password.js
Expand Up @@ -78,18 +78,22 @@ module.exports = function(options, issue) {

return function password(req, res, next) {
if (!req.body) { return next(new Error('OAuth2orize requires body parsing. Did you forget app.use(express.bodyParser())?')); }

// The 'user' property of `req` holds the authenticated user. In the case
// of the token endpoint, the property will contain the OAuth 2.0 client.
var client = req[userProperty]
, username = req.body.username
, passwd = req.body.password
, scope = req.body.scope;

if (!username) { return next(new TokenError('Missing required parameter: username', 'invalid_request')); }
if (!passwd) { return next(new TokenError('Missing required parameter: password', 'invalid_request')); }

if (scope) {
if (typeof scope !== 'string') {
return next(new TokenError('Invalid parameter: scope must be a string', 'invalid_scope'));
}

for (var i = 0, len = separators.length; i < len; i++) {
var separated = scope.split(separators[i]);
// only separate on the first matching separator. this allows for a sort
Expand All @@ -101,21 +105,21 @@ module.exports = function(options, issue) {
}
if (!Array.isArray(scope)) { scope = [ scope ]; }
}

function issued(err, accessToken, refreshToken, params) {
if (err) { return next(err); }
if (!accessToken) { return next(new TokenError('Invalid resource owner credentials', 'invalid_grant')); }
if (refreshToken && typeof refreshToken == 'object') {
params = refreshToken;
refreshToken = null;
}

var tok = {};
tok.access_token = accessToken;
if (refreshToken) { tok.refresh_token = refreshToken; }
if (params) { utils.merge(tok, params); }
tok.token_type = tok.token_type || 'Bearer';

var json = JSON.stringify(tok);
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'no-store');
Expand Down
22 changes: 13 additions & 9 deletions lib/exchange/refreshToken.js
Expand Up @@ -58,9 +58,9 @@ module.exports = function(options, issue) {
options = undefined;
}
options = options || {};

if (!issue) { throw new TypeError('oauth2orize.refreshToken exchange requires an issue callback'); }

var userProperty = options.userProperty || 'user';

// For maximum flexibility, multiple scope spearators can optionally be
Expand All @@ -75,16 +75,20 @@ module.exports = function(options, issue) {

return function refresh_token(req, res, next) {
if (!req.body) { return next(new Error('OAuth2orize requires body parsing. Did you forget app.use(express.bodyParser())?')); }

// The 'user' property of `req` holds the authenticated user. In the case
// of the token endpoint, the property will contain the OAuth 2.0 client.
var client = req[userProperty]
, refreshToken = req.body.refresh_token
, scope = req.body.scope;

if (!refreshToken) { return next(new TokenError('Missing required parameter: refresh_token', 'invalid_request')); }

if (scope) {
if (typeof scope !== 'string') {
return next(new TokenError('Invalid parameter: scope must be a string', 'invalid_scope'));
}

for (var i = 0, len = separators.length; i < len; i++) {
var separated = scope.split(separators[i]);
// only separate on the first matching separator. this allows for a sort
Expand All @@ -96,28 +100,28 @@ module.exports = function(options, issue) {
}
if (!Array.isArray(scope)) { scope = [ scope ]; }
}

function issued(err, accessToken, refreshToken, params) {
if (err) { return next(err); }
if (!accessToken) { return next(new TokenError('Invalid refresh token', 'invalid_grant')); }
if (refreshToken && typeof refreshToken == 'object') {
params = refreshToken;
refreshToken = null;
}

var tok = {};
tok.access_token = accessToken;
if (refreshToken) { tok.refresh_token = refreshToken; }
if (params) { utils.merge(tok, params); }
tok.token_type = tok.token_type || 'Bearer';

var json = JSON.stringify(tok);
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'no-store');
res.setHeader('Pragma', 'no-cache');
res.end(json);
}

try {
var arity = issue.length;
if (arity == 6) {
Expand Down

0 comments on commit abc35a2

Please sign in to comment.