Skip to content

Commit f0d70f9

Browse files
committed
Prepare for multi-arity form of state verify.
1 parent ac6e390 commit f0d70f9

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

lib/strategy.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,12 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
149149
}
150150

151151
if (req.query && req.query.code) {
152-
var state = req.query.state;
153-
this._stateStore.verify(req, state, function(err, ok, info) {
152+
function loaded(err, ok, info) {
154153
if (err) { return self.error(err); }
155154
if (!ok) {
156155
return self.fail(info, 403);
157156
}
158-
157+
159158
var code = req.query.code;
160159

161160
var params = self.tokenParams(options);
@@ -197,7 +196,10 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
197196
});
198197
}
199198
);
200-
});
199+
}
200+
201+
var state = req.query.state;
202+
this._stateStore.verify(req, state, loaded);
201203
} else {
202204
var params = this.authorizationParams(options);
203205
params.response_type = 'code';

test/oauth2.state.custom.test.js

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

28-
CustomStore.prototype.verify = function(req, state, meta, cb) {
28+
CustomStore.prototype.verify = function(req, state, cb) {
29+
req.customStoreVerifyCalled = req.customStoreVerifyCalled ? req.customStoreVerifyCalled++ : 1;
30+
return cb(null, true);
2931
};
3032

3133

@@ -61,7 +63,7 @@ describe('OAuth2Strategy', function() {
6163
expect(url).to.equal('https://www.example.com/oauth2/authorize?response_type=code&redirect_uri=https%3A%2F%2Fwww.example.net%2Fauth%2Fexample%2Fcallback&state=foos7473&client_id=ABC123');
6264
});
6365

64-
it('should store request token in custom store', function() {
66+
it('should serialize state using custom store', function() {
6567
expect(request.customStoreStoreCalled).to.equal(1);
6668
});
6769
}); // that redirects to service provider
@@ -112,6 +114,73 @@ describe('OAuth2Strategy', function() {
112114

113115
}); // issuing authorization request
114116

117+
118+
describe('processing response to authorization request', function() {
119+
var strategy = new OAuth2Strategy({
120+
authorizationURL: 'https://www.example.com/oauth2/authorize',
121+
tokenURL: 'https://www.example.com/oauth2/token',
122+
clientID: 'ABC123',
123+
clientSecret: 'secret',
124+
callbackURL: 'https://www.example.net/auth/example/callback',
125+
store: new CustomStore()
126+
},
127+
function(accessToken, refreshToken, profile, done) {
128+
if (accessToken !== '2YotnFZFEjr1zCsicMWpAA') { return done(new Error('incorrect accessToken argument')); }
129+
if (refreshToken !== 'tGzv3JOkF0XG5Qx2TlKWIA') { return done(new Error('incorrect refreshToken argument')); }
130+
if (typeof profile !== 'object') { return done(new Error('incorrect profile argument')); }
131+
if (Object.keys(profile).length !== 0) { return done(new Error('incorrect profile argument')); }
132+
133+
return done(null, { id: '1234' }, { message: 'Hello' });
134+
});
135+
136+
strategy._oauth2.getOAuthAccessToken = function(code, options, callback) {
137+
if (code !== 'SplxlOBeZQQYbYS6WxSbIA') { return callback(new Error('incorrect code argument')); }
138+
if (options.grant_type !== 'authorization_code') { return callback(new Error('incorrect options.grant_type argument')); }
139+
if (options.redirect_uri !== 'https://www.example.net/auth/example/callback') { return callback(new Error('incorrect options.redirect_uri argument')); }
140+
141+
return callback(null, '2YotnFZFEjr1zCsicMWpAA', 'tGzv3JOkF0XG5Qx2TlKWIA', { token_type: 'example' });
142+
}
143+
144+
145+
describe('that was approved', function() {
146+
var request
147+
, user
148+
, info;
149+
150+
before(function(done) {
151+
chai.passport.use(strategy)
152+
.success(function(u, i) {
153+
user = u;
154+
info = i;
155+
done();
156+
})
157+
.req(function(req) {
158+
request = req;
159+
160+
req.query = {};
161+
req.query.code = 'SplxlOBeZQQYbYS6WxSbIA';
162+
req.query.state = 'foos7473';
163+
})
164+
.authenticate();
165+
});
166+
167+
it('should supply user', function() {
168+
expect(user).to.be.an.object;
169+
expect(user.id).to.equal('1234');
170+
});
171+
172+
it('should supply info', function() {
173+
expect(info).to.be.an.object;
174+
expect(info.message).to.equal('Hello');
175+
});
176+
177+
it('should verify state using custom store', function() {
178+
expect(request.customStoreVerifyCalled).to.equal(1);
179+
});
180+
}); // that was approved
181+
182+
}); // processing response to authorization request
183+
115184
}); // with custom state store that accepts meta argument
116185

117186
});

0 commit comments

Comments
 (0)