Skip to content

Commit b97f6c7

Browse files
authored
Merge pull request #256 from jorenvandeweyer/feature/pass-client-to-get-user
Support for retrieving user based on client
2 parents 782af2a + d012193 commit b97f6c7

File tree

5 files changed

+33
-22
lines changed

5 files changed

+33
-22
lines changed

docs/model/spec.rst

+10-8
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ The return value (``client``) can carry additional properties that will be ignor
441441

442442
.. _Model#getUser:
443443

444-
``getUser(username, password)``
444+
``getUser(username, password, client)``
445445
===========================================
446446

447447
Invoked to retrieve a user using a username/password combination.
@@ -454,13 +454,15 @@ This model function is **required** if the ``password`` grant is used.
454454

455455
**Arguments:**
456456

457-
+------------+----------+---------------------------------------------------------------------+
458-
| Name | Type | Description |
459-
+============+==========+=====================================================================+
460-
| username | String | The username of the user to retrieve. |
461-
+------------+----------+---------------------------------------------------------------------+
462-
| password | String | The user's password. |
463-
+------------+----------+---------------------------------------------------------------------+
457+
+-------------------+----------+---------------------------------------------------------------------+
458+
| Name | Type | Description |
459+
+===================+==========+=====================================================================+
460+
| username | String | The username of the user to retrieve. |
461+
+-------------------+----------+---------------------------------------------------------------------+
462+
| password | String | The user's password. |
463+
+-------------------+----------+---------------------------------------------------------------------+
464+
| client (optional) | Client | The client. |
465+
+-------------------+----------+---------------------------------------------------------------------+
464466

465467
**Return value:**
466468

index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ declare namespace OAuth2Server {
334334
* Invoked to retrieve a user using a username/password combination.
335335
*
336336
*/
337-
getUser(username: string, password: string): Promise<User | Falsey>;
337+
getUser(username: string, password: string, client: Client): Promise<User | Falsey>;
338338

339339
/**
340340
* Invoked to check if the requested scope is valid for a particular client/user combination.

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class PasswordGrantType extends AbstractGrantType {
4747
}
4848

4949
const scope = this.getScope(request);
50-
const user = await this.getUser(request);
50+
const user = await this.getUser(request, client);
5151

5252
return this.saveToken(user, client, scope);
5353
}
@@ -56,7 +56,7 @@ class PasswordGrantType extends AbstractGrantType {
5656
* Get user using a username/password combination.
5757
*/
5858

59-
async getUser(request) {
59+
async getUser(request, client) {
6060
if (!request.body.username) {
6161
throw new InvalidRequestError('Missing parameter: `username`');
6262
}
@@ -73,7 +73,7 @@ class PasswordGrantType extends AbstractGrantType {
7373
throw new InvalidRequestError('Invalid parameter: `password`');
7474
}
7575

76-
const user = await this.model.getUser(request.body.username, request.body.password);
76+
const user = await this.model.getUser(request.body.username, request.body.password, client);
7777

7878
if (!user) {
7979
throw new InvalidGrantError('Invalid grant: user credentials are invalid');

test/integration/grant-types/password-grant-type_test.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,12 @@ describe('PasswordGrantType integration', function() {
177177
getUser: () => should.fail(),
178178
saveToken: () => should.fail()
179179
};
180+
const client = { id: 'foobar' };
180181
const grantType = new PasswordGrantType({ accessTokenLifetime: 123, model });
181182
const request = new Request({ body: {}, headers: {}, method: {}, query: {} });
182183

183184
try {
184-
await grantType.getUser(request);
185+
await grantType.getUser(request, client);
185186

186187
should.fail();
187188
} catch (e) {
@@ -195,11 +196,12 @@ describe('PasswordGrantType integration', function() {
195196
getUser: () => should.fail(),
196197
saveToken: () => should.fail()
197198
};
199+
const client = { id: 'foobar' };
198200
const grantType = new PasswordGrantType({ accessTokenLifetime: 123, model });
199201
const request = new Request({ body: { username: 'foo' }, headers: {}, method: {}, query: {} });
200202

201203
try {
202-
await grantType.getUser(request);
204+
await grantType.getUser(request, client);
203205

204206
should.fail();
205207
} catch (e) {
@@ -213,11 +215,12 @@ describe('PasswordGrantType integration', function() {
213215
getUser: () => should.fail(),
214216
saveToken: () => should.fail()
215217
};
218+
const client = { id: 'foobar' };
216219
const grantType = new PasswordGrantType({ accessTokenLifetime: 123, model });
217220
const request = new Request({ body: { username: '\r\n', password: 'foobar' }, headers: {}, method: {}, query: {} });
218221

219222
try {
220-
await grantType.getUser(request);
223+
await grantType.getUser(request, client);
221224

222225
should.fail();
223226
} catch (e) {
@@ -231,11 +234,12 @@ describe('PasswordGrantType integration', function() {
231234
getUser: () => should.fail(),
232235
saveToken: () => should.fail()
233236
};
237+
const client = { id: 'foobar' };
234238
const grantType = new PasswordGrantType({ accessTokenLifetime: 123, model });
235239
const request = new Request({ body: { username: 'foobar', password: '\r\n' }, headers: {}, method: {}, query: {} });
236240

237241
try {
238-
await grantType.getUser(request);
242+
await grantType.getUser(request, client);
239243

240244
should.fail();
241245
} catch (e) {
@@ -249,11 +253,12 @@ describe('PasswordGrantType integration', function() {
249253
getUser: async () => undefined,
250254
saveToken: () => should.fail()
251255
};
256+
const client = { id: 'foobar' };
252257
const grantType = new PasswordGrantType({ accessTokenLifetime: 123, model });
253258
const request = new Request({ body: { username: 'foo', password: 'bar' }, headers: {}, method: {}, query: {} });
254259

255260
try {
256-
await grantType.getUser(request);
261+
await grantType.getUser(request, client);
257262
should.fail();
258263
} catch (e) {
259264
e.should.be.an.instanceOf(InvalidGrantError);
@@ -263,6 +268,7 @@ describe('PasswordGrantType integration', function() {
263268

264269
it('should return a user', async function() {
265270
const user = { email: '[email protected]' };
271+
const client = { id: 'foobar' };
266272
const model = {
267273
getUser: function(username, password) {
268274
username.should.equal('foo');
@@ -274,32 +280,34 @@ describe('PasswordGrantType integration', function() {
274280
const grantType = new PasswordGrantType({ accessTokenLifetime: 123, model });
275281
const request = new Request({ body: { username: 'foo', password: 'bar' }, headers: {}, method: {}, query: {} });
276282

277-
const data = await grantType.getUser(request);
283+
const data = await grantType.getUser(request, client);
278284
data.should.equal(user);
279285
});
280286

281287
it('should support promises', function() {
282288
const user = { email: '[email protected]' };
289+
const client = { id: 'foobar' };
283290
const model = {
284291
getUser: async function() { return user; },
285292
saveToken: () => should.fail()
286293
};
287294
const grantType = new PasswordGrantType({ accessTokenLifetime: 123, model });
288295
const request = new Request({ body: { username: 'foo', password: 'bar' }, headers: {}, method: {}, query: {} });
289296

290-
grantType.getUser(request).should.be.an.instanceOf(Promise);
297+
grantType.getUser(request, client).should.be.an.instanceOf(Promise);
291298
});
292299

293300
it('should support non-promises', function() {
294301
const user = { email: '[email protected]' };
302+
const client = { id: 'foobar' };
295303
const model = {
296304
getUser: function() { return user; },
297305
saveToken: () => should.fail()
298306
};
299307
const grantType = new PasswordGrantType({ accessTokenLifetime: 123, model });
300308
const request = new Request({ body: { username: 'foo', password: 'bar' }, headers: {}, method: {}, query: {} });
301309

302-
grantType.getUser(request).should.be.an.instanceOf(Promise);
310+
grantType.getUser(request, client).should.be.an.instanceOf(Promise);
303311
});
304312
});
305313

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ describe('PasswordGrantType', function() {
2020
getUser: sinon.stub().returns(true),
2121
saveToken: function() {}
2222
};
23+
const client = { id: 'foobar' };
2324
const handler = new PasswordGrantType({ accessTokenLifetime: 120, model: model });
2425
const request = new Request({ body: { username: 'foo', password: 'bar' }, headers: {}, method: {}, query: {} });
2526

26-
return handler.getUser(request)
27+
return handler.getUser(request, client)
2728
.then(function() {
2829
model.getUser.callCount.should.equal(1);
29-
model.getUser.firstCall.args.should.have.length(2);
30+
model.getUser.firstCall.args.should.have.length(3);
3031
model.getUser.firstCall.args[0].should.equal('foo');
3132
model.getUser.firstCall.args[1].should.equal('bar');
3233
model.getUser.firstCall.thisValue.should.equal(model);

0 commit comments

Comments
 (0)