Skip to content

Commit 62ee78b

Browse files
committed
Identity Provider - Default Implementation
1 parent 49585a7 commit 62ee78b

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

providers.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package entraid
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/redis-developer/go-redis-entraid/identity"
7+
"github.com/redis-developer/go-redis-entraid/manager"
8+
"github.com/redis-developer/go-redis-entraid/shared"
9+
"github.com/redis/go-redis/v9/auth"
10+
)
11+
12+
// CredentialsProviderOptions is a struct that holds the options for the credentials provider.
13+
// It is used to configure the streaming credentials provider when requesting a token with a token manager.
14+
type CredentialsProviderOptions struct {
15+
// ClientID is the client ID of the identity.
16+
// This is used to identify the identity when requesting a token.
17+
ClientID string
18+
19+
// TokenManagerOptions is the options for the token manager.
20+
// This is used to configure the token manager when requesting a token.
21+
TokenManagerOptions manager.TokenManagerOptions
22+
23+
// tokenManagerFactory is a private field that can be injected from within the package.
24+
// It is used to create a token manager for the credentials provider.
25+
tokenManagerFactory func(shared.IdentityProvider, manager.TokenManagerOptions) (manager.TokenManager, error)
26+
}
27+
28+
// defaultTokenManagerFactory is the default implementation of the token manager factory.
29+
// It creates a new token manager using the provided identity provider and options.
30+
func defaultTokenManagerFactory(provider shared.IdentityProvider, options manager.TokenManagerOptions) (manager.TokenManager, error) {
31+
return manager.NewTokenManager(provider, options)
32+
}
33+
34+
// getTokenManagerFactory returns the token manager factory to use.
35+
// If no factory is provided, it returns the default implementation.
36+
func (o *CredentialsProviderOptions) getTokenManagerFactory() func(shared.IdentityProvider, manager.TokenManagerOptions) (manager.TokenManager, error) {
37+
if o.tokenManagerFactory == nil {
38+
return defaultTokenManagerFactory
39+
}
40+
return o.tokenManagerFactory
41+
}
42+
43+
// Managed identity type
44+
45+
// ManagedIdentityCredentialsProviderOptions is a struct that holds the options for the managed identity credentials provider.
46+
type ManagedIdentityCredentialsProviderOptions struct {
47+
// CredentialsProviderOptions is the options for the credentials provider.
48+
// This is used to configure the credentials provider when requesting a token.
49+
// It is used to specify the client ID, tenant ID, and scopes for the identity.
50+
CredentialsProviderOptions
51+
52+
// ManagedIdentityProviderOptions is the options for the managed identity provider.
53+
// This is used to configure the managed identity provider when requesting a token.
54+
identity.ManagedIdentityProviderOptions
55+
}
56+
57+
// NewManagedIdentityCredentialsProvider creates a new streaming credentials provider for managed identity.
58+
// It uses the provided options to configure the provider.
59+
// Use this when you want either a system assigned identity or a user assigned identity.
60+
// The system assigned identity is automatically managed by Azure and does not require any additional configuration.
61+
// The user assigned identity is a separate resource that can be managed independently.
62+
func NewManagedIdentityCredentialsProvider(options ManagedIdentityCredentialsProviderOptions) (auth.StreamingCredentialsProvider, error) {
63+
// Create a new identity provider using the managed identity type.
64+
idp, err := identity.NewManagedIdentityProvider(options.ManagedIdentityProviderOptions)
65+
if err != nil {
66+
return nil, fmt.Errorf("cannot create managed identity provider: %w", err)
67+
}
68+
69+
// Create a new token manager using the identity provider.
70+
tokenManager, err := options.getTokenManagerFactory()(idp, options.TokenManagerOptions)
71+
if err != nil {
72+
return nil, fmt.Errorf("cannot create token manager: %w", err)
73+
}
74+
// Create a new credentials provider using the token manager.
75+
credentialsProvider, err := NewCredentialsProvider(tokenManager, options.CredentialsProviderOptions)
76+
if err != nil {
77+
return nil, fmt.Errorf("cannot create credentials provider: %w", err)
78+
}
79+
80+
return credentialsProvider, nil
81+
}
82+
83+
// ConfidentialCredentialsProviderOptions is a struct that holds the options for the confidential credentials provider.
84+
// It is used to configure the credentials provider when requesting a token.
85+
type ConfidentialCredentialsProviderOptions struct {
86+
// CredentialsProviderOptions is the options for the credentials provider.
87+
// This is used to configure the credentials provider when requesting a token.
88+
CredentialsProviderOptions
89+
90+
// ConfidentialIdentityProviderOptions is the options for the confidential identity provider.
91+
// This is used to configure the identity provider when requesting a token.
92+
identity.ConfidentialIdentityProviderOptions
93+
}
94+
95+
// NewConfidentialCredentialsProvider creates a new confidential credentials provider.
96+
// It uses client id and client credentials to authenticate with the identity provider.
97+
// The client credentials can be either a client secret or a client certificate.
98+
func NewConfidentialCredentialsProvider(options ConfidentialCredentialsProviderOptions) (auth.StreamingCredentialsProvider, error) {
99+
// Create a new identity provider using the client ID and client credentials.
100+
idp, err := identity.NewConfidentialIdentityProvider(options.ConfidentialIdentityProviderOptions)
101+
if err != nil {
102+
return nil, fmt.Errorf("cannot create confidential identity provider: %w", err)
103+
}
104+
105+
// Create a new token manager using the identity provider.
106+
tokenManager, err := options.getTokenManagerFactory()(idp, options.TokenManagerOptions)
107+
if err != nil {
108+
return nil, fmt.Errorf("cannot create token manager: %w", err)
109+
}
110+
111+
// Create a new credentials provider using the token manager.
112+
credentialsProvider, err := NewCredentialsProvider(tokenManager, options.CredentialsProviderOptions)
113+
if err != nil {
114+
return nil, fmt.Errorf("cannot create credentials provider: %w", err)
115+
}
116+
return credentialsProvider, nil
117+
}
118+
119+
// DefaultAzureCredentialsProviderOptions is a struct that holds the options for the default azure credentials provider.
120+
// It is used to configure the credentials provider when requesting a token.
121+
type DefaultAzureCredentialsProviderOptions struct {
122+
CredentialsProviderOptions
123+
identity.DefaultAzureIdentityProviderOptions
124+
}
125+
126+
// NewDefaultAzureCredentialsProvider creates a new default azure credentials provider.
127+
// It uses the default azure identity provider to authenticate with the identity provider.
128+
// The default azure identity provider is a special type of identity provider that uses the default azure identity to authenticate.
129+
// It is used to authenticate with the identity provider when requesting a token.
130+
func NewDefaultAzureCredentialsProvider(options DefaultAzureCredentialsProviderOptions) (auth.StreamingCredentialsProvider, error) {
131+
// Create a new identity provider using the default azure identity type.
132+
idp, err := identity.NewDefaultAzureIdentityProvider(options.DefaultAzureIdentityProviderOptions)
133+
if err != nil {
134+
return nil, fmt.Errorf("cannot create default azure identity provider: %w", err)
135+
}
136+
137+
// Create a new token manager using the identity provider.
138+
tokenManager, err := options.getTokenManagerFactory()(idp, options.TokenManagerOptions)
139+
if err != nil {
140+
return nil, fmt.Errorf("cannot create token manager: %w", err)
141+
}
142+
143+
// Create a new credentials provider using the token manager.
144+
credentialsProvider, err := NewCredentialsProvider(tokenManager, options.CredentialsProviderOptions)
145+
if err != nil {
146+
return nil, fmt.Errorf("cannot create credentials provider: %w", err)
147+
}
148+
return credentialsProvider, nil
149+
}

0 commit comments

Comments
 (0)