Skip to content

Commit b196a7b

Browse files
authored
feat(state): Base64 encoding instead of uri encoding of state param for yahoo (#658)
* base 64 encoding of state param for yahoo * Add a property base64_state to change the encoding * add comment in yahoo module * add more tests * Address review comments
1 parent db93ed7 commit b196a7b

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/hello.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,12 @@ hello.utils.extend(hello, {
375375
}
376376

377377
// Convert state to a string
378-
p.qs.state = encodeURIComponent(JSON.stringify(p.qs.state));
378+
if (provider.oauth.base64_state) {
379+
p.qs.state = window.btoa(JSON.stringify(p.qs.state));
380+
}
381+
else {
382+
p.qs.state = encodeURIComponent(JSON.stringify(p.qs.state));
383+
}
379384

380385
// URL
381386
if (parseInt(provider.oauth.version, 10) === 1) {

src/modules/yahoo.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
version: '1.0a',
1010
auth: 'https://api.login.yahoo.com/oauth/v2/request_auth',
1111
request: 'https://api.login.yahoo.com/oauth/v2/get_request_token',
12-
token: 'https://api.login.yahoo.com/oauth/v2/get_token'
12+
token: 'https://api.login.yahoo.com/oauth/v2/get_token',
13+
// Yahoo requires the state param to be base 64 encoded, hence the flag base64_state is set to true for Yahoo.
14+
// Else uri encoding is used for all the other providers.
15+
base64_state: true
1316
},
1417

1518
// Login handler

tests/specs/unit/core/hello.login.js

+46
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,52 @@ describe('hello.login', function() {
154154
hello.login('testable', {redirect_uri: REDIRECT_URI});
155155
});
156156

157+
it('should base 64 encode the state if oauth.base64_state is true', function(done) {
158+
159+
hello.services.testable.oauth.base64_state = true;
160+
161+
var spy = sinon.spy(function(url, name, optins) {
162+
// The url should not contain uri encoded characters
163+
expect(url).to.not.contain('state=%7B%22');
164+
165+
done();
166+
});
167+
168+
utils.popup = spy;
169+
170+
hello.login('testable');
171+
});
172+
173+
it('should uri encode the state if oauth.base64_state is false', function(done) {
174+
175+
hello.services.testable.oauth.base64_state = false;
176+
177+
var spy = sinon.spy(function(url, name, optins) {
178+
// The url should contain uri encoded characters
179+
expect(url).to.contain('state=%7B%22');
180+
181+
done();
182+
});
183+
184+
utils.popup = spy;
185+
186+
hello.login('testable');
187+
});
188+
189+
it('should uri encode the state by default', function(done) {
190+
191+
var spy = sinon.spy(function(url, name, optins) {
192+
// The url should contain uri encoded characters
193+
expect(url).to.contain('state=%7B%22');
194+
195+
done();
196+
});
197+
198+
utils.popup = spy;
199+
200+
hello.login('testable');
201+
});
202+
157203
it('should pass through unknown scopes defined in `options.scope`', function(done) {
158204

159205
var spy = sinon.spy(function(url, name, optins) {

0 commit comments

Comments
 (0)