Skip to content

Commit 52d0660

Browse files
committed
add more tests
1 parent 0c0bda5 commit 52d0660

File tree

4 files changed

+408
-4
lines changed

4 files changed

+408
-4
lines changed

identity/authority_configuration_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ func TestAuthorityConfiguration(t *testing.T) {
3636
expected: "https://custom-authority.com",
3737
expectError: false,
3838
},
39+
{
40+
name: "Invalid Authority Type",
41+
authorityType: "invalid",
42+
expectError: true,
43+
},
44+
{
45+
name: "Missing Tenant ID for Multi-Tenant",
46+
authorityType: AuthorityTypeMultiTenant,
47+
expectError: true,
48+
},
49+
{
50+
name: "Missing Authority for Custom",
51+
authorityType: AuthorityTypeCustom,
52+
expectError: true,
53+
},
54+
{
55+
name: "Default Authority Type with Tenant ID",
56+
authorityType: AuthorityTypeDefault,
57+
tenantID: "12345",
58+
expected: "https://login.microsoftonline.com/common",
59+
expectError: false,
60+
},
3961
}
4062

4163
for _, test := range tests {

identity/confidential_identity_provider.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ type ConfidentialIdentityProviderOptions struct {
3232

3333
// Authority is the authority used to authenticate with the identity provider.
3434
Authority AuthorityConfiguration
35+
36+
// confidentialCredFactory is a factory for creating the confidential credential.
37+
// This is used for testing purposes, to allow mocking the credential creation.
38+
confidentialCredFactory confidentialCredFactory
3539
}
3640

3741
// ConfidentialIdentityProvider represents a confidential identity provider.
@@ -46,7 +50,34 @@ type ConfidentialIdentityProvider struct {
4650
scopes []string
4751

4852
// client confidential is the client used to request a manager from the identity provider.
49-
client *confidential.Client
53+
client confidentialTokenClient
54+
}
55+
56+
// confidentialCredFacotory is a factory for creating the confidential credential.
57+
// Introduced for testing purposes. This allows mocking the credential creation, default behavior is to use the confidential.NewCredFromSecret and confidential.NewCredFromCert methods.
58+
type confidentialCredFactory interface {
59+
NewCredFromSecret(clientSecret string) (confidential.Credential, error)
60+
NewCredFromCert(clientCert []*x509.Certificate, clientPrivateKey crypto.PrivateKey) (confidential.Credential, error)
61+
}
62+
63+
// confidentialTokenClient is an interface that defines the methods for a confidential token client.
64+
// It is used to acquire a token using the client credentials.
65+
// Introduced for testing purposes. This allows mocking the token client, default behavior is to use the
66+
// client returned by confidential.New method.
67+
type confidentialTokenClient interface {
68+
// AcquireTokenByCredential acquires a token using the client credentials.
69+
// It returns the token and an error if any.
70+
AcquireTokenByCredential(ctx context.Context, scopes []string, opts ...confidential.AcquireByCredentialOption) (confidential.AuthResult, error)
71+
}
72+
73+
type defaultConfidentialCredFactory struct{}
74+
75+
func (d *defaultConfidentialCredFactory) NewCredFromSecret(clientSecret string) (confidential.Credential, error) {
76+
return confidential.NewCredFromSecret(clientSecret)
77+
}
78+
79+
func (d *defaultConfidentialCredFactory) NewCredFromCert(clientCert []*x509.Certificate, clientPrivateKey crypto.PrivateKey) (confidential.Credential, error) {
80+
return confidential.NewCredFromCert(clientCert, clientPrivateKey)
5081
}
5182

5283
// NewConfidentialIdentityProvider creates a new confidential identity provider.
@@ -57,6 +88,7 @@ type ConfidentialIdentityProvider struct {
5788
// The authority is used to authenticate with the identity provider.
5889
func NewConfidentialIdentityProvider(opts ConfidentialIdentityProviderOptions) (*ConfidentialIdentityProvider, error) {
5990
var credential confidential.Credential
91+
var credFactory confidentialCredFactory
6092
var authority string
6193
var err error
6294

@@ -74,14 +106,19 @@ func NewConfidentialIdentityProvider(opts ConfidentialIdentityProviderOptions) (
74106
return nil, fmt.Errorf("failed to get authority: %w", err)
75107
}
76108

109+
credFactory = &defaultConfidentialCredFactory{}
110+
if opts.confidentialCredFactory != nil {
111+
credFactory = opts.confidentialCredFactory
112+
}
113+
77114
switch opts.CredentialsType {
78115
case ClientSecretCredentialType:
79116
// ClientSecretCredentialType is the type of credentials that uses a client secret to authenticate.
80117
if opts.ClientSecret == "" {
81118
return nil, fmt.Errorf("client secret is required when using client secret credentials")
82119
}
83120

84-
credential, err = confidential.NewCredFromSecret(opts.ClientSecret)
121+
credential, err = credFactory.NewCredFromSecret(opts.ClientSecret)
85122
if err != nil {
86123
return nil, fmt.Errorf("failed to create client secret credential: %w", err)
87124
}
@@ -93,7 +130,7 @@ func NewConfidentialIdentityProvider(opts ConfidentialIdentityProviderOptions) (
93130
if opts.ClientPrivateKey == nil {
94131
return nil, fmt.Errorf("client private key is required when using client certificate credentials")
95132
}
96-
credential, err = confidential.NewCredFromCert(opts.ClientCert, opts.ClientPrivateKey)
133+
credential, err = credFactory.NewCredFromCert(opts.ClientCert, opts.ClientPrivateKey)
97134
if err != nil {
98135
return nil, fmt.Errorf("failed to create client certificate credential: %w", err)
99136
}
@@ -125,7 +162,7 @@ func (c *ConfidentialIdentityProvider) RequestToken() (shared.IdentityProviderRe
125162

126163
result, err := c.client.AcquireTokenByCredential(context.TODO(), c.scopes)
127164
if err != nil {
128-
return nil, fmt.Errorf("failed to acquire manager: %w", err)
165+
return nil, fmt.Errorf("failed to acquire token: %w", err)
129166
}
130167

131168
return shared.NewIDPResponse(shared.ResponseTypeAuthResult, &result)

0 commit comments

Comments
 (0)