Skip to content

Extend model object with request context #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/handlers/authenticate-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ AuthenticateHandler.prototype.handle = function(request, response) {
throw new InvalidArgumentError('Invalid argument: `response` must be an instance of Response');
}

// Extend model object with request
this.model.request = request;

return Promise.bind(this)
.then(function() {
return this.getTokenFromRequest(request);
Expand Down
3 changes: 3 additions & 0 deletions lib/handlers/authorize-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ AuthorizeHandler.prototype.handle = function(request, response) {
return Promise.reject(new AccessDeniedError('Access denied: user denied access to application'));
}

// Extend model object with request
this.model.request = request;

var fns = [
this.getAuthorizationCodeLifetime(),
this.getClient(request),
Expand Down
3 changes: 3 additions & 0 deletions lib/handlers/token-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ TokenHandler.prototype.handle = function(request, response) {
return Promise.reject(new InvalidRequestError('Invalid request: content must be application/x-www-form-urlencoded'));
}

// Extend model object with request
this.model.request = request;

return Promise.bind(this)
.then(function() {
return this.getClient(request, response);
Expand Down
3 changes: 2 additions & 1 deletion test/integration/handlers/authenticate-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ describe('AuthenticateHandler integration', function() {
});
});

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

return handler.handle(request, response)
.then(function(data) {
model.request.should.equal(request);
data.should.equal(accessToken);
})
.catch(should.fail);
Expand Down
3 changes: 2 additions & 1 deletion test/integration/handlers/authorize-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ describe('AuthorizeHandler integration', function() {
});
});

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

return handler.handle(request, response)
.then(function(data) {
model.request.should.equal(request);
data.should.eql({
authorizationCode: 12345,
client: client
Expand Down
3 changes: 2 additions & 1 deletion test/integration/handlers/token-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ describe('TokenHandler integration', function() {
});
});

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

return handler.handle(request, response)
.then(function(data) {
model.request.should.equal(request);
data.should.eql(token);
})
.catch(should.fail);
Expand Down
34 changes: 34 additions & 0 deletions test/unit/handlers/authenticate-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

var AuthenticateHandler = require('../../../lib/handlers/authenticate-handler');
var Request = require('../../../lib/request');
var Response = require('../../../lib/response');
var sinon = require('sinon');
var should = require('should');
var ServerError = require('../../../lib/errors/server-error');
Expand All @@ -15,6 +16,39 @@ var ServerError = require('../../../lib/errors/server-error');
*/

describe('AuthenticateHandler', function() {
describe('handle()', function() {
it('should extend model object with request context', function() {
var model = {
getAccessToken: sinon.stub().returns({
user: 'foo',
accessTokenExpiresAt: new Date(new Date().getTime() + 10000)
}),
verifyScope: sinon.stub().returns(true)
};

var handler = new AuthenticateHandler({
addAcceptedScopesHeader: true,
addAuthorizedScopesHeader: true,
model: model,
scope: 'bar'
});

var request = new Request({
body: {},
headers: { 'Authorization': 'Bearer foo' },
method: {},
query: {}
});
var response = new Response({});

return handler.handle(request, response)
.then(function() {
model.request.should.equal(request);
})
.catch(should.fail);
});
});

describe('getTokenFromRequest()', function() {
describe('with bearer token in the request authorization header', function() {
it('should call `getTokenFromRequestHeader()`', function() {
Expand Down
34 changes: 34 additions & 0 deletions test/unit/handlers/authorize-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,40 @@ var should = require('should');
*/

describe('AuthorizeHandler', function() {
describe('handle()', function() {
it('should extend model object with request context', function() {
var model = {
getClient: sinon.stub().returns({
grants: ['authorization_code'],
redirectUris: ['/abc']
}),
saveAuthorizationCode: sinon.stub().returns({ authorizationCode: 'code_abc' })
};
var handler = new AuthorizeHandler({
authenticateHandler: {
handle: sinon.stub().returns({ name: 'xyz' })
},
authorizationCodeLifetime: 123,
allowEmptyState: true,
model: model
});

var request = new Request({
body: { client_id: '123', response_type: 'code' },
headers: {},
method: {},
query: {}
});
var response = new Response({});

return handler.handle(request, response)
.then(function() {
model.request.should.equal(request);
})
.catch(should.fail);
});
});

describe('generateAuthorizationCode()', function() {
it('should call `model.generateAuthorizationCode()`', function() {
var model = {
Expand Down
37 changes: 37 additions & 0 deletions test/unit/handlers/token-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

var Request = require('../../../lib/request');
var Response = require('../../../lib/response');
var TokenHandler = require('../../../lib/handlers/token-handler');
var sinon = require('sinon');
var should = require('should');
Expand All @@ -14,6 +15,42 @@ var should = require('should');
*/

describe('TokenHandler', function() {
describe('handle()', function() {
it('should extend model object with request context', function() {
var model = {
getClient: sinon.stub().returns({ grants: ['client_credentials'] }),
getUserFromClient: sinon.stub().returns({}),
saveToken: sinon.stub().returns({
accessToken: '123',
client: {},
user: {},
accessTokenExpiresAt: new Date(new Date().getTime() + 10000),
refreshTokenExpiresAt: new Date(new Date().getTime() + 10000)
}),
};

var handler = new TokenHandler({
accessTokenLifetime: 123,
refreshTokenLifetime: 123,
model: model,
});

var request = new Request({
method: 'POST',
body: { 'grant_type': 'client_credentials', 'client_id': 'abc', 'client_secret': 'xyz' },
headers: { 'content-type': 'application/x-www-form-urlencoded', 'transfer-encoding': 'chunked' },
query: {}
});
var response = new Response({});

return handler.handle(request, response)
.then(function() {
model.request.should.equal(request);
})
.catch(should.fail);
});
});

describe('getClient()', function() {
it('should call `model.getClient()`', function() {
var model = {
Expand Down