Skip to content

Add ability to list service names. #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,9 @@ function getGitHubService(label) {
}
```

You can list all of the service names you've previously stored tokens for using
`OAuth2.getServiceNames(propertyStore)`.

## Compatibility

This library was designed to work with any OAuth2 provider, but because of small
Expand Down
26 changes: 26 additions & 0 deletions src/OAuth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var TOKEN_FORMAT = {
FORM_URL_ENCODED: 'application/x-www-form-urlencoded'
};

var STORAGE_PREFIX_ = 'oauth2.';

/**
* Creates a new OAuth2 service with the name specified. It's usually best to
* create and configure your service once at the start of your script, and then
Expand All @@ -53,10 +55,34 @@ function getRedirectUri(optScriptId) {
'/usercallback';
}

/**
* Gets the list of services with tokens stored in the given property store.
* This is useful if you connect to the same API with multiple accounts and
* need to keep track of them. If no stored tokens are found this will return
* an empty array.
* @param {PropertiesService.Properties} propertyStore The properties to check.
* @return {Array.<string>} The service names.
*/
function getServiceNames(propertyStore) {
var props = propertyStore.getProperties();
return Object.keys(props).filter(function(key) {
var parts = key.split('.');
return key.indexOf(STORAGE_PREFIX_) == 0 && parts.length > 1 && parts[1];
}).map(function(key) {
return key.split('.')[1];
}).reduce(function(result, key) {
if (result.indexOf(key) < 0) {
result.push(key);
}
return result;
}, []);
}

if (typeof module === 'object') {
module.exports = {
createService: createService,
getRedirectUri: getRedirectUri,
getServiceNames: getServiceNames,
TOKEN_FORMAT: TOKEN_FORMAT
};
}
2 changes: 1 addition & 1 deletion src/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ Service_.prototype.refresh = function() {
*/
Service_.prototype.getStorage = function() {
if (!this.storage_) {
var prefix = 'oauth2.' + this.serviceName_;
var prefix = STORAGE_PREFIX_ + this.serviceName_;
this.storage_ = new Storage_(prefix, this.propertyStore_, this.cache_);
}
return this.storage_;
Expand Down
47 changes: 47 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,53 @@ var mocks = {
};
var OAuth2 = gas.require('./src', mocks);

describe('OAuth2', function() {
describe('#getServiceNames()', function() {
it('should return the service names for stored tokens', function() {
var props = new MockProperties({
'oauth2.foo': '{"access_token": "abc"}',
'oauth2.bar': '{"access_token": "abc"}',
});

var names = OAuth2.getServiceNames(props);

assert.deepEqual(names, ['foo', 'bar']);
});

it('should return an empty array when no tokens are stored', function() {
var props = new MockProperties();

var names = OAuth2.getServiceNames(props);

assert.deepEqual(names, []);
});

it('should ignore keys without a service name', function() {
var props = new MockProperties({
'oauth2.': 'foo',
'oauth2..bar': 'bar',
});

var names = OAuth2.getServiceNames(props);

assert.deepEqual(names, []);
});

it('should not have duplicate names when there are custom keys',
function() {
var props = new MockProperties({
'oauth2.foo': '{"access_token": "abc"}',
'oauth2.foo.extra': 'my extra stuff',
'oauth2.foo.extra2': 'more extra stuff',
});

var names = OAuth2.getServiceNames(props);

assert.deepEqual(names, ['foo']);
});
});
});

describe('Service', function() {
describe('#getToken()', function() {
it('should return null when no token is stored', function() {
Expand Down