Skip to content

Commit fdc60bc

Browse files
committed
feat: Add WithMinimumCacheTTL option
The WithMinimumCacheTTL option is used to control when cache entries are removed from the cache. The existing code sets this value to 15 seconds, and if this option is not passed, that is the fallback value. Signed-off-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
1 parent 5455052 commit fdc60bc

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

authn/token_exchange.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ func WithTokenExchangeClientCache(cache cache.Cache) ExchangeClientOpts {
4040
}
4141
}
4242

43+
// WithMinimumCacheTTL allows setting the minimum amount of time that a cache
44+
// entry must be valid for in order for it to be reused.
45+
func WithMinimumCacheTTL(ttl time.Duration) ExchangeClientOpts {
46+
return func(c *TokenExchangeClient) {
47+
c.minimumTTL = ttl
48+
}
49+
}
50+
4351
func NewTokenExchangeClient(cfg TokenExchangeConfig, opts ...ExchangeClientOpts) (*TokenExchangeClient, error) {
4452
if cfg.Token == "" {
4553
return nil, fmt.Errorf("%w: missing required token", ErrMissingConfig)
@@ -50,9 +58,10 @@ func NewTokenExchangeClient(cfg TokenExchangeConfig, opts ...ExchangeClientOpts)
5058
}
5159

5260
c := &TokenExchangeClient{
53-
cache: nil, // See below.
54-
cfg: cfg,
55-
singlef: singleflight.Group{},
61+
cache: nil, // See below.
62+
minimumTTL: 15 * time.Second,
63+
cfg: cfg,
64+
singlef: singleflight.Group{},
5665
}
5766

5867
for _, opt := range opts {
@@ -77,14 +86,14 @@ func NewTokenExchangeClient(cfg TokenExchangeConfig, opts ...ExchangeClientOpts)
7786
}
7887

7988
return c, nil
80-
8189
}
8290

8391
type TokenExchangeClient struct {
84-
cache cache.Cache
85-
cfg TokenExchangeConfig
86-
client *http.Client
87-
singlef singleflight.Group
92+
cache cache.Cache
93+
minimumTTL time.Duration // Minimum time that token must be valid to be reused.
94+
cfg TokenExchangeConfig
95+
client *http.Client
96+
singlef singleflight.Group
8897
}
8998

9099
type TokenExchangeRequest struct {
@@ -207,8 +216,6 @@ func (c *TokenExchangeClient) getCache(ctx context.Context, key string) (string,
207216
}
208217

209218
func (c *TokenExchangeClient) setCache(ctx context.Context, token string, key string) error {
210-
const cacheLeeway = 15 * time.Second
211-
212219
parsed, err := jwt.ParseSigned(token)
213220
if err != nil {
214221
return fmt.Errorf("failed to parse token: %v", err)
@@ -219,7 +226,7 @@ func (c *TokenExchangeClient) setCache(ctx context.Context, token string, key st
219226
return fmt.Errorf("failed to extract claims from the token: %v", err)
220227
}
221228

222-
return c.cache.Set(ctx, key, []byte(token), time.Until(claims.Expiry.Time())-cacheLeeway)
229+
return c.cache.Set(ctx, key, []byte(token), time.Until(claims.Expiry.Time())-c.minimumTTL)
223230
}
224231

225232
var _ TokenExchanger = StaticTokenExchanger{}

authn/token_exchange_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,19 @@ func Test_TokenExchangeClient_Exchange(t *testing.T) {
217217
})
218218
}
219219

220+
func Test_WithMinimumCacheTTL(t *testing.T) {
221+
cfg := TokenExchangeConfig{
222+
Token: "some-token",
223+
TokenExchangeURL: "http://localhost",
224+
}
225+
226+
customTTL := 42 * time.Second
227+
client, err := NewTokenExchangeClient(cfg, WithMinimumCacheTTL(customTTL))
228+
require.NoError(t, err)
229+
require.NotNil(t, client)
230+
assert.Equal(t, customTTL, client.minimumTTL)
231+
}
232+
220233
func signAccessToken(t *testing.T, expiresIn time.Duration) string {
221234
signer, err := jose.NewSigner(jose.SigningKey{
222235
Algorithm: jose.HS256,

0 commit comments

Comments
 (0)