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 Original file line Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
var InvalidArgumentError = require('../errors/invalid-argument-error'); var InvalidArgumentError = require('../errors/invalid-argument-error');
var InvalidScopeError = require('../errors/invalid-scope-error'); var InvalidScopeError = require('../errors/invalid-scope-error');
var Promise = require('bluebird'); var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var is = require('../validator/is'); var is = require('../validator/is');
var tokenUtil = require('../utils/token-util'); var tokenUtil = require('../utils/token-util');


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


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


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


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


return tokenUtil.generateRandomToken(); return tokenUtil.generateRandomToken();
Expand Down Expand Up @@ -94,14 +95,18 @@ AbstractGrantType.prototype.getScope = function(request) {
* Validate requested scope. * Validate requested scope.
*/ */
AbstractGrantType.prototype.validateScope = function(user, client, scope) { AbstractGrantType.prototype.validateScope = function(user, client, scope) {
return Promise.try(this.model.validateScope, [user, client, 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).

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

}
return scope;
}); 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 Original file line 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 InvalidGrantError = require('../errors/invalid-grant-error');
var InvalidRequestError = require('../errors/invalid-request-error'); var InvalidRequestError = require('../errors/invalid-request-error');
var Promise = require('bluebird'); var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var ServerError = require('../errors/server-error'); var ServerError = require('../errors/server-error');
var is = require('../validator/is'); var is = require('../validator/is');
var util = require('util'); var util = require('util');
Expand Down Expand Up @@ -87,8 +88,7 @@ AuthorizationCodeGrantType.prototype.getAuthorizationCode = function(request, cl
if (!is.vschar(request.body.code)) { if (!is.vschar(request.body.code)) {
throw new InvalidRequestError('Invalid parameter: `code`'); throw new InvalidRequestError('Invalid parameter: `code`');
} }

return promisify(this.model.getAuthorizationCode, 1)(request.body.code)
return Promise.try(this.model.getAuthorizationCode, request.body.code)
.then(function(code) { .then(function(code) {
if (!code) { if (!code) {
throw new InvalidGrantError('Invalid grant: authorization code is invalid'); 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) { AuthorizationCodeGrantType.prototype.revokeAuthorizationCode = function(code) {
return Promise.try(this.model.revokeAuthorizationCode, code) return promisify(this.model.revokeAuthorizationCode, 1)(code)
.then(function(status) { .then(function(status) {
if (!status) { if (!status) {
throw new InvalidGrantError('Invalid grant: authorization code is invalid'); 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 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 Original file line 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 InvalidArgumentError = require('../errors/invalid-argument-error');
var InvalidGrantError = require('../errors/invalid-grant-error'); var InvalidGrantError = require('../errors/invalid-grant-error');
var Promise = require('bluebird'); var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var util = require('util'); var util = require('util');


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


ClientCredentialsGrantType.prototype.getUserFromClient = function(client) { ClientCredentialsGrantType.prototype.getUserFromClient = function(client) {
return Promise.try(this.model.getUserFromClient, client) return promisify(this.model.getUserFromClient, 1)(client)
.then(function(user) { .then(function(user) {
if (!user) { if (!user) {
throw new InvalidGrantError('Invalid grant: user credentials are invalid'); 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 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 Original file line 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 InvalidGrantError = require('../errors/invalid-grant-error');
var InvalidRequestError = require('../errors/invalid-request-error'); var InvalidRequestError = require('../errors/invalid-request-error');
var Promise = require('bluebird'); var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var is = require('../validator/is'); var is = require('../validator/is');
var util = require('util'); var util = require('util');


Expand Down Expand Up @@ -87,7 +88,7 @@ PasswordGrantType.prototype.getUser = function(request) {
throw new InvalidRequestError('Invalid parameter: `password`'); 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) { .then(function(user) {
if (!user) { if (!user) {
throw new InvalidGrantError('Invalid grant: user credentials are invalid'); 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 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 Original file line 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 InvalidGrantError = require('../errors/invalid-grant-error');
var InvalidRequestError = require('../errors/invalid-request-error'); var InvalidRequestError = require('../errors/invalid-request-error');
var Promise = require('bluebird'); var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var ServerError = require('../errors/server-error'); var ServerError = require('../errors/server-error');
var is = require('../validator/is'); var is = require('../validator/is');
var util = require('util'); 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`'); 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) { .then(function(token) {
if (!token) { if (!token) {
throw new InvalidGrantError('Invalid grant: refresh token is invalid'); 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) { RefreshTokenGrantType.prototype.revokeToken = function(token) {
return Promise.try(this.model.revokeToken, token) return promisify(this.model.revokeToken, 1)(token)
.then(function(status) { .then(function(status) {
if (!status) { if (!status) {
throw new InvalidGrantError('Invalid grant: refresh token is invalid'); 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 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 Original file line 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 InvalidTokenError = require('../errors/invalid-token-error');
var OAuthError = require('../errors/oauth-error'); var OAuthError = require('../errors/oauth-error');
var Promise = require('bluebird'); var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var Request = require('../request'); var Request = require('../request');
var Response = require('../response'); var Response = require('../response');
var ServerError = require('../errors/server-error'); var ServerError = require('../errors/server-error');
Expand Down Expand Up @@ -196,7 +197,7 @@ AuthenticateHandler.prototype.getTokenFromRequestBody = function(request) {
*/ */


AuthenticateHandler.prototype.getAccessToken = function(token) { AuthenticateHandler.prototype.getAccessToken = function(token) {
return Promise.try(this.model.getAccessToken, token) return promisify(this.model.getAccessToken, 1)(token)
.then(function(accessToken) { .then(function(accessToken) {
if (!accessToken) { if (!accessToken) {
throw new InvalidTokenError('Invalid token: access token is invalid'); 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) { 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) { if (!scope) {
throw new InvalidScopeError('Invalid scope: scope is invalid'); 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 Original file line 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 InvalidScopeError = require('../errors/invalid-scope-error');
var OAuthError = require('../errors/oauth-error'); var OAuthError = require('../errors/oauth-error');
var Promise = require('bluebird'); var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var Request = require('../request'); var Request = require('../request');
var Response = require('../response'); var Response = require('../response');
var ServerError = require('../errors/server-error'); var ServerError = require('../errors/server-error');
Expand Down Expand Up @@ -113,7 +114,6 @@ AuthorizeHandler.prototype.handle = function(request, response) {
if (!(e instanceof OAuthError)) { if (!(e instanceof OAuthError)) {
e = new ServerError(e); e = new ServerError(e);
} }

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


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


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

return tokenUtil.generateRandomToken(); return tokenUtil.generateRandomToken();
}; };


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


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

return expires; return expires;
}; };


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

return promisify(this.model.getClient, 2)(clientId, null)
return Promise.try(this.model.getClient, clientId)
.then(function(client) { .then(function(client) {
if (!client) { if (!client) {
throw new InvalidClientError('Invalid client: client credentials are invalid'); 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)) { if (redirectUri && !_.contains(client.redirectUris, redirectUri)) {
throw new InvalidClientError('Invalid client: `redirect_uri` does not match client value'); throw new InvalidClientError('Invalid client: `redirect_uri` does not match client value');
} }

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

return promisify(this.authenticateHandler.handle, 2)(request, response).then(function(user) {
return Promise.try(this.authenticateHandler.handle, [request, response]).then(function(user) {
if (!user) { if (!user) {
throw new ServerError('Server error: `handle()` did not return a `user` object'); 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, redirectUri: redirectUri,
scope: scope scope: scope
}; };

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


/** /**
Expand Down
3 changes: 2 additions & 1 deletion lib/handlers/token-handler.js
Original file line number Original file line 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 InvalidRequestError = require('../errors/invalid-request-error');
var OAuthError = require('../errors/oauth-error'); var OAuthError = require('../errors/oauth-error');
var Promise = require('bluebird'); var Promise = require('bluebird');
var promisify = require('promisify-any').use(Promise);
var Request = require('../request'); var Request = require('../request');
var Response = require('../response'); var Response = require('../response');
var ServerError = require('../errors/server-error'); 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`'); 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) { .then(function(client) {
if (!client) { if (!client) {
throw new InvalidClientError('Invalid client: client is invalid'); throw new InvalidClientError('Invalid client: client is invalid');
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"bluebird": "^2.9.13", "bluebird": "^2.9.13",
"camel-case": "^1.1.1", "camel-case": "^1.1.1",
"lodash": "^3.3.1", "lodash": "^3.3.1",
"promisify-any": "2.0.1",
"standard-http-error": "^1.1.0", "standard-http-error": "^1.1.0",
"type-is": "^1.6.0", "type-is": "^1.6.0",
"validator.js": "^1.1.1" "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 Original file line 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); 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() { describe('getAuthorizationCode()', function() {
Expand Down Expand Up @@ -374,6 +387,20 @@ describe('AuthorizationCodeGrantType integration', function() {


grantType.getAuthorizationCode(request, client).should.be.an.instanceOf(Promise); 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() { describe('validateRedirectUri()', function() {
Expand Down Expand Up @@ -458,6 +485,18 @@ describe('AuthorizationCodeGrantType integration', function() {


grantType.revokeAuthorizationCode(authorizationCode).should.be.an.instanceOf(Promise); 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() { describe('saveToken()', function() {
Expand Down Expand Up @@ -501,5 +540,17 @@ describe('AuthorizationCodeGrantType integration', function() {


grantType.saveToken(token).should.be.an.instanceOf(Promise); 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