Skip to content

Commit bd43960

Browse files
committed
Test cases for verifying state.
1 parent f0d70f9 commit bd43960

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

lib/strategy.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,16 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
199199
}
200200

201201
var state = req.query.state;
202-
this._stateStore.verify(req, state, loaded);
202+
try {
203+
var arity = this._stateStore.verify.length;
204+
if (arity == 4) {
205+
this._stateStore.verify(req, state, meta, loaded);
206+
} else { // arity == 3
207+
this._stateStore.verify(req, state, loaded);
208+
}
209+
} catch (ex) {
210+
return this.error(ex);
211+
}
203212
} else {
204213
var params = this.authorizationParams(options);
205214
params.response_type = 'code';

test/oauth2.state.custom.test.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,16 @@ describe('OAuth2Strategy', function() {
2525
return cb(null, 'foos7473');
2626
};
2727

28-
CustomStore.prototype.verify = function(req, state, cb) {
28+
CustomStore.prototype.verify = function(req, state, meta, cb) {
29+
if (req.url === '/error') { return cb(new Error('something went wrong verifying state')); }
30+
if (req.url === '/exception') { throw new Error('something went horribly wrong verifying state'); }
31+
32+
if (req.url !== '/auth/example/callback') { return cb(new Error('incorrect req argument')); }
33+
if (state !== 'foos7473') { return cb(new Error('incorrect state argument')); }
34+
if (meta.authorizationURL !== 'https://www.example.com/oauth2/authorize') { return cb(new Error('incorrect meta.authorizationURL argument')); }
35+
if (meta.tokenURL !== 'https://www.example.com/oauth2/token') { return cb(new Error('incorrect meta.tokenURL argument')); }
36+
if (meta.clientID !== 'ABC123') { return callback(new Error('incorrect meta.clientID argument')); }
37+
2938
req.customStoreVerifyCalled = req.customStoreVerifyCalled ? req.customStoreVerifyCalled++ : 1;
3039
return cb(null, true);
3140
};
@@ -157,6 +166,7 @@ describe('OAuth2Strategy', function() {
157166
.req(function(req) {
158167
request = req;
159168

169+
req.url = '/auth/example/callback';
160170
req.query = {};
161171
req.query.code = 'SplxlOBeZQQYbYS6WxSbIA';
162172
req.query.state = 'foos7473';
@@ -179,6 +189,60 @@ describe('OAuth2Strategy', function() {
179189
});
180190
}); // that was approved
181191

192+
describe('that errors due to custom store supplying error', function() {
193+
var request
194+
, err;
195+
196+
before(function(done) {
197+
chai.passport.use(strategy)
198+
.error(function(e) {
199+
err = e;
200+
done();
201+
})
202+
.req(function(req) {
203+
request = req;
204+
205+
req.url = '/error';
206+
req.query = {};
207+
req.query.code = 'SplxlOBeZQQYbYS6WxSbIA';
208+
req.query.state = 'foos7473';
209+
})
210+
.authenticate();
211+
});
212+
213+
it('should error', function() {
214+
expect(err).to.be.an.instanceof(Error);
215+
expect(err.message).to.equal('something went wrong verifying state');
216+
});
217+
}); // that errors due to custom store supplying error
218+
219+
describe('that errors due to custom store throwing error', function() {
220+
var request
221+
, err;
222+
223+
before(function(done) {
224+
chai.passport.use(strategy)
225+
.error(function(e) {
226+
err = e;
227+
done();
228+
})
229+
.req(function(req) {
230+
request = req;
231+
232+
req.url = '/exception';
233+
req.query = {};
234+
req.query.code = 'SplxlOBeZQQYbYS6WxSbIA';
235+
req.query.state = 'foos7473';
236+
})
237+
.authenticate();
238+
});
239+
240+
it('should error', function() {
241+
expect(err).to.be.an.instanceof(Error);
242+
expect(err.message).to.equal('something went horribly wrong verifying state');
243+
});
244+
}); // that errors due to custom store throwing error
245+
182246
}); // processing response to authorization request
183247

184248
}); // with custom state store that accepts meta argument

0 commit comments

Comments
 (0)