Skip to content

Commit

Permalink
fix(handlers): skip varcheck for state when allowEmptyState
Browse files Browse the repository at this point in the history
  • Loading branch information
jankapunkt committed Nov 29, 2021
1 parent 5f2b0bb commit 4ca8032
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/handlers/authorize-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,14 @@ AuthorizeHandler.prototype.getScope = function(request) {

AuthorizeHandler.prototype.getState = function(request) {
const state = request.body.state || request.query.state;

if (!this.allowEmptyState && !state) {
throw new InvalidRequestError('Missing parameter: `state`');
}

if (!is.vschar(state)) {
throw new InvalidRequestError('Invalid parameter: `state`');
const stateExists = state && state.length > 0;
const stateIsValid = stateExists
? is.vschar(state)
: this.allowEmptyState;

if (!stateIsValid) {
const message = (!stateExists) ? 'Missing' : 'Invalid';
throw new InvalidRequestError(`${message} parameter: \`state\``);
}

return state;
Expand Down
12 changes: 12 additions & 0 deletions test/integration/handlers/authorize-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,18 @@ describe('AuthorizeHandler integration', function() {
}
});

it('should allow missing `state` if `allowEmptyState` is valid', function () {
const model = {
getAccessToken: function() {},
getClient: function() {},
saveAuthorizationCode: function() {}
};
const handler = new AuthorizeHandler({ allowEmptyState: true, authorizationCodeLifetime: 120, model: model });
const request = new Request({ body: {}, headers: {}, method: {}, query: {} });
const state = handler.getState(request);
should.equal(state, undefined);
});

it('should throw an error if `state` is invalid', function() {
const model = {
getAccessToken: function() {},
Expand Down

0 comments on commit 4ca8032

Please sign in to comment.