@@ -377,32 +377,37 @@ Service_.prototype.handleCallback = function(callbackRequest) {
377377 */
378378Service_ . 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/**
0 commit comments