@@ -18,20 +18,19 @@ type TokenManagerOptions struct {
18
18
// ExpirationRefreshRatio is the ratio of the token expiration time to refresh the token.
19
19
// It is used to determine when to refresh the token.
20
20
// The value should be between 0 and 1.
21
- // For example, if the expiration time is 1 hour and the ratio is 0.5 ,
22
- // the token will be refreshed after 30 minutes.
21
+ // For example, if the expiration time is 1 hour and the ratio is 0.75 ,
22
+ // the token will be refreshed after 45 minutes. (the token is refreshed when 75% of its lifetime has passed)
23
23
//
24
24
// default: 0.7
25
25
ExpirationRefreshRatio float64
26
26
27
27
// LowerRefreshBoundMs is the lower bound for the refresh time in milliseconds.
28
- // It is used to determine when to refresh the token.
29
- // The value should be greater than 0.
30
- // For example, if the expiration time is 1 hour and the lower bound is 30 minutes,
31
- // the token will be refreshed after 30 minutes.
28
+ // Represents the minimum time in milliseconds before token expiration to trigger a refresh, in milliseconds.
29
+ // This value sets a fixed lower bound for when a token refresh should occur, regardless
30
+ // of the token's total lifetime.
32
31
//
33
32
// default: 0 ms (no lower bound, refresh based on ExpirationRefreshRatio)
34
- LowerRefreshBoundMs int
33
+ LowerRefreshBoundMs int64
35
34
36
35
// IdentityProviderResponseParser is a function that parses the IdentityProviderResponse.
37
36
// The function takes the response and based on its type returns the populated Token object.
@@ -168,17 +167,24 @@ func NewTokenManager(idp IdentityProvider, options TokenManagerOptions) (TokenMa
168
167
token : nil ,
169
168
closed : make (chan struct {}),
170
169
expirationRefreshRatio : options .ExpirationRefreshRatio ,
170
+ lowerRefreshBoundMs : options .LowerRefreshBoundMs ,
171
+ lowerBoundDuration : time .Duration (options .LowerRefreshBoundMs ) * time .Millisecond ,
171
172
identityProviderResponseParser : options .IdentityProviderResponseParser ,
172
173
retryOptions : options .RetryOptions ,
173
174
}, nil
174
175
}
175
176
176
177
// entraidTokenManager is a struct that implements the TokenManager interface.
177
178
type entraidTokenManager struct {
178
- idp IdentityProvider
179
+ // idp is the identity provider used to obtain the token.
180
+ idp IdentityProvider
181
+
182
+ // token is the authentication token for the user which should be kept in memory if valid.
179
183
token * Token
180
184
181
- // TokenParser is a function that parses the token.
185
+ // identityProviderResponseParser is a function that parses the IdentityProviderResponse.
186
+ // it can be supplied by the user to parse the token and return the populated Token object or
187
+ // the default implementation will be used.
182
188
identityProviderResponseParser IdentityProviderResponseParserFunc
183
189
184
190
// retryOptions is a struct that contains the options for retrying the token request.
@@ -199,8 +205,22 @@ type entraidTokenManager struct {
199
205
200
206
// expirationRefreshRatio is the ratio of the token expiration time to refresh the token.
201
207
// It is used to determine when to refresh the token.
208
+ // The value should be between 0 and 1.
209
+ // For example, if the expiration time is 1 hour and the ratio is 0.75,
210
+ // the token will be refreshed after 45 minutes. (the token is refreshed when 75% of its lifetime has passed)
202
211
expirationRefreshRatio float64
203
212
213
+ // lowerRefreshBoundMs is the lower bound for the refresh time in milliseconds.
214
+ // Represents the minimum time in milliseconds before token expiration to trigger a refresh, in milliseconds.
215
+ // This value sets a fixed lower bound for when a token refresh should occur, regardless
216
+ // of the token's total lifetime.
217
+ lowerRefreshBoundMs int64
218
+
219
+ // lowerBoundDuration is the lower bound for the refresh time in time.Duration.
220
+ lowerBoundDuration time.Duration
221
+
222
+ // closed is a channel that is closed when the token manager is closed.
223
+ // It is used to signal the token manager to stop requesting tokens.
204
224
closed chan struct {}
205
225
}
206
226
@@ -238,6 +258,19 @@ type TokenListener interface {
238
258
OnTokenError (err error )
239
259
}
240
260
261
+ func (e * entraidTokenManager ) durationToRenewal () time.Duration {
262
+ if e .token == nil {
263
+ return 0
264
+ }
265
+ // Calculate the time to renew the token based on the expiration refresh ratio
266
+ duration := time .Duration (float64 (time .Until (e .token .expiresOn )) * e .expirationRefreshRatio )
267
+ if duration < e .lowerBoundDuration {
268
+ return e .lowerBoundDuration
269
+ }
270
+
271
+ return duration
272
+ }
273
+
241
274
// Start starts the token manager and returns cancelFunc to stop the token manager.
242
275
// It takes a TokenListener as an argument, which is used to receive updates.
243
276
// The token manager will call the listener's OnTokenNext method with the updated token.
@@ -265,7 +298,7 @@ func (e *entraidTokenManager) Start(listener TokenListener) (cancelFunc, error)
265
298
case <- e .closed :
266
299
// Token manager is closed, stop the loop
267
300
return
268
- case <- time .After (time . Until ( token . expiresOn ) * time . Duration ( e . expirationRefreshRatio )):
301
+ case <- time .After (e . durationToRenewal ( )):
269
302
// Token is about to expire, refresh it
270
303
for i := 0 ; i < e .retryOptions .MaxAttempts ; i ++ {
271
304
select {
0 commit comments