Skip to content

Commit

Permalink
Optimized token renewal
Browse files Browse the repository at this point in the history
  • Loading branch information
virtualzone committed Aug 9, 2021
1 parent 03114bd commit 7d19c5d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ func main() {
logError("Could not read secret store: " + err.Error())
return
}
logVerbose("Renewing access token...")
if _, err := client.RenewAccessToken(); err != nil {
logError("Could not renew access token: " + err.Error())
return
if client.ShouldRenewAccessToken() {
logVerbose("Renewing access token...")
if _, err := client.RenewAccessToken(); err != nil {
logError("Could not renew access token: " + err.Error())
return
}
}
}
cmdDef.Fn(client, args)
Expand Down
6 changes: 6 additions & 0 deletions sdk/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ func (client *Client) redeemCodeForAccessToken(code string) (*LoginRedeemCodeRes
return &json, nil
}

func (client *Client) ShouldRenewAccessToken() bool {
now := time.Now()
diff := now.Sub(client.SecretStore.Expiry)
return diff.Minutes() > -30
}

func (client *Client) RenewAccessToken() (*LoginRedeemCodeResponse, error) {
params := make(HTTPRequestParams)
params["client_id"] = client.Config.ClientID
Expand Down

0 comments on commit 7d19c5d

Please sign in to comment.