Skip to content

Commit 5f48a5c

Browse files
committed
Pass correct "this" to the model.
1 parent bddabcd commit 5f48a5c

17 files changed

+42
-20
lines changed

lib/grant-types/abstract-grant-type.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function AbstractGrantType(options) {
3838

3939
AbstractGrantType.prototype.generateAccessToken = function(client, user, scope) {
4040
if (this.model.generateAccessToken) {
41-
return promisify(this.model.generateAccessToken, 3)(client, user, scope)
41+
return promisify(this.model.generateAccessToken, 3).call(this.model, client, user, scope)
4242
.then(function(accessToken) {
4343
return accessToken || tokenUtil.generateRandomToken();
4444
});
@@ -53,7 +53,7 @@ AbstractGrantType.prototype.generateAccessToken = function(client, user, scope)
5353

5454
AbstractGrantType.prototype.generateRefreshToken = function(client, user, scope) {
5555
if (this.model.generateRefreshToken) {
56-
return promisify(this.model.generateRefreshToken, 3)(client, user, scope)
56+
return promisify(this.model.generateRefreshToken, 3).call(this.model, client, user, scope)
5757
.then(function(refreshToken) {
5858
return refreshToken || tokenUtil.generateRandomToken();
5959
});
@@ -103,7 +103,7 @@ AbstractGrantType.prototype.getScope = function(request) {
103103
*/
104104
AbstractGrantType.prototype.validateScope = function(user, client, scope) {
105105
if (this.model.validateScope) {
106-
return promisify(this.model.validateScope, 3)(user, client, scope)
106+
return promisify(this.model.validateScope, 3).call(this.model, user, client, scope)
107107
.then(function (scope) {
108108
if (!scope) {
109109
throw new InvalidScopeError('Invalid scope: Requested scope is invalid');

lib/grant-types/authorization-code-grant-type.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ AuthorizationCodeGrantType.prototype.getAuthorizationCode = function(request, cl
8888
if (!is.vschar(request.body.code)) {
8989
throw new InvalidRequestError('Invalid parameter: `code`');
9090
}
91-
return promisify(this.model.getAuthorizationCode, 1)(request.body.code)
91+
return promisify(this.model.getAuthorizationCode, 1).call(this.model, request.body.code)
9292
.then(function(code) {
9393
if (!code) {
9494
throw new InvalidGrantError('Invalid grant: authorization code is invalid');
@@ -160,7 +160,7 @@ AuthorizationCodeGrantType.prototype.getAuthorizationCode = function(request, cl
160160
*/
161161

162162
AuthorizationCodeGrantType.prototype.revokeAuthorizationCode = function(code) {
163-
return promisify(this.model.revokeAuthorizationCode, 1)(code)
163+
return promisify(this.model.revokeAuthorizationCode, 1).call(this.model, code)
164164
.then(function(status) {
165165
if (!status) {
166166
throw new InvalidGrantError('Invalid grant: authorization code is invalid');
@@ -195,7 +195,7 @@ AuthorizationCodeGrantType.prototype.saveToken = function(user, client, authoriz
195195
scope: scope
196196
};
197197

198-
return promisify(this.model.saveToken, 3)(token, client, user);
198+
return promisify(this.model.saveToken, 3).call(this.model, token, client, user);
199199
});
200200
};
201201

lib/grant-types/client-credentials-grant-type.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ ClientCredentialsGrantType.prototype.handle = function(request, client) {
7070
*/
7171

7272
ClientCredentialsGrantType.prototype.getUserFromClient = function(client) {
73-
return promisify(this.model.getUserFromClient, 1)(client)
73+
return promisify(this.model.getUserFromClient, 1).call(this.model, client)
7474
.then(function(user) {
7575
if (!user) {
7676
throw new InvalidGrantError('Invalid grant: user credentials are invalid');
@@ -100,7 +100,7 @@ ClientCredentialsGrantType.prototype.saveToken = function(user, client, scope) {
100100
scope: scope
101101
};
102102

103-
return promisify(this.model.saveToken, 3)(token, client, user);
103+
return promisify(this.model.saveToken, 3).call(this.model, token, client, user);
104104
});
105105
};
106106

lib/grant-types/password-grant-type.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ PasswordGrantType.prototype.getUser = function(request) {
8888
throw new InvalidRequestError('Invalid parameter: `password`');
8989
}
9090

91-
return promisify(this.model.getUser, 2)(request.body.username, request.body.password)
91+
return promisify(this.model.getUser, 2).call(this.model, request.body.username, request.body.password)
9292
.then(function(user) {
9393
if (!user) {
9494
throw new InvalidGrantError('Invalid grant: user credentials are invalid');
@@ -122,7 +122,7 @@ PasswordGrantType.prototype.saveToken = function(user, client, scope) {
122122
scope: scope
123123
};
124124

125-
return promisify(this.model.saveToken, 3)(token, client, user);
125+
return promisify(this.model.saveToken, 3).call(this.model, token, client, user);
126126
});
127127
};
128128

lib/grant-types/refresh-token-grant-type.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ RefreshTokenGrantType.prototype.getRefreshToken = function(request, client) {
8686
throw new InvalidRequestError('Invalid parameter: `refresh_token`');
8787
}
8888

89-
return promisify(this.model.getRefreshToken, 1)(request.body.refresh_token)
89+
return promisify(this.model.getRefreshToken, 1).call(this.model, request.body.refresh_token)
9090
.then(function(token) {
9191
if (!token) {
9292
throw new InvalidGrantError('Invalid grant: refresh token is invalid');
@@ -127,7 +127,7 @@ RefreshTokenGrantType.prototype.revokeToken = function(token) {
127127
return Promise.resolve(token);
128128
}
129129

130-
return promisify(this.model.revokeToken, 1)(token)
130+
return promisify(this.model.revokeToken, 1).call(this.model, token)
131131
.then(function(status) {
132132
if (!status) {
133133
throw new InvalidGrantError('Invalid grant: refresh token is invalid');
@@ -166,7 +166,7 @@ RefreshTokenGrantType.prototype.saveToken = function(user, client, scope) {
166166
return token;
167167
})
168168
.then(function(token) {
169-
return Promise.try(promisify(this.model.saveToken, 3), [token, client, user])
169+
return promisify(this.model.saveToken, 3).call(this.model, token, client, user)
170170
.then(function(savedToken) {
171171
return savedToken;
172172
});

lib/handlers/authenticate-handler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ AuthenticateHandler.prototype.getTokenFromRequestBody = function(request) {
197197
*/
198198

199199
AuthenticateHandler.prototype.getAccessToken = function(token) {
200-
return promisify(this.model.getAccessToken, 1)(token)
200+
return promisify(this.model.getAccessToken, 1).call(this.model, token)
201201
.then(function(accessToken) {
202202
if (!accessToken) {
203203
throw new InvalidTokenError('Invalid token: access token is invalid');
@@ -232,7 +232,7 @@ AuthenticateHandler.prototype.validateAccessToken = function(accessToken) {
232232
*/
233233

234234
AuthenticateHandler.prototype.verifyScope = function(accessToken) {
235-
return promisify(this.model.verifyScope, 2)(accessToken, this.scope)
235+
return promisify(this.model.verifyScope, 2).call(this.model, accessToken, this.scope)
236236
.then(function(scope) {
237237
if (!scope) {
238238
throw new InsufficientScopeError('Insufficient scope: authorized scope is insufficient');

lib/handlers/authorize-handler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ AuthorizeHandler.prototype.handle = function(request, response) {
135135

136136
AuthorizeHandler.prototype.generateAuthorizationCode = function(client, user, scope) {
137137
if (this.model.generateAuthorizationCode) {
138-
return promisify(this.model.generateAuthorizationCode)(client, user, scope);
138+
return promisify(this.model.generateAuthorizationCode).call(this.model, client, user, scope);
139139
}
140140
return tokenUtil.generateRandomToken();
141141
};
@@ -171,7 +171,7 @@ AuthorizeHandler.prototype.getClient = function(request) {
171171
if (redirectUri && !is.uri(redirectUri)) {
172172
throw new InvalidRequestError('Invalid request: `redirect_uri` is not a valid URI');
173173
}
174-
return promisify(this.model.getClient, 2)(clientId, null)
174+
return promisify(this.model.getClient, 2).call(this.model, clientId, null)
175175
.then(function(client) {
176176
if (!client) {
177177
throw new InvalidClientError('Invalid client: client credentials are invalid');
@@ -264,7 +264,7 @@ AuthorizeHandler.prototype.saveAuthorizationCode = function(authorizationCode, e
264264
redirectUri: redirectUri,
265265
scope: scope
266266
};
267-
return promisify(this.model.saveAuthorizationCode, 3)(code, client, user);
267+
return promisify(this.model.saveAuthorizationCode, 3).call(this.model, code, client, user);
268268
};
269269

270270
/**

lib/handlers/token-handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ TokenHandler.prototype.getClient = function(request, response) {
132132
throw new InvalidRequestError('Invalid parameter: `client_secret`');
133133
}
134134

135-
return promisify(this.model.getClient, 2)(credentials.clientId, credentials.clientSecret)
135+
return promisify(this.model.getClient, 2).call(this.model, credentials.clientId, credentials.clientSecret)
136136
.then(function(client) {
137137
if (!client) {
138138
throw new InvalidClientError('Invalid client: client is invalid');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"jshint": "2.9.4",
2929
"mocha": "3.3.0",
3030
"should": "11.2.1",
31-
"sinon": "2.1.0"
31+
"sinon": "2.3.2"
3232
},
3333
"license": "MIT",
3434
"engines": {

test/unit/grant-types/abstract-grant-type_test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('AbstractGrantType', function() {
2323
return handler.generateAccessToken()
2424
.then(function() {
2525
model.generateAccessToken.callCount.should.equal(1);
26+
model.generateAccessToken.firstCall.thisValue.should.equal(model);
2627
})
2728
.catch(should.fail);
2829
});
@@ -38,6 +39,7 @@ describe('AbstractGrantType', function() {
3839
return handler.generateRefreshToken()
3940
.then(function() {
4041
model.generateRefreshToken.callCount.should.equal(1);
42+
model.generateRefreshToken.firstCall.thisValue.should.equal(model);
4143
})
4244
.catch(should.fail);
4345
});

0 commit comments

Comments
 (0)