Skip to content

Commit 53a4aa9

Browse files
author
Eric Koleda
authored
Add ability to list service names. (#260)
* Add ability to list service names. * Update README.
1 parent 036e929 commit 53a4aa9

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,9 @@ function getGitHubService(label) {
494494
}
495495
```
496496
497+
You can list all of the service names you've previously stored tokens for using
498+
`OAuth2.getServiceNames(propertyStore)`.
499+
497500
## Compatibility
498501
499502
This library was designed to work with any OAuth2 provider, but because of small

src/OAuth2.js

+26
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ var TOKEN_FORMAT = {
2828
FORM_URL_ENCODED: 'application/x-www-form-urlencoded'
2929
};
3030

31+
var STORAGE_PREFIX_ = 'oauth2.';
32+
3133
/**
3234
* Creates a new OAuth2 service with the name specified. It's usually best to
3335
* create and configure your service once at the start of your script, and then
@@ -53,10 +55,34 @@ function getRedirectUri(optScriptId) {
5355
'/usercallback';
5456
}
5557

58+
/**
59+
* Gets the list of services with tokens stored in the given property store.
60+
* This is useful if you connect to the same API with multiple accounts and
61+
* need to keep track of them. If no stored tokens are found this will return
62+
* an empty array.
63+
* @param {PropertiesService.Properties} propertyStore The properties to check.
64+
* @return {Array.<string>} The service names.
65+
*/
66+
function getServiceNames(propertyStore) {
67+
var props = propertyStore.getProperties();
68+
return Object.keys(props).filter(function(key) {
69+
var parts = key.split('.');
70+
return key.indexOf(STORAGE_PREFIX_) == 0 && parts.length > 1 && parts[1];
71+
}).map(function(key) {
72+
return key.split('.')[1];
73+
}).reduce(function(result, key) {
74+
if (result.indexOf(key) < 0) {
75+
result.push(key);
76+
}
77+
return result;
78+
}, []);
79+
}
80+
5681
if (typeof module === 'object') {
5782
module.exports = {
5883
createService: createService,
5984
getRedirectUri: getRedirectUri,
85+
getServiceNames: getServiceNames,
6086
TOKEN_FORMAT: TOKEN_FORMAT
6187
};
6288
}

src/Service.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ Service_.prototype.refresh = function() {
609609
*/
610610
Service_.prototype.getStorage = function() {
611611
if (!this.storage_) {
612-
var prefix = 'oauth2.' + this.serviceName_;
612+
var prefix = STORAGE_PREFIX_ + this.serviceName_;
613613
this.storage_ = new Storage_(prefix, this.propertyStore_, this.cache_);
614614
}
615615
return this.storage_;

test/test.js

+47
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,53 @@ var mocks = {
3333
};
3434
var OAuth2 = gas.require('./src', mocks);
3535

36+
describe('OAuth2', function() {
37+
describe('#getServiceNames()', function() {
38+
it('should return the service names for stored tokens', function() {
39+
var props = new MockProperties({
40+
'oauth2.foo': '{"access_token": "abc"}',
41+
'oauth2.bar': '{"access_token": "abc"}',
42+
});
43+
44+
var names = OAuth2.getServiceNames(props);
45+
46+
assert.deepEqual(names, ['foo', 'bar']);
47+
});
48+
49+
it('should return an empty array when no tokens are stored', function() {
50+
var props = new MockProperties();
51+
52+
var names = OAuth2.getServiceNames(props);
53+
54+
assert.deepEqual(names, []);
55+
});
56+
57+
it('should ignore keys without a service name', function() {
58+
var props = new MockProperties({
59+
'oauth2.': 'foo',
60+
'oauth2..bar': 'bar',
61+
});
62+
63+
var names = OAuth2.getServiceNames(props);
64+
65+
assert.deepEqual(names, []);
66+
});
67+
68+
it('should not have duplicate names when there are custom keys',
69+
function() {
70+
var props = new MockProperties({
71+
'oauth2.foo': '{"access_token": "abc"}',
72+
'oauth2.foo.extra': 'my extra stuff',
73+
'oauth2.foo.extra2': 'more extra stuff',
74+
});
75+
76+
var names = OAuth2.getServiceNames(props);
77+
78+
assert.deepEqual(names, ['foo']);
79+
});
80+
});
81+
});
82+
3683
describe('Service', function() {
3784
describe('#getToken()', function() {
3885
it('should return null when no token is stored', function() {

0 commit comments

Comments
 (0)