Skip to content

Commit 3946491

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 2f4a826 commit 3946491

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

authn/token_exchange.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ func WithHTTPClient(client *http.Client) ExchangeClientOpts {
3434
}
3535
}
3636

37+
// WithMinimumCacheTTL allows setting the minimum amount of time that a cache
38+
// entry must be valid for in order for it to be reused.
39+
func WithMinimumCacheTTL(ttl time.Duration) ExchangeClientOpts {
40+
return func(c *TokenExchangeClient) {
41+
c.minimumTTL = ttl
42+
}
43+
}
44+
3745
func NewTokenExchangeClient(cfg TokenExchangeConfig, opts ...ExchangeClientOpts) (*TokenExchangeClient, error) {
3846
if cfg.Token == "" {
3947
return nil, fmt.Errorf("%w: missing required token", ErrMissingConfig)
@@ -47,8 +55,9 @@ func NewTokenExchangeClient(cfg TokenExchangeConfig, opts ...ExchangeClientOpts)
4755
cache: cache.NewLocalCache(cache.Config{
4856
CleanupInterval: 5 * time.Minute,
4957
}),
50-
cfg: cfg,
51-
singlef: singleflight.Group{},
58+
minimumTTL: 15 * time.Second,
59+
cfg: cfg,
60+
singlef: singleflight.Group{},
5261
}
5362

5463
for _, opt := range opts {
@@ -64,10 +73,11 @@ func NewTokenExchangeClient(cfg TokenExchangeConfig, opts ...ExchangeClientOpts)
6473
}
6574

6675
type TokenExchangeClient struct {
67-
cache cache.Cache
68-
cfg TokenExchangeConfig
69-
client *http.Client
70-
singlef singleflight.Group
76+
cache cache.Cache
77+
minimumTTL time.Duration // Minimum time that token must be valid to be reused.
78+
cfg TokenExchangeConfig
79+
client *http.Client
80+
singlef singleflight.Group
7181
}
7282

7383
type TokenExchangeRequest struct {
@@ -190,8 +200,6 @@ func (c *TokenExchangeClient) getCache(ctx context.Context, key string) (string,
190200
}
191201

192202
func (c *TokenExchangeClient) setCache(ctx context.Context, token string, key string) error {
193-
const cacheLeeway = 15 * time.Second
194-
195203
parsed, err := jwt.ParseSigned(token)
196204
if err != nil {
197205
return fmt.Errorf("failed to parse token: %v", err)
@@ -202,7 +210,7 @@ func (c *TokenExchangeClient) setCache(ctx context.Context, token string, key st
202210
return fmt.Errorf("failed to extract claims from the token: %v", err)
203211
}
204212

205-
return c.cache.Set(ctx, key, []byte(token), time.Until(claims.Expiry.Time())-cacheLeeway)
213+
return c.cache.Set(ctx, key, []byte(token), time.Until(claims.Expiry.Time())-c.minimumTTL)
206214
}
207215

208216
var _ TokenExchanger = StaticTokenExchanger{}

authn/token_exchange_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ func Test_TokenExchangeClient_Exchange(t *testing.T) {
185185
})
186186
}
187187

188+
func Test_WithMinimumCacheTTL(t *testing.T) {
189+
cfg := TokenExchangeConfig{
190+
Token: "some-token",
191+
TokenExchangeURL: "http://localhost",
192+
}
193+
194+
customTTL := 42 * time.Second
195+
client, err := NewTokenExchangeClient(cfg, WithMinimumCacheTTL(customTTL))
196+
require.NoError(t, err)
197+
require.NotNil(t, client)
198+
assert.Equal(t, customTTL, client.minimumTTL)
199+
}
200+
188201
func signAccessToken(t *testing.T, expiresIn time.Duration) string {
189202
signer, err := jose.NewSigner(jose.SigningKey{
190203
Algorithm: jose.HS256,

0 commit comments

Comments
 (0)