Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion tokencache/cache_token_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ package tokencache
import (
"context"
"fmt"
"time"

"github.com/pardot/oidc"
)

const (
tokenExpirationGracePeriod = time.Duration(30 * time.Second)
)

type cachingTokenSource struct {
src oidc.TokenSource
cache CredentialCache
Expand Down Expand Up @@ -87,7 +92,7 @@ func (c *cachingTokenSource) Token(ctx context.Context) (*oidc.Token, error) {
}

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

return newToken, nil
}

func tokenWithinGracePeriod(token *oidc.Token) bool {
gracePeriodStart := token.Claims.Expiry.Time().Add(-tokenExpirationGracePeriod)
return gracePeriodStart.Before(time.Now()) && token.Valid()
}
Loading