Skip to content

Commit 7d19c5d

Browse files
committed
Optimized token renewal
1 parent 03114bd commit 7d19c5d

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

main.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ func main() {
8686
logError("Could not read secret store: " + err.Error())
8787
return
8888
}
89-
logVerbose("Renewing access token...")
90-
if _, err := client.RenewAccessToken(); err != nil {
91-
logError("Could not renew access token: " + err.Error())
92-
return
89+
if client.ShouldRenewAccessToken() {
90+
logVerbose("Renewing access token...")
91+
if _, err := client.RenewAccessToken(); err != nil {
92+
logError("Could not renew access token: " + err.Error())
93+
return
94+
}
9395
}
9496
}
9597
cmdDef.Fn(client, args)

sdk/login.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ func (client *Client) redeemCodeForAccessToken(code string) (*LoginRedeemCodeRes
9393
return &json, nil
9494
}
9595

96+
func (client *Client) ShouldRenewAccessToken() bool {
97+
now := time.Now()
98+
diff := now.Sub(client.SecretStore.Expiry)
99+
return diff.Minutes() > -30
100+
}
101+
96102
func (client *Client) RenewAccessToken() (*LoginRedeemCodeResponse, error) {
97103
params := make(HTTPRequestParams)
98104
params["client_id"] = client.Config.ClientID

0 commit comments

Comments
 (0)