Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions lib/grant-types/abstract-grant-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
var InvalidArgumentError = require('../errors/invalid-argument-error');
var InvalidScopeError = require('../errors/invalid-scope-error');
var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var is = require('../validator/is');
var tokenUtil = require('../utils/token-util');

Expand Down Expand Up @@ -36,7 +37,7 @@ function AbstractGrantType(options) {

AbstractGrantType.prototype.generateAccessToken = function() {
if (this.model.generateAccessToken) {
return Promise.try(this.model.generateAccessToken);
return promisify(this.model.generateAccessToken)();
}

return tokenUtil.generateRandomToken();
Expand All @@ -48,7 +49,7 @@ AbstractGrantType.prototype.generateAccessToken = function() {

AbstractGrantType.prototype.generateRefreshToken = function() {
if (this.model.generateRefreshToken) {
return Promise.try(this.model.generateRefreshToken);
return promisify(this.model.generateRefreshToken)();
}

return tokenUtil.generateRandomToken();
Expand Down Expand Up @@ -94,14 +95,18 @@ AbstractGrantType.prototype.getScope = function(request) {
* Validate requested scope.
*/
AbstractGrantType.prototype.validateScope = function(user, client, scope) {
return Promise.try(this.model.validateScope, [user, client, scope])
.then(function(scope) {
if(!scope) {
throw new InvalidScopeError('Invalid scope: Requested scope is invalid');
}

return scope;
});
if (this.model.validateScope) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously model#validateScope was a required model function. (Promise.try throws a TypeError if the first argument isn't a function. ) This change makes validateScope optional.
If this is the desired effect there should probably be an else accepting any scope. Something like this should work:

} else {
  return scope;
}

Without this addition all scopes are lost, resulting in a call to model#saveToken with token.scope === undefined (see for example ClientCredentialsGrantType#saveToken).

return promisify(this.model.validateScope, 3)(user, client, scope)
.then(function (scope) {
if (!scope) {
throw new InvalidScopeError('Invalid scope: Requested scope is invalid');
}

return scope;
});
} else {
return scope;
}
};

/**
Expand Down
8 changes: 4 additions & 4 deletions lib/grant-types/authorization-code-grant-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var InvalidArgumentError = require('../errors/invalid-argument-error');
var InvalidGrantError = require('../errors/invalid-grant-error');
var InvalidRequestError = require('../errors/invalid-request-error');
var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var ServerError = require('../errors/server-error');
var is = require('../validator/is');
var util = require('util');
Expand Down Expand Up @@ -87,8 +88,7 @@ AuthorizationCodeGrantType.prototype.getAuthorizationCode = function(request, cl
if (!is.vschar(request.body.code)) {
throw new InvalidRequestError('Invalid parameter: `code`');
}

return Promise.try(this.model.getAuthorizationCode, request.body.code)
return promisify(this.model.getAuthorizationCode, 1)(request.body.code)
.then(function(code) {
if (!code) {
throw new InvalidGrantError('Invalid grant: authorization code is invalid');
Expand Down Expand Up @@ -160,7 +160,7 @@ AuthorizationCodeGrantType.prototype.getAuthorizationCode = function(request, cl
*/

AuthorizationCodeGrantType.prototype.revokeAuthorizationCode = function(code) {
return Promise.try(this.model.revokeAuthorizationCode, code)
return promisify(this.model.revokeAuthorizationCode, 1)(code)
.then(function(status) {
if (!status) {
throw new InvalidGrantError('Invalid grant: authorization code is invalid');
Expand Down Expand Up @@ -191,7 +191,7 @@ AuthorizationCodeGrantType.prototype.saveToken = function(user, client, authoriz
scope: scope
};

return this.model.saveToken(token, client, user);
return promisify(this.model.saveToken, 3)(token, client, user);
});
};

Expand Down
5 changes: 3 additions & 2 deletions lib/grant-types/client-credentials-grant-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var AbstractGrantType = require('./abstract-grant-type');
var InvalidArgumentError = require('../errors/invalid-argument-error');
var InvalidGrantError = require('../errors/invalid-grant-error');
var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var util = require('util');

/**
Expand Down Expand Up @@ -69,7 +70,7 @@ ClientCredentialsGrantType.prototype.handle = function(request, client) {
*/

ClientCredentialsGrantType.prototype.getUserFromClient = function(client) {
return Promise.try(this.model.getUserFromClient, client)
return promisify(this.model.getUserFromClient, 1)(client)
.then(function(user) {
if (!user) {
throw new InvalidGrantError('Invalid grant: user credentials are invalid');
Expand Down Expand Up @@ -99,7 +100,7 @@ ClientCredentialsGrantType.prototype.saveToken = function(user, client, scope) {
scope: scope
};

return this.model.saveToken(token, client, user);
return promisify(this.model.saveToken, 3)(token, client, user);
});
};

Expand Down
5 changes: 3 additions & 2 deletions lib/grant-types/password-grant-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var InvalidArgumentError = require('../errors/invalid-argument-error');
var InvalidGrantError = require('../errors/invalid-grant-error');
var InvalidRequestError = require('../errors/invalid-request-error');
var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var is = require('../validator/is');
var util = require('util');

Expand Down Expand Up @@ -87,7 +88,7 @@ PasswordGrantType.prototype.getUser = function(request) {
throw new InvalidRequestError('Invalid parameter: `password`');
}

return Promise.try(this.model.getUser, [request.body.username, request.body.password])
return promisify(this.model.getUser, 2)(request.body.username, request.body.password)
.then(function(user) {
if (!user) {
throw new InvalidGrantError('Invalid grant: user credentials are invalid');
Expand Down Expand Up @@ -121,7 +122,7 @@ PasswordGrantType.prototype.saveToken = function(user, client, scope) {
scope: scope
};

return this.model.saveToken(token, client, user);
return promisify(this.model.saveToken, 3)(token, client, user);
});
};

Expand Down
13 changes: 10 additions & 3 deletions lib/grant-types/refresh-token-grant-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var InvalidArgumentError = require('../errors/invalid-argument-error');
var InvalidGrantError = require('../errors/invalid-grant-error');
var InvalidRequestError = require('../errors/invalid-request-error');
var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var ServerError = require('../errors/server-error');
var is = require('../validator/is');
var util = require('util');
Expand Down Expand Up @@ -85,7 +86,7 @@ RefreshTokenGrantType.prototype.getRefreshToken = function(request, client) {
throw new InvalidRequestError('Invalid parameter: `refresh_token`');
}

return Promise.try(this.model.getRefreshToken, request.body.refresh_token)
return promisify(this.model.getRefreshToken, 1)(request.body.refresh_token)
.then(function(token) {
if (!token) {
throw new InvalidGrantError('Invalid grant: refresh token is invalid');
Expand Down Expand Up @@ -122,7 +123,7 @@ RefreshTokenGrantType.prototype.getRefreshToken = function(request, client) {
*/

RefreshTokenGrantType.prototype.revokeToken = function(token) {
return Promise.try(this.model.revokeToken, token)
return promisify(this.model.revokeToken, 1)(token)
.then(function(status) {
if (!status) {
throw new InvalidGrantError('Invalid grant: refresh token is invalid');
Expand Down Expand Up @@ -155,7 +156,13 @@ RefreshTokenGrantType.prototype.saveToken = function(user, client, scope) {
scope: scope
};

return this.model.saveToken(token, client, user);
return token;
})
.then(function(token) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding additional thens and Promise.try is unnecessary. See previous comments.

return Promise.try(promisify(this.model.saveToken, 3), [token, client, user])
.then(function(savedToken) {
return savedToken;
});
});
};

Expand Down
5 changes: 3 additions & 2 deletions lib/handlers/authenticate-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var InvalidScopeError = require('../errors/invalid-scope-error');
var InvalidTokenError = require('../errors/invalid-token-error');
var OAuthError = require('../errors/oauth-error');
var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var Request = require('../request');
var Response = require('../response');
var ServerError = require('../errors/server-error');
Expand Down Expand Up @@ -196,7 +197,7 @@ AuthenticateHandler.prototype.getTokenFromRequestBody = function(request) {
*/

AuthenticateHandler.prototype.getAccessToken = function(token) {
return Promise.try(this.model.getAccessToken, token)
return promisify(this.model.getAccessToken, 1)(token)
.then(function(accessToken) {
if (!accessToken) {
throw new InvalidTokenError('Invalid token: access token is invalid');
Expand Down Expand Up @@ -231,7 +232,7 @@ AuthenticateHandler.prototype.validateAccessToken = function(accessToken) {
*/

AuthenticateHandler.prototype.verifyScope = function(accessToken) {
return Promise.try(this.model.verifyScope, [accessToken, this.scope]).then(function(scope) {
return promisify(this.model.verifyScope, 2)(accessToken, this.scope).then(function(scope) {
if (!scope) {
throw new InvalidScopeError('Invalid scope: scope is invalid');
}
Expand Down
16 changes: 5 additions & 11 deletions lib/handlers/authorize-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var InvalidRequestError = require('../errors/invalid-request-error');
var InvalidScopeError = require('../errors/invalid-scope-error');
var OAuthError = require('../errors/oauth-error');
var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var Request = require('../request');
var Response = require('../response');
var ServerError = require('../errors/server-error');
Expand Down Expand Up @@ -113,7 +114,6 @@ AuthorizeHandler.prototype.handle = function(request, response) {
if (!(e instanceof OAuthError)) {
e = new ServerError(e);
}

var redirectUri = this.buildErrorRedirectUri(uri, e);

this.updateResponse(response, redirectUri, state);
Expand All @@ -129,9 +129,8 @@ AuthorizeHandler.prototype.handle = function(request, response) {

AuthorizeHandler.prototype.generateAuthorizationCode = function() {
if (this.model.generateAuthorizationCode) {
return Promise.try(this.model.generateAuthorizationCode);
return promisify(this.model.generateAuthorizationCode)();
}

return tokenUtil.generateRandomToken();
};

Expand All @@ -143,7 +142,6 @@ AuthorizeHandler.prototype.getAuthorizationCodeLifetime = function() {
var expires = new Date();

expires.setSeconds(expires.getSeconds() + this.authorizationCodeLifetime);

return expires;
};

Expand All @@ -167,8 +165,7 @@ AuthorizeHandler.prototype.getClient = function(request) {
if (redirectUri && !is.uri(redirectUri)) {
throw new InvalidRequestError('Invalid request: `redirect_uri` is not a valid URI');
}

return Promise.try(this.model.getClient, clientId)
return promisify(this.model.getClient, 2)(clientId, null)
.then(function(client) {
if (!client) {
throw new InvalidClientError('Invalid client: client credentials are invalid');
Expand All @@ -189,7 +186,6 @@ AuthorizeHandler.prototype.getClient = function(request) {
if (redirectUri && !_.contains(client.redirectUris, redirectUri)) {
throw new InvalidClientError('Invalid client: `redirect_uri` does not match client value');
}

return client;
});
};
Expand Down Expand Up @@ -234,8 +230,7 @@ AuthorizeHandler.prototype.getUser = function(request, response) {
if (this.authenticateHandler instanceof AuthenticateHandler) {
return this.authenticateHandler.handle(request, response).get('user');
}

return Promise.try(this.authenticateHandler.handle, [request, response]).then(function(user) {
return promisify(this.authenticateHandler.handle, 2)(request, response).then(function(user) {
if (!user) {
throw new ServerError('Server error: `handle()` did not return a `user` object');
}
Expand Down Expand Up @@ -263,8 +258,7 @@ AuthorizeHandler.prototype.saveAuthorizationCode = function(authorizationCode, e
redirectUri: redirectUri,
scope: scope
};

return Promise.try(this.model.saveAuthorizationCode, [code, client, user]);
return promisify(this.model.saveAuthorizationCode, 3)(code, client, user);
};

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/handlers/token-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var InvalidClientError = require('../errors/invalid-client-error');
var InvalidRequestError = require('../errors/invalid-request-error');
var OAuthError = require('../errors/oauth-error');
var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var Request = require('../request');
var Response = require('../response');
var ServerError = require('../errors/server-error');
Expand Down Expand Up @@ -127,7 +128,7 @@ TokenHandler.prototype.getClient = function(request, response) {
throw new InvalidRequestError('Invalid parameter: `client_secret`');
}

return Promise.try(this.model.getClient, [credentials.clientId, credentials.clientSecret])
return promisify(this.model.getClient, 2)(credentials.clientId, credentials.clientSecret)
.then(function(client) {
if (!client) {
throw new InvalidClientError('Invalid client: client is invalid');
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"bluebird": "^2.9.13",
"camel-case": "^1.1.1",
"lodash": "^3.3.1",
"promisify-any": "2.0.1",
"standard-http-error": "^1.1.0",
"type-is": "^1.6.0",
"validator.js": "^1.1.1"
Expand Down
51 changes: 51 additions & 0 deletions test/integration/grant-types/authorization-code-grant-type_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ describe('AuthorizationCodeGrantType integration', function() {

grantType.handle(request, client).should.be.an.instanceOf(Promise);
});

it('should support callbacks', function() {
var client = { id: 'foobar' };
var model = {
getAuthorizationCode: function(code, callback) { callback(null, { authorizationCode: 12345, client: { id: 'foobar' }, expiresAt: new Date(new Date() * 2), user: {} }); },
revokeAuthorizationCode: function(code, callback) { callback(null, { authorizationCode: 12345, client: { id: 'foobar' }, expiresAt: new Date(new Date() / 2), user: {} }); },
saveToken: function(tokenToSave, client, user, callback) { callback(null, tokenToSave); }
};
var grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });
var request = new Request({ body: { code: 12345 }, headers: {}, method: {}, query: {} });

grantType.handle(request, client).should.be.an.instanceOf(Promise);
});
});

describe('getAuthorizationCode()', function() {
Expand Down Expand Up @@ -374,6 +387,20 @@ describe('AuthorizationCodeGrantType integration', function() {

grantType.getAuthorizationCode(request, client).should.be.an.instanceOf(Promise);
});

it('should support callbacks', function() {
var authorizationCode = { authorizationCode: 12345, client: { id: 'foobar' }, expiresAt: new Date(new Date() * 2), user: {} };
var client = { id: 'foobar' };
var model = {
getAuthorizationCode: function(code, callback) { callback(null, authorizationCode); },
revokeAuthorizationCode: function() {},
saveToken: function() {}
};
var grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });
var request = new Request({ body: { code: 12345 }, headers: {}, method: {}, query: {} });

grantType.getAuthorizationCode(request, client).should.be.an.instanceOf(Promise);
});
});

describe('validateRedirectUri()', function() {
Expand Down Expand Up @@ -458,6 +485,18 @@ describe('AuthorizationCodeGrantType integration', function() {

grantType.revokeAuthorizationCode(authorizationCode).should.be.an.instanceOf(Promise);
});

it('should support callbacks', function() {
var authorizationCode = { authorizationCode: 12345, client: {}, expiresAt: new Date(new Date() / 2), user: {} };
var model = {
getAuthorizationCode: function() {},
revokeAuthorizationCode: function(code, callback) { callback(null, authorizationCode); },
saveToken: function() {}
};
var grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });

grantType.revokeAuthorizationCode(authorizationCode).should.be.an.instanceOf(Promise);
});
});

describe('saveToken()', function() {
Expand Down Expand Up @@ -501,5 +540,17 @@ describe('AuthorizationCodeGrantType integration', function() {

grantType.saveToken(token).should.be.an.instanceOf(Promise);
});

it('should support callbacks', function() {
var token = {};
var model = {
getAuthorizationCode: function() {},
revokeAuthorizationCode: function() {},
saveToken: function(tokenToSave, client, user, callback) { callback(null, token); }
};
var grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });

grantType.saveToken(token).should.be.an.instanceOf(Promise);
});
});
});
Loading