Skip to content

Commit 8c45328

Browse files
authored
Add token refresh grace period (#54)
* Add token within expiry grace period check * Migrate WithinGracePeriod * Deassociate * Not * Unexport * Unexport var
1 parent c982e42 commit 8c45328

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

tokencache/cache_token_source.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ package tokencache
33
import (
44
"context"
55
"fmt"
6+
"time"
67

78
"github.com/pardot/oidc"
89
)
910

11+
const (
12+
tokenExpirationGracePeriod = time.Duration(30 * time.Second)
13+
)
14+
1015
type cachingTokenSource struct {
1116
src oidc.TokenSource
1217
cache CredentialCache
@@ -87,7 +92,7 @@ func (c *cachingTokenSource) Token(ctx context.Context) (*oidc.Token, error) {
8792
}
8893

8994
var newToken *oidc.Token
90-
if token != nil && token.Valid() {
95+
if token != nil && token.Valid() && !tokenWithinGracePeriod(token) {
9196
return token, nil
9297
} else if token != nil && token.RefreshToken != "" {
9398
// we have an expired token, try and refresh if we can.
@@ -114,3 +119,8 @@ func (c *cachingTokenSource) Token(ctx context.Context) (*oidc.Token, error) {
114119

115120
return newToken, nil
116121
}
122+
123+
func tokenWithinGracePeriod(token *oidc.Token) bool {
124+
gracePeriodStart := token.Claims.Expiry.Time().Add(-tokenExpirationGracePeriod)
125+
return gracePeriodStart.Before(time.Now()) && token.Valid()
126+
}

0 commit comments

Comments
 (0)