From 5f48a5ce060e22ae8032ea78a781518d5a7cd411 Mon Sep 17 00:00:00 2001 From: Daniel Bell Date: Wed, 7 Jun 2017 23:00:08 +1000 Subject: [PATCH] Pass correct "this" to the model. --- lib/grant-types/abstract-grant-type.js | 6 +++--- lib/grant-types/authorization-code-grant-type.js | 6 +++--- 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 | 6 +++--- lib/handlers/authenticate-handler.js | 4 ++-- lib/handlers/authorize-handler.js | 6 +++--- lib/handlers/token-handler.js | 2 +- package.json | 2 +- test/unit/grant-types/abstract-grant-type_test.js | 2 ++ .../unit/grant-types/authorization-code-grant-type_test.js | 3 +++ .../unit/grant-types/client-credentials-grant-type_test.js | 2 ++ test/unit/grant-types/password-grant-type_test.js | 2 ++ test/unit/grant-types/refresh-token-grant-type_test.js | 7 +++++++ test/unit/handlers/authenticate-handler_test.js | 2 ++ test/unit/handlers/authorize-handler_test.js | 3 +++ test/unit/handlers/token-handler_test.js | 1 + 17 files changed, 42 insertions(+), 20 deletions(-) diff --git a/lib/grant-types/abstract-grant-type.js b/lib/grant-types/abstract-grant-type.js index 3158ce48b..be4259dec 100644 --- a/lib/grant-types/abstract-grant-type.js +++ b/lib/grant-types/abstract-grant-type.js @@ -38,7 +38,7 @@ function AbstractGrantType(options) { AbstractGrantType.prototype.generateAccessToken = function(client, user, scope) { if (this.model.generateAccessToken) { - return promisify(this.model.generateAccessToken, 3)(client, user, scope) + return promisify(this.model.generateAccessToken, 3).call(this.model, client, user, scope) .then(function(accessToken) { return accessToken || tokenUtil.generateRandomToken(); }); @@ -53,7 +53,7 @@ AbstractGrantType.prototype.generateAccessToken = function(client, user, scope) AbstractGrantType.prototype.generateRefreshToken = function(client, user, scope) { if (this.model.generateRefreshToken) { - return promisify(this.model.generateRefreshToken, 3)(client, user, scope) + return promisify(this.model.generateRefreshToken, 3).call(this.model, client, user, scope) .then(function(refreshToken) { return refreshToken || tokenUtil.generateRandomToken(); }); @@ -103,7 +103,7 @@ AbstractGrantType.prototype.getScope = function(request) { */ AbstractGrantType.prototype.validateScope = function(user, client, scope) { if (this.model.validateScope) { - return promisify(this.model.validateScope, 3)(user, client, scope) + return promisify(this.model.validateScope, 3).call(this.model, user, client, scope) .then(function (scope) { if (!scope) { throw new InvalidScopeError('Invalid scope: Requested scope is invalid'); diff --git a/lib/grant-types/authorization-code-grant-type.js b/lib/grant-types/authorization-code-grant-type.js index 1bd20c804..7eae70f8f 100644 --- a/lib/grant-types/authorization-code-grant-type.js +++ b/lib/grant-types/authorization-code-grant-type.js @@ -88,7 +88,7 @@ AuthorizationCodeGrantType.prototype.getAuthorizationCode = function(request, cl if (!is.vschar(request.body.code)) { throw new InvalidRequestError('Invalid parameter: `code`'); } - return promisify(this.model.getAuthorizationCode, 1)(request.body.code) + return promisify(this.model.getAuthorizationCode, 1).call(this.model, request.body.code) .then(function(code) { if (!code) { throw new InvalidGrantError('Invalid grant: authorization code is invalid'); @@ -160,7 +160,7 @@ AuthorizationCodeGrantType.prototype.getAuthorizationCode = function(request, cl */ AuthorizationCodeGrantType.prototype.revokeAuthorizationCode = function(code) { - return promisify(this.model.revokeAuthorizationCode, 1)(code) + return promisify(this.model.revokeAuthorizationCode, 1).call(this.model, code) .then(function(status) { if (!status) { throw new InvalidGrantError('Invalid grant: authorization code is invalid'); @@ -195,7 +195,7 @@ AuthorizationCodeGrantType.prototype.saveToken = function(user, client, authoriz scope: scope }; - return promisify(this.model.saveToken, 3)(token, client, user); + return promisify(this.model.saveToken, 3).call(this.model, token, client, user); }); }; diff --git a/lib/grant-types/client-credentials-grant-type.js b/lib/grant-types/client-credentials-grant-type.js index 2c9e1a6ae..138333e50 100644 --- a/lib/grant-types/client-credentials-grant-type.js +++ b/lib/grant-types/client-credentials-grant-type.js @@ -70,7 +70,7 @@ ClientCredentialsGrantType.prototype.handle = function(request, client) { */ ClientCredentialsGrantType.prototype.getUserFromClient = function(client) { - return promisify(this.model.getUserFromClient, 1)(client) + return promisify(this.model.getUserFromClient, 1).call(this.model, client) .then(function(user) { if (!user) { throw new InvalidGrantError('Invalid grant: user credentials are invalid'); @@ -100,7 +100,7 @@ ClientCredentialsGrantType.prototype.saveToken = function(user, client, scope) { scope: scope }; - return promisify(this.model.saveToken, 3)(token, client, user); + return promisify(this.model.saveToken, 3).call(this.model, token, client, user); }); }; diff --git a/lib/grant-types/password-grant-type.js b/lib/grant-types/password-grant-type.js index db0f2e107..b7f17935b 100644 --- a/lib/grant-types/password-grant-type.js +++ b/lib/grant-types/password-grant-type.js @@ -88,7 +88,7 @@ PasswordGrantType.prototype.getUser = function(request) { throw new InvalidRequestError('Invalid parameter: `password`'); } - return promisify(this.model.getUser, 2)(request.body.username, request.body.password) + return promisify(this.model.getUser, 2).call(this.model, request.body.username, request.body.password) .then(function(user) { if (!user) { throw new InvalidGrantError('Invalid grant: user credentials are invalid'); @@ -122,7 +122,7 @@ PasswordGrantType.prototype.saveToken = function(user, client, scope) { scope: scope }; - return promisify(this.model.saveToken, 3)(token, client, user); + return promisify(this.model.saveToken, 3).call(this.model, token, client, user); }); }; diff --git a/lib/grant-types/refresh-token-grant-type.js b/lib/grant-types/refresh-token-grant-type.js index 2379a2239..19f9010c2 100644 --- a/lib/grant-types/refresh-token-grant-type.js +++ b/lib/grant-types/refresh-token-grant-type.js @@ -86,7 +86,7 @@ RefreshTokenGrantType.prototype.getRefreshToken = function(request, client) { throw new InvalidRequestError('Invalid parameter: `refresh_token`'); } - return promisify(this.model.getRefreshToken, 1)(request.body.refresh_token) + return promisify(this.model.getRefreshToken, 1).call(this.model, request.body.refresh_token) .then(function(token) { if (!token) { throw new InvalidGrantError('Invalid grant: refresh token is invalid'); @@ -127,7 +127,7 @@ RefreshTokenGrantType.prototype.revokeToken = function(token) { return Promise.resolve(token); } - return promisify(this.model.revokeToken, 1)(token) + return promisify(this.model.revokeToken, 1).call(this.model, token) .then(function(status) { if (!status) { throw new InvalidGrantError('Invalid grant: refresh token is invalid'); @@ -166,7 +166,7 @@ RefreshTokenGrantType.prototype.saveToken = function(user, client, scope) { return token; }) .then(function(token) { - return Promise.try(promisify(this.model.saveToken, 3), [token, client, user]) + return promisify(this.model.saveToken, 3).call(this.model, token, client, user) .then(function(savedToken) { return savedToken; }); diff --git a/lib/handlers/authenticate-handler.js b/lib/handlers/authenticate-handler.js index fb7488fb6..dc9117b27 100644 --- a/lib/handlers/authenticate-handler.js +++ b/lib/handlers/authenticate-handler.js @@ -197,7 +197,7 @@ AuthenticateHandler.prototype.getTokenFromRequestBody = function(request) { */ AuthenticateHandler.prototype.getAccessToken = function(token) { - return promisify(this.model.getAccessToken, 1)(token) + return promisify(this.model.getAccessToken, 1).call(this.model, token) .then(function(accessToken) { if (!accessToken) { throw new InvalidTokenError('Invalid token: access token is invalid'); @@ -232,7 +232,7 @@ AuthenticateHandler.prototype.validateAccessToken = function(accessToken) { */ AuthenticateHandler.prototype.verifyScope = function(accessToken) { - return promisify(this.model.verifyScope, 2)(accessToken, this.scope) + return promisify(this.model.verifyScope, 2).call(this.model, accessToken, this.scope) .then(function(scope) { if (!scope) { throw new InsufficientScopeError('Insufficient scope: authorized scope is insufficient'); diff --git a/lib/handlers/authorize-handler.js b/lib/handlers/authorize-handler.js index bc0d132e4..984136a8d 100644 --- a/lib/handlers/authorize-handler.js +++ b/lib/handlers/authorize-handler.js @@ -135,7 +135,7 @@ AuthorizeHandler.prototype.handle = function(request, response) { AuthorizeHandler.prototype.generateAuthorizationCode = function(client, user, scope) { if (this.model.generateAuthorizationCode) { - return promisify(this.model.generateAuthorizationCode)(client, user, scope); + return promisify(this.model.generateAuthorizationCode).call(this.model, client, user, scope); } return tokenUtil.generateRandomToken(); }; @@ -171,7 +171,7 @@ AuthorizeHandler.prototype.getClient = function(request) { if (redirectUri && !is.uri(redirectUri)) { throw new InvalidRequestError('Invalid request: `redirect_uri` is not a valid URI'); } - return promisify(this.model.getClient, 2)(clientId, null) + return promisify(this.model.getClient, 2).call(this.model, clientId, null) .then(function(client) { if (!client) { throw new InvalidClientError('Invalid client: client credentials are invalid'); @@ -264,7 +264,7 @@ AuthorizeHandler.prototype.saveAuthorizationCode = function(authorizationCode, e redirectUri: redirectUri, scope: scope }; - return promisify(this.model.saveAuthorizationCode, 3)(code, client, user); + return promisify(this.model.saveAuthorizationCode, 3).call(this.model, code, client, user); }; /** diff --git a/lib/handlers/token-handler.js b/lib/handlers/token-handler.js index 65b7faa53..feaad3f54 100644 --- a/lib/handlers/token-handler.js +++ b/lib/handlers/token-handler.js @@ -132,7 +132,7 @@ TokenHandler.prototype.getClient = function(request, response) { throw new InvalidRequestError('Invalid parameter: `client_secret`'); } - return promisify(this.model.getClient, 2)(credentials.clientId, credentials.clientSecret) + return promisify(this.model.getClient, 2).call(this.model, credentials.clientId, credentials.clientSecret) .then(function(client) { if (!client) { throw new InvalidClientError('Invalid client: client is invalid'); diff --git a/package.json b/package.json index e98113c25..d6c303778 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "jshint": "2.9.4", "mocha": "3.3.0", "should": "11.2.1", - "sinon": "2.1.0" + "sinon": "2.3.2" }, "license": "MIT", "engines": { diff --git a/test/unit/grant-types/abstract-grant-type_test.js b/test/unit/grant-types/abstract-grant-type_test.js index cffeab91d..528ca4041 100644 --- a/test/unit/grant-types/abstract-grant-type_test.js +++ b/test/unit/grant-types/abstract-grant-type_test.js @@ -23,6 +23,7 @@ describe('AbstractGrantType', function() { return handler.generateAccessToken() .then(function() { model.generateAccessToken.callCount.should.equal(1); + model.generateAccessToken.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); @@ -38,6 +39,7 @@ describe('AbstractGrantType', function() { return handler.generateRefreshToken() .then(function() { model.generateRefreshToken.callCount.should.equal(1); + model.generateRefreshToken.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); diff --git a/test/unit/grant-types/authorization-code-grant-type_test.js b/test/unit/grant-types/authorization-code-grant-type_test.js index 87c3e3a38..480416e68 100644 --- a/test/unit/grant-types/authorization-code-grant-type_test.js +++ b/test/unit/grant-types/authorization-code-grant-type_test.js @@ -31,6 +31,7 @@ describe('AuthorizationCodeGrantType', function() { model.getAuthorizationCode.callCount.should.equal(1); model.getAuthorizationCode.firstCall.args.should.have.length(1); model.getAuthorizationCode.firstCall.args[0].should.equal(12345); + model.getAuthorizationCode.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); @@ -51,6 +52,7 @@ describe('AuthorizationCodeGrantType', function() { model.revokeAuthorizationCode.callCount.should.equal(1); model.revokeAuthorizationCode.firstCall.args.should.have.length(1); model.revokeAuthorizationCode.firstCall.args[0].should.equal(authorizationCode); + model.revokeAuthorizationCode.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); @@ -80,6 +82,7 @@ describe('AuthorizationCodeGrantType', function() { model.saveToken.firstCall.args[0].should.eql({ accessToken: 'foo', authorizationCode: 'foobar', accessTokenExpiresAt: 'biz', refreshToken: 'bar', refreshTokenExpiresAt: 'baz', scope: 'foobiz' }); model.saveToken.firstCall.args[1].should.equal(client); model.saveToken.firstCall.args[2].should.equal(user); + model.saveToken.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); diff --git a/test/unit/grant-types/client-credentials-grant-type_test.js b/test/unit/grant-types/client-credentials-grant-type_test.js index 1f0e4a772..fe1fc4840 100644 --- a/test/unit/grant-types/client-credentials-grant-type_test.js +++ b/test/unit/grant-types/client-credentials-grant-type_test.js @@ -27,6 +27,7 @@ describe('ClientCredentialsGrantType', function() { model.getUserFromClient.callCount.should.equal(1); model.getUserFromClient.firstCall.args.should.have.length(1); model.getUserFromClient.firstCall.args[0].should.equal(client); + model.getUserFromClient.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); @@ -53,6 +54,7 @@ describe('ClientCredentialsGrantType', function() { model.saveToken.firstCall.args[0].should.eql({ accessToken: 'foo', accessTokenExpiresAt: 'biz', scope: 'foobar' }); model.saveToken.firstCall.args[1].should.equal(client); model.saveToken.firstCall.args[2].should.equal(user); + model.saveToken.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); diff --git a/test/unit/grant-types/password-grant-type_test.js b/test/unit/grant-types/password-grant-type_test.js index c45670ae9..8e3bfc84e 100644 --- a/test/unit/grant-types/password-grant-type_test.js +++ b/test/unit/grant-types/password-grant-type_test.js @@ -29,6 +29,7 @@ describe('PasswordGrantType', function() { model.getUser.firstCall.args.should.have.length(2); model.getUser.firstCall.args[0].should.equal('foo'); model.getUser.firstCall.args[1].should.equal('bar'); + model.getUser.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); @@ -57,6 +58,7 @@ describe('PasswordGrantType', function() { model.saveToken.firstCall.args[0].should.eql({ accessToken: 'foo', accessTokenExpiresAt: 'biz', refreshToken: 'bar', refreshTokenExpiresAt: 'baz', scope: 'foobar' }); model.saveToken.firstCall.args[1].should.equal(client); model.saveToken.firstCall.args[2].should.equal(user); + model.saveToken.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); diff --git a/test/unit/grant-types/refresh-token-grant-type_test.js b/test/unit/grant-types/refresh-token-grant-type_test.js index 999377f57..e5693ba8f 100644 --- a/test/unit/grant-types/refresh-token-grant-type_test.js +++ b/test/unit/grant-types/refresh-token-grant-type_test.js @@ -31,6 +31,7 @@ describe('RefreshTokenGrantType', function() { model.revokeToken.callCount.should.equal(1); model.revokeToken.firstCall.args.should.have.length(1); model.revokeToken.firstCall.args[0].should.equal(token); + model.revokeToken.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); @@ -52,6 +53,7 @@ describe('RefreshTokenGrantType', function() { model.getRefreshToken.callCount.should.equal(1); model.getRefreshToken.firstCall.args.should.have.length(1); model.getRefreshToken.firstCall.args[0].should.equal('bar'); + model.getRefreshToken.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); @@ -72,6 +74,7 @@ describe('RefreshTokenGrantType', function() { model.revokeToken.callCount.should.equal(1); model.revokeToken.firstCall.args.should.have.length(1); model.revokeToken.firstCall.args[0].should.equal(token); + model.revokeToken.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); @@ -106,6 +109,7 @@ describe('RefreshTokenGrantType', function() { model.revokeToken.callCount.should.equal(1); model.revokeToken.firstCall.args.should.have.length(1); model.revokeToken.firstCall.args[0].should.equal(token); + model.revokeToken.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); @@ -134,6 +138,7 @@ describe('RefreshTokenGrantType', function() { model.saveToken.firstCall.args[0].should.eql({ accessToken: 'foo', accessTokenExpiresAt: 'biz', refreshToken: 'bar', refreshTokenExpiresAt: 'baz', scope: 'foobar' }); model.saveToken.firstCall.args[1].should.equal(client); model.saveToken.firstCall.args[2].should.equal(user); + model.saveToken.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); @@ -160,6 +165,7 @@ describe('RefreshTokenGrantType', function() { model.saveToken.firstCall.args[0].should.eql({ accessToken: 'foo', accessTokenExpiresAt: 'biz', scope: 'foobar' }); model.saveToken.firstCall.args[1].should.equal(client); model.saveToken.firstCall.args[2].should.equal(user); + model.saveToken.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); @@ -186,6 +192,7 @@ describe('RefreshTokenGrantType', function() { model.saveToken.firstCall.args[0].should.eql({ accessToken: 'foo', accessTokenExpiresAt: 'biz', refreshToken: 'bar', refreshTokenExpiresAt: 'baz', scope: 'foobar' }); model.saveToken.firstCall.args[1].should.equal(client); model.saveToken.firstCall.args[2].should.equal(user); + model.saveToken.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); diff --git a/test/unit/handlers/authenticate-handler_test.js b/test/unit/handlers/authenticate-handler_test.js index 39ebd0aa8..2adac7884 100644 --- a/test/unit/handlers/authenticate-handler_test.js +++ b/test/unit/handlers/authenticate-handler_test.js @@ -89,6 +89,7 @@ describe('AuthenticateHandler', function() { model.getAccessToken.callCount.should.equal(1); model.getAccessToken.firstCall.args.should.have.length(1); model.getAccessToken.firstCall.args[0].should.equal('foo'); + model.getAccessToken.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); @@ -144,6 +145,7 @@ describe('AuthenticateHandler', function() { model.verifyScope.callCount.should.equal(1); model.verifyScope.firstCall.args.should.have.length(2); model.verifyScope.firstCall.args[0].should.equal('foo', 'bar'); + model.verifyScope.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); diff --git a/test/unit/handlers/authorize-handler_test.js b/test/unit/handlers/authorize-handler_test.js index ee8c41773..fe9b6b1d7 100644 --- a/test/unit/handlers/authorize-handler_test.js +++ b/test/unit/handlers/authorize-handler_test.js @@ -29,6 +29,7 @@ describe('AuthorizeHandler', function() { return handler.generateAuthorizationCode() .then(function() { model.generateAuthorizationCode.callCount.should.equal(1); + model.generateAuthorizationCode.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); @@ -49,6 +50,7 @@ describe('AuthorizeHandler', function() { model.getClient.callCount.should.equal(1); model.getClient.firstCall.args.should.have.length(2); model.getClient.firstCall.args[0].should.equal(12345); + model.getClient.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); @@ -92,6 +94,7 @@ describe('AuthorizeHandler', function() { model.saveAuthorizationCode.firstCall.args[0].should.eql({ authorizationCode: 'foo', expiresAt: 'bar', redirectUri: 'baz', scope: 'qux' }); model.saveAuthorizationCode.firstCall.args[1].should.equal('biz'); model.saveAuthorizationCode.firstCall.args[2].should.equal('boz'); + model.saveAuthorizationCode.firstCall.thisValue.should.equal(model); }) .catch(should.fail); }); diff --git a/test/unit/handlers/token-handler_test.js b/test/unit/handlers/token-handler_test.js index 3642725af..2b37cd05a 100644 --- a/test/unit/handlers/token-handler_test.js +++ b/test/unit/handlers/token-handler_test.js @@ -29,6 +29,7 @@ describe('TokenHandler', function() { model.getClient.firstCall.args.should.have.length(2); model.getClient.firstCall.args[0].should.equal(12345); model.getClient.firstCall.args[1].should.equal('secret'); + model.getClient.firstCall.thisValue.should.equal(model); }) .catch(should.fail); });