Skip to content

Commit 91eb076

Browse files
committed
use struct so it is easier to mock
1 parent 7fb64c1 commit 91eb076

File tree

4 files changed

+121
-57
lines changed

4 files changed

+121
-57
lines changed

entraid_test.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ import (
2323
// }
2424
const testJWTtoken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZXN0IGp3dCIsImlhdCI6MTc0MzUxNTAxMSwiZXhwIjoxNzc1MDUxMDExLCJhdWQiOiJ3d3cuZXhhbXBsZS5jb20iLCJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwib2lkIjoidGVzdCJ9.6RG721V2eFlSLsCRmo53kSRRrTZIe1UPdLZCUEvIarU"
2525

26+
var testTokenValid = NewToken(
27+
"test",
28+
"password",
29+
"test",
30+
time.Now().Add(time.Hour),
31+
time.Now(),
32+
int64(time.Hour),
33+
)
34+
35+
type mockIdentityProviderResponseParser struct {
36+
// Mock implementation of the IdentityProviderResponseParser interface
37+
mock.Mock
38+
}
39+
40+
func (m *mockIdentityProviderResponseParser) ParseResponse(response IdentityProviderResponse) (*Token, error) {
41+
args := m.Called(response)
42+
if args.Get(0) == nil {
43+
return nil, args.Error(1)
44+
}
45+
return args.Get(0).(*Token), args.Error(1)
46+
}
47+
2648
type mockIdentityProvider struct {
2749
// Mock implementation of the IdentityProvider interface
2850
// Add any necessary fields or methods for the mock identity provider here
@@ -75,20 +97,6 @@ func (m *mockTokenListener) OnTokenError(err error) {
7597
_ = m.Called(err)
7698
}
7799

78-
func mockTokenParserFunc(idpResponse IdentityProviderResponse) (*Token, error) {
79-
if idpResponse != nil && idpResponse.Type() == ResponseTypeRawToken {
80-
return NewToken(
81-
"test",
82-
"password",
83-
"test",
84-
time.Now().Add(time.Hour),
85-
time.Now(),
86-
int64(time.Hour),
87-
), nil
88-
}
89-
return nil, nil
90-
}
91-
92100
type mockAzureCredential struct {
93101
mock.Mock
94102
}

identity_provider.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ type IdentityProviderResponse interface {
2626
RawToken() string
2727
}
2828

29-
// IdentityProviderResponseParserFunc is a function that parses the token and returns the username and password.
30-
type IdentityProviderResponseParserFunc func(response IdentityProviderResponse) (*Token, error)
29+
// IdentityProviderResponseParser is an interface that defines the methods for parsing the identity provider response.
30+
// It is used to parse the response from the identity provider and extract the token.
31+
// If not provided, the default implementation will be used.
32+
type IdentityProviderResponseParser interface {
33+
ParseResponse(response IdentityProviderResponse) (*Token, error)
34+
}
3135

3236
// IdentityProvider is an interface that defines the methods for an identity provider.
3337
// It is used to request a token for authentication.

token_manager.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ type TokenManagerOptions struct {
2929
//
3030
// default: 0 ms (no lower bound, refresh based on ExpirationRefreshRatio)
3131
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.
3537
//
36-
// required: true
38+
// required: false
3739
// default: defaultIdentityProviderResponseParser
38-
IdentityProviderResponseParser IdentityProviderResponseParserFunc
40+
IdentityProviderResponseParser IdentityProviderResponseParser
3941
// RetryOptions is a struct that contains the options for retrying the token request.
4042
// It contains the maximum number of attempts, initial delay, maximum delay, and backoff multiplier.
4143
//
@@ -79,9 +81,12 @@ type TokenManager interface {
7981
// Close closes the token manager and releases any resources.
8082
Close() error
8183
}
84+
type defaultIdentityProviderResponseParser struct{}
8285

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) {
8590
var username, password, rawToken string
8691
var expiresOn time.Time
8792
if response == nil {
@@ -155,6 +160,9 @@ var defaultIdentityProviderResponseParser IdentityProviderResponseParserFunc = f
155160
), nil
156161
}
157162

163+
// entraidIdentityProviderResponseParser is the default implementation of the IdentityProviderResponseParser interface.
164+
var entraidIdentityProviderResponseParser IdentityProviderResponseParser = &defaultIdentityProviderResponseParser{}
165+
158166
// NewTokenManager creates a new TokenManager.
159167
// It takes an IdentityProvider and TokenManagerOptions as arguments and returns a TokenManager interface.
160168
// The IdentityProvider is used to obtain the token, and the TokenManagerOptions contains options for the TokenManager.
@@ -189,10 +197,9 @@ type entraidTokenManager struct {
189197
// token is the authentication token for the user which should be kept in memory if valid.
190198
token *Token
191199

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
196203

197204
// retryOptions is a struct that contains the options for retrying the token request.
198205
// It contains the maximum number of attempts, initial delay, maximum delay, and backoff multiplier.
@@ -242,7 +249,7 @@ func (e *entraidTokenManager) GetToken() (*Token, error) {
242249
return nil, fmt.Errorf("failed to request token from idp: %w", err)
243250
}
244251

245-
token, err := e.identityProviderResponseParser(idpResult)
252+
token, err := e.identityProviderResponseParser.ParseResponse(idpResult)
246253
if err != nil {
247254
return nil, fmt.Errorf("failed to parse token: %w", err)
248255
}
@@ -420,9 +427,9 @@ func defaultRetryOptionsOr(retryOptions RetryOptions) RetryOptions {
420427
// defaultIdentityProviderResponseParserOr returns the default token parser if the provided token parser is not set.
421428
// It sets the default token parser to the defaultIdentityProviderResponseParser function.
422429
// 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 {
424431
if idpResponseParser == nil {
425-
return defaultIdentityProviderResponseParser
432+
return &defaultIdentityProviderResponseParser{}
426433
}
427434
return idpResponseParser
428435
}

0 commit comments

Comments
 (0)