Skip to content

Commit cd674ec

Browse files
author
Eric Koleda
committed
Simplify hasAccess logic further
1 parent af50ac0 commit cd674ec

File tree

3 files changed

+60
-34
lines changed

3 files changed

+60
-34
lines changed

src/Service.js

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -377,32 +377,37 @@ Service_.prototype.handleCallback = function(callbackRequest) {
377377
*/
378378
Service_.prototype.hasAccess = function() {
379379
var token = this.getToken();
380-
if (!token || this.isExpired_(token)) {
381-
return this.lockable_(function() {
382-
// Get the token again, bypassing the local memory cache.
383-
token = this.getToken(true);
384-
// Check to see if the token is still missing or expired, as another
385-
// execution may have refreshed it while we were waiting for the lock.
386-
if (!token || this.isExpired_(token)) {
387-
try {
388-
if (token && this.canRefresh_(token)) {
389-
this.refresh();
390-
} else if (this.privateKey_) {
391-
this.exchangeJwt_();
392-
} else if (this.grantType_) {
393-
this.exchangeGrant_();
394-
} else {
395-
return false;
396-
}
397-
} catch (e) {
398-
this.lastError_ = e;
399-
return false;
400-
}
380+
if (token && !this.isExpired_(token)) return true; // Token still has access.
381+
var canGetToken = (token && this.canRefresh_(token)) ||
382+
this.privateKey_ || this.grantType_;
383+
if (!canGetToken) return false;
384+
385+
return this.lockable_(function() {
386+
// Get the token again, bypassing the local memory cache.
387+
token = this.getToken(true);
388+
// Check to see if the token is no longer missing or expired, as another
389+
// execution may have refreshed it while we were waiting for the lock.
390+
if (token && !this.isExpired_(token)) return true; // Token now has access.
391+
try {
392+
if (token && this.canRefresh_(token)) {
393+
this.refresh();
394+
return true;
395+
} else if (this.privateKey_) {
396+
this.exchangeJwt_();
397+
return true;
398+
} else if (this.grantType_) {
399+
this.exchangeGrant_();
400+
return true;
401+
} else {
402+
// This should never happen, since canGetToken should have been false
403+
// earlier.
404+
return false;
401405
}
402-
return true;
403-
});
404-
}
405-
return true;
406+
} catch (e) {
407+
this.lastError_ = e;
408+
return false;
409+
}
410+
});
406411
};
407412

408413
/**
@@ -586,12 +591,13 @@ Service_.prototype.saveToken_ = function(token) {
586591

587592
/**
588593
* Gets the token from the service's property store or cache.
589-
* @param {boolean} optSkipMemory Whether to bypass the local memory cache when
590-
* fetching the token (the default is false).
594+
* @param {boolean?} optSkipMemoryCheck If true, bypass the local memory cache
595+
* when fetching the token.
591596
* @return {Object} The token, or null if no token was found.
592597
*/
593-
Service_.prototype.getToken = function(optSkipMemory) {
594-
return this.getStorage().getValue(null, optSkipMemory);
598+
Service_.prototype.getToken = function(optSkipMemoryCheck) {
599+
// Gets the stored value under the null key, which is reserved for the token.
600+
return this.getStorage().getValue(null, optSkipMemoryCheck);
595601
};
596602

597603
/**

src/Storage.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ Storage_.CACHE_EXPIRATION_TIME_SECONDS = 21600; // 6 hours.
4343
/**
4444
* Gets a stored value.
4545
* @param {string} key The key.
46-
* @param {boolean} optSkipMemory Whether to bypass the local memory cache when
47-
* fetching the value (the default is false).
46+
* @param {boolean?} optSkipMemoryCheck Whether to bypass the local memory cache
47+
* when fetching the value (the default is false).
4848
* @return {*} The stored value.
4949
*/
50-
Storage_.prototype.getValue = function(key, optSkipMemory) {
51-
if (!optSkipMemory) {
52-
// Check memory.
50+
Storage_.prototype.getValue = function(key, optSkipMemoryCheck) {
51+
if (!optSkipMemoryCheck) {
52+
// Check in-memory cache.
5353
if (this.memory_[key]) {
5454
return this.memory_[key];
5555
}

test/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,26 @@ describe('Service', function() {
214214
service.hasAccess();
215215
assert.equal(lock.counter, 0);
216216
});
217+
218+
it('should not acquire a lock when there is no refresh token', function() {
219+
var token = {
220+
granted_time: 100,
221+
expires_in: 100,
222+
access_token: 'foo',
223+
};
224+
var lock = new MockLock();
225+
var properties = new MockProperties({
226+
'oauth2.test': JSON.stringify(token)
227+
});
228+
var service = OAuth2.createService('test')
229+
.setClientId('abc')
230+
.setClientSecret('def')
231+
.setTokenUrl('http://www.example.com')
232+
.setPropertyStore(properties)
233+
.setLock(lock);
234+
service.hasAccess();
235+
assert.equal(lock.counter, 0);
236+
});
217237
});
218238

219239
describe('#refresh()', function() {

0 commit comments

Comments
 (0)