Skip to content

Commit 4ca8032

Browse files
committed
fix(handlers): skip varcheck for state when allowEmptyState
1 parent 5f2b0bb commit 4ca8032

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

lib/handlers/authorize-handler.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,14 @@ AuthorizeHandler.prototype.getScope = function(request) {
238238

239239
AuthorizeHandler.prototype.getState = function(request) {
240240
const state = request.body.state || request.query.state;
241-
242-
if (!this.allowEmptyState && !state) {
243-
throw new InvalidRequestError('Missing parameter: `state`');
244-
}
245-
246-
if (!is.vschar(state)) {
247-
throw new InvalidRequestError('Invalid parameter: `state`');
241+
const stateExists = state && state.length > 0;
242+
const stateIsValid = stateExists
243+
? is.vschar(state)
244+
: this.allowEmptyState;
245+
246+
if (!stateIsValid) {
247+
const message = (!stateExists) ? 'Missing' : 'Invalid';
248+
throw new InvalidRequestError(`${message} parameter: \`state\``);
248249
}
249250

250251
return state;

test/integration/handlers/authorize-handler_test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,18 @@ describe('AuthorizeHandler integration', function() {
932932
}
933933
});
934934

935+
it('should allow missing `state` if `allowEmptyState` is valid', function () {
936+
const model = {
937+
getAccessToken: function() {},
938+
getClient: function() {},
939+
saveAuthorizationCode: function() {}
940+
};
941+
const handler = new AuthorizeHandler({ allowEmptyState: true, authorizationCodeLifetime: 120, model: model });
942+
const request = new Request({ body: {}, headers: {}, method: {}, query: {} });
943+
const state = handler.getState(request);
944+
should.equal(state, undefined);
945+
});
946+
935947
it('should throw an error if `state` is invalid', function() {
936948
const model = {
937949
getAccessToken: function() {},

0 commit comments

Comments
 (0)