Skip to content

Commit 41d8586

Browse files
author
Eric Koleda
authored
Remove all properties from storage when the service is reset (#230)
1 parent 8c605ba commit 41d8586

File tree

5 files changed

+78
-8
lines changed

5 files changed

+78
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"del": "^1.2.1",
2020
"eslint": "^4.19.1",
2121
"eslint-config-google": "^0.9.1",
22-
"fibers": "^2.0.2",
22+
"fibers": "^4.0.2",
2323
"gas-local": "^1.3.1",
2424
"gulp": "^3.9.1",
2525
"gulp-clean": "^0.3.2",

src/Service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ Service_.prototype.getIdToken = function() {
476476
* re-authorized.
477477
*/
478478
Service_.prototype.reset = function() {
479-
this.getStorage().removeValue(null);
479+
this.getStorage().reset();
480480
};
481481

482482
/**

src/Storage.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Storage_.prototype.getValue = function(key, optSkipMemoryCheck) {
6161

6262
if (!optSkipMemoryCheck) {
6363
// Check in-memory cache.
64-
if (value = this.memory_[key]) {
64+
if (value = this.memory_[prefixedKey]) {
6565
if (value === Storage_.CACHE_NULL_VALUE) {
6666
return null;
6767
}
@@ -72,7 +72,7 @@ Storage_.prototype.getValue = function(key, optSkipMemoryCheck) {
7272
// Check cache.
7373
if (this.cache_ && (jsonValue = this.cache_.get(prefixedKey))) {
7474
value = JSON.parse(jsonValue);
75-
this.memory_[key] = value;
75+
this.memory_[prefixedKey] = value;
7676
if (value === Storage_.CACHE_NULL_VALUE) {
7777
return null;
7878
}
@@ -87,13 +87,13 @@ Storage_.prototype.getValue = function(key, optSkipMemoryCheck) {
8787
jsonValue, Storage_.CACHE_EXPIRATION_TIME_SECONDS);
8888
}
8989
value = JSON.parse(jsonValue);
90-
this.memory_[key] = value;
90+
this.memory_[prefixedKey] = value;
9191
return value;
9292
}
9393

9494
// Not found. Store a special null value in the memory and cache to reduce
9595
// hits on the PropertiesService.
96-
this.memory_[key] = Storage_.CACHE_NULL_VALUE;
96+
this.memory_[prefixedKey] = Storage_.CACHE_NULL_VALUE;
9797
if (this.cache_) {
9898
this.cache_.put(prefixedKey, JSON.stringify(Storage_.CACHE_NULL_VALUE),
9999
Storage_.CACHE_EXPIRATION_TIME_SECONDS);
@@ -116,7 +116,7 @@ Storage_.prototype.setValue = function(key, value) {
116116
this.cache_.put(prefixedKey, jsonValue,
117117
Storage_.CACHE_EXPIRATION_TIME_SECONDS);
118118
}
119-
this.memory_[key] = value;
119+
this.memory_[prefixedKey] = value;
120120
};
121121

122122
/**
@@ -125,13 +125,39 @@ Storage_.prototype.setValue = function(key, value) {
125125
*/
126126
Storage_.prototype.removeValue = function(key) {
127127
var prefixedKey = this.getPrefixedKey_(key);
128+
this.removeValueWithPrefixedKey_(prefixedKey);
129+
};
130+
131+
/**
132+
* Resets the storage, removing all stored data.
133+
* @param {string} key The key.
134+
*/
135+
Storage_.prototype.reset = function() {
136+
var prefix = this.getPrefixedKey_();
137+
var prefixedKeys = Object.keys(this.memory_);
138+
if (this.properties_) {
139+
var props = this.properties_.getProperties();
140+
prefixedKeys = Object.keys(props).filter(function(prefixedKey) {
141+
return prefixedKey === prefix || prefixedKey.indexOf(prefix + '.') === 0;
142+
});
143+
}
144+
for (var i = 0; i < prefixedKeys.length; i++) {
145+
this.removeValueWithPrefixedKey_(prefixedKeys[i]);
146+
};
147+
};
148+
149+
/**
150+
* Removes a stored value.
151+
* @param {string} key The key.
152+
*/
153+
Storage_.prototype.removeValueWithPrefixedKey_ = function(prefixedKey) {
128154
if (this.properties_) {
129155
this.properties_.deleteProperty(prefixedKey);
130156
}
131157
if (this.cache_) {
132158
this.cache_.remove(prefixedKey);
133159
}
134-
delete this.memory_[key];
160+
delete this.memory_[prefixedKey];
135161
};
136162

137163
/**

test/mocks/properties.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ MockProperties.prototype.deleteProperty = function(key) {
1616
delete this.store[key];
1717
};
1818

19+
MockProperties.prototype.getProperties = function() {
20+
return Object.assign({}, this.store);
21+
};
22+
1923
module.exports = MockProperties;

test/test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,46 @@ describe('Service', function() {
196196
assert.notExists(cache.get(key));
197197
assert.notExists(properties.getProperty(key));
198198
});
199+
200+
it('should delete values in storage', function() {
201+
var cache = new MockCache();
202+
var properties = new MockProperties();
203+
var service = OAuth2.createService('test')
204+
.setPropertyStore(properties)
205+
.setCache(cache);
206+
var storage = service.getStorage();
207+
storage.setValue('foo', 'bar');
208+
209+
service.reset();
210+
211+
assert.notExists(storage.getValue('foo'));
212+
});
213+
214+
it('should not delete values from other services', function() {
215+
var cache = new MockCache();
216+
var properties = new MockProperties();
217+
var service = OAuth2.createService('test')
218+
.setPropertyStore(properties)
219+
.setCache(cache);
220+
var values = {
221+
'oauth2.something': 'token',
222+
'oauth2.something.foo': 'bar',
223+
'oauth2.something.test': 'baz',
224+
'oauth2.testing': 'token',
225+
'oauth2.testing.foo': 'bar',
226+
};
227+
for (let [key, value] of Object.entries(values)) {
228+
properties.setProperty(key, value);
229+
cache.put(key, value);
230+
}
231+
232+
service.reset();
233+
234+
for (let [key, value] of Object.entries(values)) {
235+
assert.equal(cache.get(key), value);
236+
assert.equal(properties.getProperty(key), value);
237+
}
238+
});
199239
});
200240

201241
describe('#hasAccess()', function() {

0 commit comments

Comments
 (0)