From 0d4f4e0c6d4fc070ac725733b1133548e9693c88 Mon Sep 17 00:00:00 2001 From: Michael Salinger Date: Thu, 10 Mar 2016 06:55:31 -0500 Subject: [PATCH 1/3] Conditionally promisify all model functions and run saveToken as a promise --- test/integration/handlers/token-handler_test.js | 4 ++-- test/integration/server_test.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/integration/handlers/token-handler_test.js b/test/integration/handlers/token-handler_test.js index ad996e8eb..6f5df2792 100644 --- a/test/integration/handlers/token-handler_test.js +++ b/test/integration/handlers/token-handler_test.js @@ -678,8 +678,8 @@ describe('TokenHandler integration', function() { it('should throw an invalid grant error if a non-oauth error is thrown', function() { var client = { grants: ['password'] }; var model = { - getClient: function() {}, - getUser: function() {}, + getClient: function(clientId, password, callback) { callback(null, client); }, + getUser: function(uid, pwd, callback) { callback(); }, saveToken: function() {} }; var handler = new TokenHandler({ accessTokenLifetime: 120, model: model, refreshTokenLifetime: 120 }); diff --git a/test/integration/server_test.js b/test/integration/server_test.js index fea78c214..a62420686 100644 --- a/test/integration/server_test.js +++ b/test/integration/server_test.js @@ -58,8 +58,8 @@ describe('Server integration', function() { it('should return a promise', function() { var model = { - getAccessToken: function() { - return { user: {} }; + getAccessToken: function(token, callback) { + callback(null, { user: {} }); } }; var server = new Server({ model: model }); From 19c99ecdace98c093bde72291126c5b04b35854a Mon Sep 17 00:00:00 2001 From: Michael Salinger Date: Thu, 26 May 2016 14:39:10 -0400 Subject: [PATCH 2/3] Added client, user, and scope as arguments to generateAccessToken and generateRefreshToken --- lib/grant-types/abstract-grant-type.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/grant-types/abstract-grant-type.js b/lib/grant-types/abstract-grant-type.js index 7bf0ac75d..d819618c4 100644 --- a/lib/grant-types/abstract-grant-type.js +++ b/lib/grant-types/abstract-grant-type.js @@ -35,9 +35,12 @@ function AbstractGrantType(options) { * Generate access token. */ -AbstractGrantType.prototype.generateAccessToken = function() { +AbstractGrantType.prototype.generateAccessToken = function(client, user, scope) { if (this.model.generateAccessToken) { - return promisify(this.model.generateAccessToken)(); + return promisify(this.model.generateAccessToken, 3)(client, user, scope) + .then(function(accessToken) { + return accessToken || tokenUtil.generateRandomToken(); + }); } return tokenUtil.generateRandomToken(); @@ -47,9 +50,12 @@ AbstractGrantType.prototype.generateAccessToken = function() { * Generate refresh token. */ -AbstractGrantType.prototype.generateRefreshToken = function() { +AbstractGrantType.prototype.generateRefreshToken = function(client, user, scope) { if (this.model.generateRefreshToken) { - return promisify(this.model.generateRefreshToken)(); + return promisify(this.model.generateRefreshToken, 3)(client, user, scope) + .then(function(refreshToken) { + return refreshToken || tokenUtil.generateRandomToken(); + }); } return tokenUtil.generateRandomToken(); From e018193e79a32ede6fbde9bb08bc57adb3b702fe Mon Sep 17 00:00:00 2001 From: Michael Salinger Date: Fri, 3 Jun 2016 10:58:47 -0400 Subject: [PATCH 3/3] Added generate token parameters for all grant types --- lib/grant-types/authorization-code-grant-type.js | 4 ++-- lib/grant-types/client-credentials-grant-type.js | 4 ++-- lib/grant-types/password-grant-type.js | 4 ++-- lib/grant-types/refresh-token-grant-type.js | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/grant-types/authorization-code-grant-type.js b/lib/grant-types/authorization-code-grant-type.js index c91ff7625..55ec7acb8 100644 --- a/lib/grant-types/authorization-code-grant-type.js +++ b/lib/grant-types/authorization-code-grant-type.js @@ -177,8 +177,8 @@ AuthorizationCodeGrantType.prototype.revokeAuthorizationCode = function(code) { AuthorizationCodeGrantType.prototype.saveToken = function(user, client, authorizationCode, scope) { var fns = [ this.validateScope(user, client, scope), - this.generateAccessToken(), - this.generateRefreshToken() + this.generateAccessToken(client, user, scope), + this.generateRefreshToken(client, user, scope) ]; return Promise.all(fns) diff --git a/lib/grant-types/client-credentials-grant-type.js b/lib/grant-types/client-credentials-grant-type.js index b101bc9bd..2c9e1a6ae 100644 --- a/lib/grant-types/client-credentials-grant-type.js +++ b/lib/grant-types/client-credentials-grant-type.js @@ -87,8 +87,8 @@ ClientCredentialsGrantType.prototype.getUserFromClient = function(client) { ClientCredentialsGrantType.prototype.saveToken = function(user, client, scope) { var fns = [ this.validateScope(user, client, scope), - this.generateAccessToken(), - this.getAccessTokenExpiresAt() + this.generateAccessToken(client, user, scope), + this.getAccessTokenExpiresAt(client, user, scope) ]; return Promise.all(fns) diff --git a/lib/grant-types/password-grant-type.js b/lib/grant-types/password-grant-type.js index 12040d79e..db0f2e107 100644 --- a/lib/grant-types/password-grant-type.js +++ b/lib/grant-types/password-grant-type.js @@ -105,8 +105,8 @@ PasswordGrantType.prototype.getUser = function(request) { PasswordGrantType.prototype.saveToken = function(user, client, scope) { var fns = [ this.validateScope(user, client, scope), - this.generateAccessToken(), - this.generateRefreshToken(), + this.generateAccessToken(client, user, scope), + this.generateRefreshToken(client, user, scope), this.getAccessTokenExpiresAt(), this.getRefreshTokenExpiresAt() ]; diff --git a/lib/grant-types/refresh-token-grant-type.js b/lib/grant-types/refresh-token-grant-type.js index ebdc3cfa9..273fadda3 100644 --- a/lib/grant-types/refresh-token-grant-type.js +++ b/lib/grant-types/refresh-token-grant-type.js @@ -139,8 +139,8 @@ RefreshTokenGrantType.prototype.revokeToken = function(token) { RefreshTokenGrantType.prototype.saveToken = function(user, client, scope) { var fns = [ - this.generateAccessToken(), - this.generateRefreshToken(), + this.generateAccessToken(client, user, scope), + this.generateRefreshToken(client, user, scope), this.getAccessTokenExpiresAt(), this.getRefreshTokenExpiresAt() ];