@@ -38,28 +38,28 @@ type MutatorIDToken struct {
38
38
templates * template.Template
39
39
templatesLock sync.Mutex
40
40
41
- tokenCache * ristretto.Cache
42
- tokenCacheEnabled bool
41
+ tokenCache * ristretto.Cache
43
42
}
44
43
45
44
type CredentialsIDTokenConfig struct {
46
- Claims string `json:"claims"`
47
- IssuerURL string `json:"issuer_url"`
48
- JWKSURL string `json:"jwks_url"`
49
- TTL string `json:"ttl"`
45
+ Claims string `json:"claims"`
46
+ IssuerURL string `json:"issuer_url"`
47
+ JWKSURL string `json:"jwks_url"`
48
+ TTL string `json:"ttl"`
49
+ Cache IdTokenCacheConfig `json:"cache"`
50
+ }
51
+
52
+ type IdTokenCacheConfig struct {
53
+ Enabled bool `json:"enabled"`
54
+ MaxCost int `json:"max_cost"`
50
55
}
51
56
52
57
func (c * CredentialsIDTokenConfig ) ClaimsTemplateID () string {
53
58
return fmt .Sprintf ("%x" , md5 .Sum ([]byte (c .Claims )))
54
59
}
55
60
56
61
func NewMutatorIDToken (c configuration.Provider , r MutatorIDTokenRegistry ) * MutatorIDToken {
57
- cache , _ := ristretto .NewCache (& ristretto.Config {
58
- NumCounters : 10000 ,
59
- MaxCost : 1 << 25 ,
60
- BufferItems : 64 ,
61
- })
62
- return & MutatorIDToken {r : r , c : c , templates : x .NewTemplate ("id_token" ), tokenCache : cache , tokenCacheEnabled : true }
62
+ return & MutatorIDToken {r : r , c : c , templates : x .NewTemplate ("id_token" )}
63
63
}
64
64
65
65
func (a * MutatorIDToken ) GetID () string {
@@ -70,10 +70,6 @@ func (a *MutatorIDToken) WithCache(t *template.Template) {
70
70
a .templates = t
71
71
}
72
72
73
- func (a * MutatorIDToken ) SetCaching (token bool ) {
74
- a .tokenCacheEnabled = token
75
- }
76
-
77
73
type idTokenCacheContainer struct {
78
74
ExpiresAt time.Time
79
75
TTL time.Duration
@@ -87,7 +83,7 @@ func (a *MutatorIDToken) cacheKey(config *CredentialsIDTokenConfig, ttl time.Dur
87
83
}
88
84
89
85
func (a * MutatorIDToken ) tokenFromCache (config * CredentialsIDTokenConfig , session * authn.AuthenticationSession , claims []byte , ttl time.Duration ) (string , bool ) {
90
- if ! a . tokenCacheEnabled {
86
+ if ! config . Cache . Enabled {
91
87
return "" , false
92
88
}
93
89
@@ -108,7 +104,7 @@ func (a *MutatorIDToken) tokenFromCache(config *CredentialsIDTokenConfig, sessio
108
104
}
109
105
110
106
func (a * MutatorIDToken ) tokenToCache (config * CredentialsIDTokenConfig , session * authn.AuthenticationSession , claims []byte , ttl time.Duration , expiresAt time.Time , token string ) {
111
- if ! a . tokenCacheEnabled {
107
+ if ! config . Cache . Enabled {
112
108
return
113
109
}
114
110
@@ -195,7 +191,11 @@ func (a *MutatorIDToken) Validate(config json.RawMessage) error {
195
191
}
196
192
197
193
func (a * MutatorIDToken ) Config (config json.RawMessage ) (* CredentialsIDTokenConfig , error ) {
198
- var c CredentialsIDTokenConfig
194
+ c := CredentialsIDTokenConfig {
195
+ Cache : IdTokenCacheConfig {
196
+ Enabled : true , // default to true
197
+ },
198
+ }
199
199
if err := a .c .MutatorConfig (a .GetID (), config , & c ); err != nil {
200
200
return nil , NewErrMutatorMisconfigured (a , err )
201
201
}
@@ -204,5 +204,29 @@ func (a *MutatorIDToken) Config(config json.RawMessage) (*CredentialsIDTokenConf
204
204
c .TTL = "15m"
205
205
}
206
206
207
+ if a .tokenCache == nil {
208
+ cost := int64 (c .Cache .MaxCost )
209
+ if cost == 0 {
210
+ cost = 1 << 25
211
+ }
212
+
213
+ cache , err := ristretto .NewCache (& ristretto.Config {
214
+ NumCounters : cost * 10 ,
215
+ // Allocate a max
216
+ MaxCost : cost ,
217
+ // This is a best-practice value.
218
+ BufferItems : 64 ,
219
+ Cost : func (value interface {}) int64 {
220
+ container := value .(* idTokenCacheContainer )
221
+ return int64 (len (container .Token ))
222
+ },
223
+ })
224
+
225
+ if err != nil {
226
+ return nil , err
227
+ }
228
+ a .tokenCache = cache
229
+ }
230
+
207
231
return & c , nil
208
232
}
0 commit comments