Skip to content

Commit

Permalink
Add token within expiry grace period check
Browse files Browse the repository at this point in the history
  • Loading branch information
mjcmtb committed Sep 16, 2024
1 parent c982e42 commit 58150f4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (

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

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

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

// Type of the token
func (t *Token) Type() string {
// only thing we support for now
Expand Down
2 changes: 1 addition & 1 deletion tokencache/cache_token_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,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() && !token.WithinGracePeriod() {
return token, nil
} else if token != nil && token.RefreshToken != "" {
// we have an expired token, try and refresh if we can.
Expand Down

0 comments on commit 58150f4

Please sign in to comment.