Skip to content

Commit 2125bae

Browse files
committed
Implement ignoreVersion option.
1 parent 5636296 commit 2125bae

File tree

4 files changed

+133
-4
lines changed

4 files changed

+133
-4
lines changed

lib/passport-http-oauth/strategies/consumer.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ function ConsumerStrategy(options, consumer, token, validate) {
122122
this._consumer = consumer;
123123
this._token = token;
124124
this._validate = validate;
125-
this._realm = options.realm || 'Clients';
126125
this._host = options.host || null;
126+
this._realm = options.realm || 'Clients';
127+
this._ignoreVersion = options.ignoreVersion || false;
127128
}
128129

129130
/**
@@ -191,7 +192,7 @@ ConsumerStrategy.prototype.authenticate = function(req) {
191192
, verifier = params['oauth_verifier']
192193
, version = params['oauth_version']
193194

194-
if (version && version !== '1.0') {
195+
if (version && version !== '1.0' && !this._ignoreVersion) {
195196
return this.fail(this._challenge('version_rejected'), 400);
196197
}
197198

lib/passport-http-oauth/strategies/token.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ function TokenStrategy(options, consumer, verify, validate) {
114114
this._consumer = consumer;
115115
this._verify = verify;
116116
this._validate = validate;
117-
this._realm = options.realm || 'Users';
118117
this._host = options.host || null;
118+
this._realm = options.realm || 'Users';
119+
this._ignoreVersion = options.ignoreVersion || false;
119120
}
120121

121122
/**
@@ -182,7 +183,7 @@ TokenStrategy.prototype.authenticate = function(req) {
182183
, nonce = params['oauth_nonce']
183184
, version = params['oauth_version']
184185

185-
if (version && version !== '1.0') {
186+
if (version && version !== '1.0' && !this._ignoreVersion) {
186187
return this.fail(this._challenge('version_rejected'), 400);
187188
}
188189

test/strategies/consumer-test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,67 @@ vows.describe('ConsumerStrategy').addBatch({
137137
},
138138
},
139139

140+
'strategy handling a valid request without a request token placing credentials in header using 1.0A version': {
141+
topic: function() {
142+
var strategy = new ConsumerStrategy(
143+
{ ignoreVersion: true },
144+
// consumer callback
145+
function(consumerKey, done) {
146+
if (consumerKey == 'abc123') {
147+
done(null, { id: '1' }, 'ssh-secret');
148+
} else {
149+
done(new Error('something is wrong'))
150+
}
151+
},
152+
// token callback
153+
function(requestToken, done) {
154+
done(new Error('token callback should not be called'));
155+
}
156+
);
157+
return strategy;
158+
},
159+
160+
'after augmenting with actions': {
161+
topic: function(strategy) {
162+
var self = this;
163+
var req = {};
164+
strategy.success = function(user, info) {
165+
self.callback(null, user, info);
166+
}
167+
strategy.fail = function(challenge, status) {
168+
self.callback(new Error('should not be called'));
169+
}
170+
strategy.error = function(err) {
171+
self.callback(new Error('should not be called'));
172+
}
173+
174+
req.url = '/oauth/request_token';
175+
req.method = 'POST';
176+
req.headers = {};
177+
req.headers['host'] = '127.0.0.1:3000';
178+
req.headers['authorization'] = 'OAuth oauth_callback="http%3A%2F%2Fmacbook-air.local.jaredhanson.net%3A3001%2Foauth%2Fcallback",oauth_consumer_key="abc123",oauth_nonce="fNyKdt8ZTgTVdEABtUMFzcXRxF4a230q",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1341176111",oauth_version="1.0A",oauth_signature="VfpGYYsNM4Ih0Lt7JsIbJz6%2FJM4%3D"';
179+
req.query = url.parse(req.url, true).query;
180+
req.connection = { encrypted: false };
181+
process.nextTick(function () {
182+
strategy.authenticate(req);
183+
});
184+
},
185+
186+
'should not generate an error' : function(err, user, info) {
187+
assert.isNull(err);
188+
},
189+
'should authenticate' : function(err, user, info) {
190+
assert.equal(user.id, '1');
191+
},
192+
'should set scheme to OAuth' : function(err, user, info) {
193+
assert.equal(info.scheme, 'OAuth');
194+
},
195+
'should set callbackURL' : function(err, user, info) {
196+
assert.equal(info.oauth.callbackURL, 'http://macbook-air.local.jaredhanson.net:3001/oauth/callback');
197+
},
198+
},
199+
},
200+
140201
'strategy handling a valid request without a request token using host option instead of host header': {
141202
topic: function() {
142203
var strategy = new ConsumerStrategy(

test/strategies/token-test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,72 @@ vows.describe('TokenStrategy').addBatch({
147147
},
148148
},
149149

150+
'strategy handling a valid request with credentials in header using 1.0A version': {
151+
topic: function() {
152+
var strategy = new TokenStrategy(
153+
{ ignoreVersion: true },
154+
// consumer callback
155+
function(consumerKey, done) {
156+
if (consumerKey == '1234') {
157+
done(null, { id: '1' }, 'keep-this-secret');
158+
} else {
159+
done(new Error('something is wrong'))
160+
}
161+
},
162+
// verify callback
163+
function(accessToken, done) {
164+
if (accessToken == 'abc-123-xyz-789') {
165+
done(null, { username: 'bob' }, 'lips-zipped');
166+
} else {
167+
done(new Error('something is wrong'))
168+
}
169+
}
170+
);
171+
return strategy;
172+
},
173+
174+
'after augmenting with actions': {
175+
topic: function(strategy) {
176+
var self = this;
177+
var req = {};
178+
strategy.success = function(user, info) {
179+
self.callback(null, user, info);
180+
}
181+
strategy.fail = function(challenge, status) {
182+
self.callback(new Error('should not be called'));
183+
}
184+
strategy.error = function(err) {
185+
self.callback(new Error('should not be called'));
186+
}
187+
188+
req.url = '/1/users/show.json?screen_name=jaredhanson&user_id=1705';
189+
req.method = 'GET';
190+
req.headers = {};
191+
req.headers['host'] = '127.0.0.1:3000';
192+
req.headers['authorization'] = 'OAuth oauth_consumer_key="1234",oauth_nonce="A7E738D9A9684A60A40607017735ADAD",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1339004912",oauth_token="abc-123-xyz-789",oauth_version="1.0A",oauth_signature="W%2BppR%2BZyXT5UgrLV%2FTQnmlVSjZI%3D"';
193+
req.query = url.parse(req.url, true).query;
194+
req.connection = { encrypted: false };
195+
process.nextTick(function () {
196+
strategy.authenticate(req);
197+
});
198+
},
199+
200+
'should not generate an error' : function(err, user, info) {
201+
assert.isNull(err);
202+
},
203+
'should authenticate' : function(err, user, info) {
204+
assert.equal(user.username, 'bob');
205+
},
206+
'should set scheme to OAuth' : function(err, user, info) {
207+
assert.equal(info.scheme, 'OAuth');
208+
},
209+
'should set consumer' : function(err, user, info) {
210+
assert.equal(info.consumer.id, '1');
211+
assert.strictEqual(info.client, info.consumer);
212+
},
213+
},
214+
},
215+
150216
'strategy handling a valid request using host option instead of host header': {
151217
topic: function() {
152218
var strategy = new TokenStrategy(

0 commit comments

Comments
 (0)