Skip to content

Commit

Permalink
feat(state): Base64 encoding instead of uri encoding of state param f…
Browse files Browse the repository at this point in the history
…or 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
  • Loading branch information
arushi364 authored Jan 24, 2023
1 parent db93ed7 commit b196a7b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/hello.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,12 @@ hello.utils.extend(hello, {
}

// Convert state to a string
p.qs.state = encodeURIComponent(JSON.stringify(p.qs.state));
if (provider.oauth.base64_state) {
p.qs.state = window.btoa(JSON.stringify(p.qs.state));
}
else {
p.qs.state = encodeURIComponent(JSON.stringify(p.qs.state));
}

// URL
if (parseInt(provider.oauth.version, 10) === 1) {
Expand Down
5 changes: 4 additions & 1 deletion src/modules/yahoo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
version: '1.0a',
auth: 'https://api.login.yahoo.com/oauth/v2/request_auth',
request: 'https://api.login.yahoo.com/oauth/v2/get_request_token',
token: 'https://api.login.yahoo.com/oauth/v2/get_token'
token: 'https://api.login.yahoo.com/oauth/v2/get_token',
// Yahoo requires the state param to be base 64 encoded, hence the flag base64_state is set to true for Yahoo.
// Else uri encoding is used for all the other providers.
base64_state: true
},

// Login handler
Expand Down
46 changes: 46 additions & 0 deletions tests/specs/unit/core/hello.login.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,52 @@ describe('hello.login', function() {
hello.login('testable', {redirect_uri: REDIRECT_URI});
});

it('should base 64 encode the state if oauth.base64_state is true', function(done) {

hello.services.testable.oauth.base64_state = true;

var spy = sinon.spy(function(url, name, optins) {
// The url should not contain uri encoded characters
expect(url).to.not.contain('state=%7B%22');

done();
});

utils.popup = spy;

hello.login('testable');
});

it('should uri encode the state if oauth.base64_state is false', function(done) {

hello.services.testable.oauth.base64_state = false;

var spy = sinon.spy(function(url, name, optins) {
// The url should contain uri encoded characters
expect(url).to.contain('state=%7B%22');

done();
});

utils.popup = spy;

hello.login('testable');
});

it('should uri encode the state by default', function(done) {

var spy = sinon.spy(function(url, name, optins) {
// The url should contain uri encoded characters
expect(url).to.contain('state=%7B%22');

done();
});

utils.popup = spy;

hello.login('testable');
});

it('should pass through unknown scopes defined in `options.scope`', function(done) {

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

0 comments on commit b196a7b

Please sign in to comment.