Skip to content

Commit 3d4fa77

Browse files
authored
Merge pull request #462 from basimhennawi/feature/extend-model-request
Extend model object with request context
2 parents 9d721a3 + cb7a559 commit 3d4fa77

File tree

9 files changed

+120
-3
lines changed

9 files changed

+120
-3
lines changed

lib/handlers/authenticate-handler.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ AuthenticateHandler.prototype.handle = function(request, response) {
6363
throw new InvalidArgumentError('Invalid argument: `response` must be an instance of Response');
6464
}
6565

66+
// Extend model object with request
67+
this.model.request = request;
68+
6669
return Promise.bind(this)
6770
.then(function() {
6871
return this.getTokenFromRequest(request);

lib/handlers/authorize-handler.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ AuthorizeHandler.prototype.handle = function(request, response) {
8282
return Promise.reject(new AccessDeniedError('Access denied: user denied access to application'));
8383
}
8484

85+
// Extend model object with request
86+
this.model.request = request;
87+
8588
var fns = [
8689
this.getAuthorizationCodeLifetime(),
8790
this.getClient(request),

lib/handlers/token-handler.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ TokenHandler.prototype.handle = function(request, response) {
8585
return Promise.reject(new InvalidRequestError('Invalid request: content must be application/x-www-form-urlencoded'));
8686
}
8787

88+
// Extend model object with request
89+
this.model.request = request;
90+
8891
return Promise.bind(this)
8992
.then(function() {
9093
return this.getClient(request, response);

test/integration/handlers/authenticate-handler_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ describe('AuthenticateHandler integration', function() {
168168
});
169169
});
170170

171-
it('should return an access token', function() {
171+
it('should return an access token with extend model obj with request', function() {
172172
var accessToken = {
173173
user: {},
174174
accessTokenExpiresAt: new Date(new Date().getTime() + 10000)
@@ -192,6 +192,7 @@ describe('AuthenticateHandler integration', function() {
192192

193193
return handler.handle(request, response)
194194
.then(function(data) {
195+
model.request.should.equal(request);
195196
data.should.equal(accessToken);
196197
})
197198
.catch(should.fail);

test/integration/handlers/authorize-handler_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ describe('AuthorizeHandler integration', function() {
532532
});
533533
});
534534

535-
it('should return the `code` if successful', function() {
535+
it('should return the `code` if successful with extend model obj with request', function() {
536536
var client = { grants: ['authorization_code'], redirectUris: ['http://example.com/cb'] };
537537
var model = {
538538
getAccessToken: function() {
@@ -567,6 +567,7 @@ describe('AuthorizeHandler integration', function() {
567567

568568
return handler.handle(request, response)
569569
.then(function(data) {
570+
model.request.should.equal(request);
570571
data.should.eql({
571572
authorizationCode: 12345,
572573
client: client

test/integration/handlers/token-handler_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ describe('TokenHandler integration', function() {
297297
});
298298
});
299299

300-
it('should return a bearer token if successful', function() {
300+
it('should return a bearer token if successful with extend model obj with request', function() {
301301
var token = { accessToken: 'foo', client: {}, refreshToken: 'bar', scope: 'foobar', user: {} };
302302
var model = {
303303
getClient: function() { return { grants: ['password'] }; },
@@ -323,6 +323,7 @@ describe('TokenHandler integration', function() {
323323

324324
return handler.handle(request, response)
325325
.then(function(data) {
326+
model.request.should.equal(request);
326327
data.should.eql(token);
327328
})
328329
.catch(should.fail);

test/unit/handlers/authenticate-handler_test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
var AuthenticateHandler = require('../../../lib/handlers/authenticate-handler');
88
var Request = require('../../../lib/request');
9+
var Response = require('../../../lib/response');
910
var sinon = require('sinon');
1011
var should = require('should');
1112
var ServerError = require('../../../lib/errors/server-error');
@@ -15,6 +16,39 @@ var ServerError = require('../../../lib/errors/server-error');
1516
*/
1617

1718
describe('AuthenticateHandler', function() {
19+
describe('handle()', function() {
20+
it('should extend model object with request context', function() {
21+
var model = {
22+
getAccessToken: sinon.stub().returns({
23+
user: 'foo',
24+
accessTokenExpiresAt: new Date(new Date().getTime() + 10000)
25+
}),
26+
verifyScope: sinon.stub().returns(true)
27+
};
28+
29+
var handler = new AuthenticateHandler({
30+
addAcceptedScopesHeader: true,
31+
addAuthorizedScopesHeader: true,
32+
model: model,
33+
scope: 'bar'
34+
});
35+
36+
var request = new Request({
37+
body: {},
38+
headers: { 'Authorization': 'Bearer foo' },
39+
method: {},
40+
query: {}
41+
});
42+
var response = new Response({});
43+
44+
return handler.handle(request, response)
45+
.then(function() {
46+
model.request.should.equal(request);
47+
})
48+
.catch(should.fail);
49+
});
50+
});
51+
1852
describe('getTokenFromRequest()', function() {
1953
describe('with bearer token in the request authorization header', function() {
2054
it('should call `getTokenFromRequestHeader()`', function() {

test/unit/handlers/authorize-handler_test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,40 @@ var should = require('should');
1616
*/
1717

1818
describe('AuthorizeHandler', function() {
19+
describe('handle()', function() {
20+
it('should extend model object with request context', function() {
21+
var model = {
22+
getClient: sinon.stub().returns({
23+
grants: ['authorization_code'],
24+
redirectUris: ['/abc']
25+
}),
26+
saveAuthorizationCode: sinon.stub().returns({ authorizationCode: 'code_abc' })
27+
};
28+
var handler = new AuthorizeHandler({
29+
authenticateHandler: {
30+
handle: sinon.stub().returns({ name: 'xyz' })
31+
},
32+
authorizationCodeLifetime: 123,
33+
allowEmptyState: true,
34+
model: model
35+
});
36+
37+
var request = new Request({
38+
body: { client_id: '123', response_type: 'code' },
39+
headers: {},
40+
method: {},
41+
query: {}
42+
});
43+
var response = new Response({});
44+
45+
return handler.handle(request, response)
46+
.then(function() {
47+
model.request.should.equal(request);
48+
})
49+
.catch(should.fail);
50+
});
51+
});
52+
1953
describe('generateAuthorizationCode()', function() {
2054
it('should call `model.generateAuthorizationCode()`', function() {
2155
var model = {

test/unit/handlers/token-handler_test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
var Request = require('../../../lib/request');
8+
var Response = require('../../../lib/response');
89
var TokenHandler = require('../../../lib/handlers/token-handler');
910
var sinon = require('sinon');
1011
var should = require('should');
@@ -14,6 +15,42 @@ var should = require('should');
1415
*/
1516

1617
describe('TokenHandler', function() {
18+
describe('handle()', function() {
19+
it('should extend model object with request context', function() {
20+
var model = {
21+
getClient: sinon.stub().returns({ grants: ['client_credentials'] }),
22+
getUserFromClient: sinon.stub().returns({}),
23+
saveToken: sinon.stub().returns({
24+
accessToken: '123',
25+
client: {},
26+
user: {},
27+
accessTokenExpiresAt: new Date(new Date().getTime() + 10000),
28+
refreshTokenExpiresAt: new Date(new Date().getTime() + 10000)
29+
}),
30+
};
31+
32+
var handler = new TokenHandler({
33+
accessTokenLifetime: 123,
34+
refreshTokenLifetime: 123,
35+
model: model,
36+
});
37+
38+
var request = new Request({
39+
method: 'POST',
40+
body: { 'grant_type': 'client_credentials', 'client_id': 'abc', 'client_secret': 'xyz' },
41+
headers: { 'content-type': 'application/x-www-form-urlencoded', 'transfer-encoding': 'chunked' },
42+
query: {}
43+
});
44+
var response = new Response({});
45+
46+
return handler.handle(request, response)
47+
.then(function() {
48+
model.request.should.equal(request);
49+
})
50+
.catch(should.fail);
51+
});
52+
});
53+
1754
describe('getClient()', function() {
1855
it('should call `model.getClient()`', function() {
1956
var model = {

0 commit comments

Comments
 (0)