Skip to content

Commit

Permalink
Migrate WithinGracePeriod
Browse files Browse the repository at this point in the history
  • Loading branch information
mjcmtb committed Sep 17, 2024
1 parent 0fcd584 commit 112f5ea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
8 changes: 1 addition & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (

const (
// ScopeOfflineAccess requests a refresh token
ScopeOfflineAccess = "offline_access"
TokenExpirationGracePeriod = time.Duration(30 * time.Second)
ScopeOfflineAccess = "offline_access"
)

type KeySource interface {
Expand Down Expand Up @@ -160,11 +159,6 @@ func (t *Token) Valid() bool {
t.IDToken != ""
}

func (t *Token) WithinGracePeriod() bool {
gracePeriodStart := t.Claims.Expiry.Time().Add(-TokenExpirationGracePeriod)
return gracePeriodStart.Before(time.Now()) && t.Valid()
}

// Type of the token
func (t *Token) Type() string {
// only thing we support for now
Expand Down
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() && !token.WithinGracePeriod() {
if token != nil && token.Valid() && !c.WithinGracePeriod(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 (c *cachingTokenSource) WithinGracePeriod(token *oidc.Token) bool {
gracePeriodStart := token.Claims.Expiry.Time().Add(-TokenExpirationGracePeriod)
return gracePeriodStart.Before(time.Now()) && token.Valid()
}

0 comments on commit 112f5ea

Please sign in to comment.