Skip to content

Commit aca48ce

Browse files
authored
fix(handlers): skip varcheck for state when allowEmptyState #89 #93
Merge pull request #93 from node-oauth/fix-vcharfail-allowemptystate
2 parents 5824f79 + 4ca8032 commit aca48ce

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
@@ -243,13 +243,14 @@ AuthorizeHandler.prototype.getScope = function(request) {
243243

244244
AuthorizeHandler.prototype.getState = function(request) {
245245
const state = request.body.state || request.query.state;
246-
247-
if (!this.allowEmptyState && !state) {
248-
throw new InvalidRequestError('Missing parameter: `state`');
249-
}
250-
251-
if (!is.vschar(state)) {
252-
throw new InvalidRequestError('Invalid parameter: `state`');
246+
const stateExists = state && state.length > 0;
247+
const stateIsValid = stateExists
248+
? is.vschar(state)
249+
: this.allowEmptyState;
250+
251+
if (!stateIsValid) {
252+
const message = (!stateExists) ? 'Missing' : 'Invalid';
253+
throw new InvalidRequestError(`${message} parameter: \`state\``);
253254
}
254255

255256
return state;

test/integration/handlers/authorize-handler_test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,18 @@ describe('AuthorizeHandler integration', function() {
953953
}
954954
});
955955

956+
it('should allow missing `state` if `allowEmptyState` is valid', function () {
957+
const model = {
958+
getAccessToken: function() {},
959+
getClient: function() {},
960+
saveAuthorizationCode: function() {}
961+
};
962+
const handler = new AuthorizeHandler({ allowEmptyState: true, authorizationCodeLifetime: 120, model: model });
963+
const request = new Request({ body: {}, headers: {}, method: {}, query: {} });
964+
const state = handler.getState(request);
965+
should.equal(state, undefined);
966+
});
967+
956968
it('should throw an error if `state` is invalid', function() {
957969
const model = {
958970
getAccessToken: function() {},

0 commit comments

Comments
 (0)