@@ -29,13 +29,15 @@ type TokenManagerOptions struct {
29
29
//
30
30
// default: 0 ms (no lower bound, refresh based on ExpirationRefreshRatio)
31
31
LowerRefreshBoundMs int64
32
- // IdentityProviderResponseParser is a function that parses the IdentityProviderResponse.
33
- // The function takes the response and based on its type returns the populated Token object.
34
- // If this function is not provided, the default implementation will be used.
32
+
33
+ // IdentityProviderResponseParser is an optional object that implements the IdentityProviderResponseParser interface.
34
+ // It is used to parse the response from the identity provider and extract the token.
35
+ // If not provided, the default implementation will be used.
36
+ // The objects ParseResponse method will be called to parse the response and return the token.
35
37
//
36
- // required: true
38
+ // required: false
37
39
// default: defaultIdentityProviderResponseParser
38
- IdentityProviderResponseParser IdentityProviderResponseParserFunc
40
+ IdentityProviderResponseParser IdentityProviderResponseParser
39
41
// RetryOptions is a struct that contains the options for retrying the token request.
40
42
// It contains the maximum number of attempts, initial delay, maximum delay, and backoff multiplier.
41
43
//
@@ -79,9 +81,12 @@ type TokenManager interface {
79
81
// Close closes the token manager and releases any resources.
80
82
Close () error
81
83
}
84
+ type defaultIdentityProviderResponseParser struct {}
82
85
83
- // defaultIdentityProviderResponseParser is a function that parses the token and returns the username and password.
84
- var defaultIdentityProviderResponseParser IdentityProviderResponseParserFunc = func (response IdentityProviderResponse ) (* Token , error ) {
86
+ // ParseResponse parses the response from the identity provider and extracts the token.
87
+ // It takes an IdentityProviderResponse as an argument and returns a Token and an error if any.
88
+ // The IdentityProviderResponse contains the raw token and the expiration time.
89
+ func (* defaultIdentityProviderResponseParser ) ParseResponse (response IdentityProviderResponse ) (* Token , error ) {
85
90
var username , password , rawToken string
86
91
var expiresOn time.Time
87
92
if response == nil {
@@ -155,6 +160,9 @@ var defaultIdentityProviderResponseParser IdentityProviderResponseParserFunc = f
155
160
), nil
156
161
}
157
162
163
+ // entraidIdentityProviderResponseParser is the default implementation of the IdentityProviderResponseParser interface.
164
+ var entraidIdentityProviderResponseParser IdentityProviderResponseParser = & defaultIdentityProviderResponseParser {}
165
+
158
166
// NewTokenManager creates a new TokenManager.
159
167
// It takes an IdentityProvider and TokenManagerOptions as arguments and returns a TokenManager interface.
160
168
// The IdentityProvider is used to obtain the token, and the TokenManagerOptions contains options for the TokenManager.
@@ -189,10 +197,9 @@ type entraidTokenManager struct {
189
197
// token is the authentication token for the user which should be kept in memory if valid.
190
198
token * Token
191
199
192
- // identityProviderResponseParser is a function that parses the IdentityProviderResponse.
193
- // it can be supplied by the user to parse the token and return the populated Token object or
194
- // the default implementation will be used.
195
- identityProviderResponseParser IdentityProviderResponseParserFunc
200
+ // identityProviderResponseParser is the parser used to parse the response from the identity provider.
201
+ // It`s ParseResponse method will be called to parse the response and return the token.
202
+ identityProviderResponseParser IdentityProviderResponseParser
196
203
197
204
// retryOptions is a struct that contains the options for retrying the token request.
198
205
// It contains the maximum number of attempts, initial delay, maximum delay, and backoff multiplier.
@@ -242,7 +249,7 @@ func (e *entraidTokenManager) GetToken() (*Token, error) {
242
249
return nil , fmt .Errorf ("failed to request token from idp: %w" , err )
243
250
}
244
251
245
- token , err := e .identityProviderResponseParser (idpResult )
252
+ token , err := e .identityProviderResponseParser . ParseResponse (idpResult )
246
253
if err != nil {
247
254
return nil , fmt .Errorf ("failed to parse token: %w" , err )
248
255
}
@@ -420,9 +427,9 @@ func defaultRetryOptionsOr(retryOptions RetryOptions) RetryOptions {
420
427
// defaultIdentityProviderResponseParserOr returns the default token parser if the provided token parser is not set.
421
428
// It sets the default token parser to the defaultIdentityProviderResponseParser function.
422
429
// The default token parser is used to parse the raw token and return a Token object.
423
- func defaultIdentityProviderResponseParserOr (idpResponseParser IdentityProviderResponseParserFunc ) IdentityProviderResponseParserFunc {
430
+ func defaultIdentityProviderResponseParserOr (idpResponseParser IdentityProviderResponseParser ) IdentityProviderResponseParser {
424
431
if idpResponseParser == nil {
425
- return defaultIdentityProviderResponseParser
432
+ return & defaultIdentityProviderResponseParser {}
426
433
}
427
434
return idpResponseParser
428
435
}
0 commit comments