Skip to content

Commit

Permalink
fix(cli): minor fix related to refreshing tokens (#1013)
Browse files Browse the repository at this point in the history
ALLY-1278
  • Loading branch information
hazedav authored Nov 9, 2022
1 parent 605a857 commit 9f4c619
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cli/cmd/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,21 @@ func (c *cliState) ReadCachedToken() {
}
}

// return true if the cached token is expired or will expire within
// the eminent duration (i.e. 10 seconds)
func (c *cliState) cachedTokenExpiryEminent() bool {
eminentDuration := -10 * time.Second
// only consider the tokenCache expiry time if we actually have a cached token
return c.tokenCache.Token != "" && c.tokenCache.ExpiresAt.Before(time.Now().Add(eminentDuration))
}

func (c *cliState) WriteCachedToken() error {
if c.noCache {
return nil
}

if c.Token == "" || c.tokenCache.ExpiresAt.Before(time.Now().Add(-10*time.Second)) {
// if we don't have a token or the cached token expiry is eminent
// then attempt to refresh it...
if c.Token == "" || c.cachedTokenExpiryEminent() {
response, err := c.LwApi.GenerateToken()
if err != nil {
return err
Expand Down

0 comments on commit 9f4c619

Please sign in to comment.