@@ -31,12 +31,13 @@ type TokenManagerOptions struct {
31
31
// default: 0 ms (no lower bound, refresh based on ExpirationRefreshRatio)
32
32
LowerRefreshBoundMs int
33
33
34
- // TokenParser is a function that parses the raw token and returns a Token object .
35
- // The function takes the raw token as a string and returns a Token object and an error .
34
+ // IdentityProviderResponseParser is a function that parses the IdentityProviderResponse .
35
+ // The function takes the response and based on its type returns the populated Token object.
36
36
// If this function is not provided, the default implementation will be used.
37
37
//
38
38
// required: true
39
- TokenParser TokenParserFunc
39
+ // default: defaultIdentityProviderResponseParser
40
+ IdentityProviderResponseParser IdentityProviderResponseParserFunc
40
41
41
42
// RetryOptions is a struct that contains the options for retrying the token request.
42
43
// It contains the maximum number of attempts, initial delay, maximum delay, and backoff multiplier.
@@ -84,17 +85,28 @@ type TokenManager interface {
84
85
Close () error
85
86
}
86
87
87
- // defaultTokenParser is a function that parses the raw token and returns Token object.
88
- var defaultTokenParser = func (rawToken string , expiresOn time.Time ) (* Token , error ) {
89
- // Parse the token and return the username and password.
90
- // In this example, we are just returning the raw token as the password.
91
- // In a real implementation, you would parse the token and extract the username and password.
92
- // For example, if the token is a JWT, you would decode the JWT and extract the claims.
93
- // This is just a placeholder implementation.
94
- // You should replace this with your own implementation.
95
- if rawToken == "" {
96
- return nil , fmt .Errorf ("raw token is empty" )
88
+ // defaultIdentityProviderResponseParser is a function that parses the token and returns the username and password.
89
+ var defaultIdentityProviderResponseParser = func (response IdentityProviderResponse ) (* Token , error ) {
90
+ var username , password , rawToken string
91
+ var expiresOn time.Time
92
+ if response == nil {
93
+ return nil , fmt .Errorf ("response is nil" )
97
94
}
95
+ switch response .Type () {
96
+ case typeAuthResult :
97
+ authResult := response .AuthResult ()
98
+ if authResult == nil {
99
+ return nil , fmt .Errorf ("auth result is nil" )
100
+ }
101
+ case typeAccessToken :
102
+ accessToken := response .AccessToken ()
103
+ if accessToken == nil {
104
+ return nil , fmt .Errorf ("access token is nil" )
105
+ }
106
+ default :
107
+ return nil , fmt .Errorf ("unknown response type: %s" , response .Type ())
108
+ }
109
+
98
110
if expiresOn .IsZero () {
99
111
return nil , fmt .Errorf ("expires on is zero" )
100
112
}
@@ -143,8 +155,9 @@ func NewTokenManager(idp IdentityProvider, options TokenManagerOptions) (TokenMa
143
155
type entraidTokenManager struct {
144
156
idp IdentityProvider
145
157
token * Token
158
+
146
159
// TokenParser is a function that parses the token.
147
- tokenParser TokenParserFunc
160
+ identityProviderResponseParser IdentityProviderResponseParserFunc
148
161
149
162
// retryOptions is a struct that contains the options for retrying the token request.
150
163
// It contains the maximum number of attempts, initial delay, maximum delay, and backoff multiplier.
@@ -175,12 +188,12 @@ func (e *entraidTokenManager) GetToken() (*Token, error) {
175
188
return copyToken (e .token ), nil
176
189
}
177
190
178
- rawToken , expiresOn , err := e .idp .RequestToken ()
191
+ idpResult , err := e .idp .RequestToken ()
179
192
if err != nil {
180
- return nil , fmt .Errorf ("failed to request token: %w" , err )
193
+ return nil , fmt .Errorf ("failed to request token from idp : %w" , err )
181
194
}
182
195
183
- token , err := e .tokenParser ( rawToken , expiresOn )
196
+ token , err := e .identityProviderResponseParser ( idpResult )
184
197
if err != nil {
185
198
return nil , fmt .Errorf ("failed to parse token: %w" , err )
186
199
}
@@ -299,7 +312,7 @@ var defaultRetryableFunc = func(err error) bool {
299
312
}
300
313
301
314
if ok := errors .As (err , netErr ); ok {
302
- return netErr .Timeout () || netErr . Temporary ()
315
+ return netErr .Timeout ()
303
316
}
304
317
return false
305
318
}
@@ -328,19 +341,19 @@ func defaultRetryOptionsOr(retryOptions RetryOptions) RetryOptions {
328
341
return retryOptions
329
342
}
330
343
331
- // defaultTokenParserOr returns the default token parser if the provided token parser is not set.
332
- // It sets the default token parser to the defaultTokenParser function.
344
+ // defaultIdentityProviderResponseParserOr returns the default token parser if the provided token parser is not set.
345
+ // It sets the default token parser to the defaultIdentityProviderResponseParser function.
333
346
// The default token parser is used to parse the raw token and return a Token object.
334
- func defaultTokenParserOr ( tokenParser TokenParserFunc ) TokenParserFunc {
335
- if tokenParser == nil {
336
- return defaultTokenParser
347
+ func defaultIdentityProviderResponseParserOr ( idpResponseParser IdentityProviderResponseParserFunc ) IdentityProviderResponseParserFunc {
348
+ if idpResponseParser == nil {
349
+ return defaultIdentityProviderResponseParser
337
350
}
338
- return tokenParser
351
+ return idpResponseParser
339
352
}
340
353
341
354
func defaultTokenManagerOptionsOr (options TokenManagerOptions ) TokenManagerOptions {
342
355
options .RetryOptions = defaultRetryOptionsOr (options .RetryOptions )
343
- options .TokenParser = defaultTokenParserOr (options .TokenParser )
356
+ options .IdentityProviderResponseParser = defaultIdentityProviderResponseParserOr (options .IdentityProviderResponseParser )
344
357
if options .ExpirationRefreshRatio <= 0 || options .ExpirationRefreshRatio > 1 {
345
358
options .ExpirationRefreshRatio = 0.7
346
359
}
0 commit comments