Skip to content

Commit

Permalink
feat: Allow usage without client_secret for services that only rely o…
Browse files Browse the repository at this point in the history
…n PKCE
  • Loading branch information
sqrrrl committed Dec 1, 2022
1 parent bcb4360 commit e6afdfb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,6 @@ Service_.prototype.handleCallback = function(callbackRequest) {
}
validate_({
'Client ID': this.clientId_,
'Client Secret': this.clientSecret_,
'Token URL': this.tokenUrl_
});
var payload = {
Expand Down
52 changes: 41 additions & 11 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var MockCache = require('./mocks/cache');
var MockLock = require('./mocks/lock');
var MockScriptApp = require('./mocks/script');
var MockUtilities = require('./mocks/utilities');

var mocks = {
ScriptApp: new MockScriptApp(),
UrlFetchApp: new MockUrlFetchApp(),
Expand Down Expand Up @@ -514,6 +515,27 @@ describe('Service', () => {
assert.include(authorizationUrl, 'code_challenge');
assert.include(authorizationUrl, 'code_challenge_method=S256');
});


it('should use supply verifier when exchanging code', () => {
var service = OAuth2.createService('test')
.setAuthorizationBaseUrl('http://www.example.com')
.setTokenUrl('http://www.example.com/token')
.setClientId('abc')
.setCallbackFunction('authCallback')
.generateCodeVerifier();
var authorizationUrl = service.getAuthorizationUrl({});
var state = extractStateTokenFromUrl(authorizationUrl);
mocks.UrlFetchApp.resultFunction = (url, opts) => {
assert.isNotNull(opts.payload.code_verifier, 'Code verifier not present');
return `{ "access_token": "123" }`;
};
service.handleCallback({
parameter: Object.assign({
code: 'test',
}, state.arguments)
});
});
});

describe('#getAuthorizationUrl()', () => {
Expand All @@ -527,17 +549,7 @@ describe('Service', () => {
foo: 'bar'
});

// Extract the state token from the URL and parse it. For example, the
// URL http://www.example.com?state=%7B%22a%22%3A1%7D would produce
// {a: 1}.
var querystring = authorizationUrl.split('?')[1];
var params = querystring.split('&').reduce((result, pair) => {
var parts = pair.split('=').map(decodeURIComponent);
result[parts[0]] = parts[1];
return result;
}, {});
var state = JSON.parse(params.state);

var state = extractStateTokenFromUrl(authorizationUrl);
assert.equal(state.arguments.foo, 'bar');
});
});
Expand Down Expand Up @@ -858,3 +870,21 @@ describe('Utilities', () => {
});
});
});


/*
*Extract the state token from the URL and parse it. For example, the
* URL http://www.example.com?state=%7B%22a%22%3A1%7D would produce
* {a: 1}.
*/
function extractStateTokenFromUrl(authorizationUrl) {
var querystring = authorizationUrl.split('?')[1];
var params = querystring.split('&').reduce((result, pair) => {
var parts = pair.split('=').map(decodeURIComponent);
result[parts[0]] = parts[1];
return result;
}, {});
var state = JSON.parse(params.state);
return state;
}

0 comments on commit e6afdfb

Please sign in to comment.