Skip to content

Commit e4408cb

Browse files
committed
reorganize
1 parent 73759c7 commit e4408cb

23 files changed

+646
-579
lines changed

credentials_provider.go

+13-30
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,35 @@ import (
44
"fmt"
55
"sync"
66

7+
"github.com/redis-developer/go-redis-entraid/manager"
8+
"github.com/redis-developer/go-redis-entraid/token"
79
"github.com/redis/go-redis/v9/auth"
810
)
911

10-
// entraidCredentialsProvider implements the auth.StreamingCredentialsProvider interface.
12+
// Ensure entraidCredentialsProvider implements the auth.StreamingCredentialsProvider interface.
1113
var _ auth.StreamingCredentialsProvider = (*entraidCredentialsProvider)(nil)
1214

13-
// entraidCredentialsProvider is a struct that implements the CredentialProvider interface.
15+
// entraidCredentialsProvider is a struct that implements the StreamingCredentialsProvider interface.
1416
type entraidCredentialsProvider struct {
1517
options CredentialsProviderOptions
1618

17-
tokenManager TokenManager
18-
cancelTokenManager cancelFunc
19+
tokenManager manager.TokenManager
20+
cancelTokenManager manager.CancelFunc
1921

2022
// listeners is a slice of listeners that are notified when the token manager receives a new token.
2123
listeners []auth.CredentialsListener
2224

2325
// rwLock is a mutex that is used to synchronize access to the listeners slice.
24-
// It is used to ensure that only one goroutine can access the listeners slice at a time.
2526
rwLock sync.RWMutex
2627
}
2728

28-
// onTokenNext is a method that is called when the token manager receives a new token.
29-
func (e *entraidCredentialsProvider) onTokenNext(token *Token) {
29+
// onTokenNext is a method that is called when the token manager receives a new manager.
30+
func (e *entraidCredentialsProvider) onTokenNext(t *token.Token) {
3031
e.rwLock.RLock()
3132
defer e.rwLock.RUnlock()
32-
// Notify all listeners with the new token.
33+
// Notify all listeners with the new manager.
3334
for _, listener := range e.listeners {
34-
listener.OnNext(token)
35+
listener.OnNext(t)
3536
}
3637
}
3738

@@ -63,7 +64,7 @@ func (e *entraidCredentialsProvider) Subscribe(listener auth.CredentialsListener
6364
}
6465

6566
if !alreadySubscribed {
66-
// Get the token from the identity provider.
67+
// Get the manager from the identity provider.
6768
e.listeners = append(e.listeners, listener)
6869
}
6970
e.rwLock.Unlock()
@@ -102,30 +103,12 @@ func (e *entraidCredentialsProvider) Subscribe(listener auth.CredentialsListener
102103
return token, cancel, nil
103104
}
104105

105-
type entraidTokenListener struct {
106-
cp *entraidCredentialsProvider
107-
}
108-
109-
func tokenListenerFromCP(cp *entraidCredentialsProvider) TokenListener {
110-
return &entraidTokenListener{
111-
cp,
112-
}
113-
}
114-
115-
func (l *entraidTokenListener) OnTokenNext(token *Token) {
116-
l.cp.onTokenNext(token)
117-
}
118-
119-
func (l *entraidTokenListener) OnTokenError(err error) {
120-
l.cp.onTokenError(err)
121-
}
122-
123106
// newCredentialsProvider creates a new credentials provider.
124107
// It takes a TokenManager and CredentialProviderOptions as arguments and returns a StreamingCredentialsProvider interface.
125-
// The TokenManager is used to obtain the token, and the CredentialProviderOptions contains options for the credentials provider.
108+
// The TokenManager is used to obtain the manager, and the CredentialProviderOptions contains options for the credentials provider.
126109
// The credentials provider is responsible for managing the credentials and refreshing them when necessary.
127110
// It returns an error if the token manager cannot be started.
128-
func newCredentialsProvider(tokenManager TokenManager, options CredentialsProviderOptions) (auth.StreamingCredentialsProvider, error) {
111+
func newCredentialsProvider(tokenManager manager.TokenManager, options CredentialsProviderOptions) (auth.StreamingCredentialsProvider, error) {
129112
cp := &entraidCredentialsProvider{
130113
tokenManager: tokenManager,
131114
options: options,

entraid_test.go

-176
This file was deleted.

examples/example_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
func ExampleEstablishRedisConn() {
11+
1112
rdb := redis.NewUniversalClient(&redis.UniversalOptions{
1213
Addrs: []string{"localhost:6379"},
1314
})

authority_configuration.go renamed to identity/authority_configuration.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
package entraid
1+
package identity
22

33
import "fmt"
44

55
const (
66
// AuthorityTypeDefault is the default authority type.
7-
// This is used to specify the authority type when requesting a token.
7+
// This is used to specify the authority type when requesting a manager.
88
AuthorityTypeDefault = "default"
99
// AuthorityTypeMultiTenant is the multi-tenant authority type.
10-
// This is used to specify the multi-tenant authority type when requesting a token.
11-
// This type of authority is used to authenticate the identity when requesting a token.
10+
// This is used to specify the multi-tenant authority type when requesting a manager.
11+
// This type of authority is used to authenticate the identity when requesting a manager.
1212
AuthorityTypeMultiTenant = "multi-tenant"
1313
// AuthorityTypeCustom is the custom authority type.
14-
// This is used to specify the custom authority type when requesting a token.
14+
// This is used to specify the custom authority type when requesting a manager.
1515
AuthorityTypeCustom = "custom"
1616
)
1717

1818
// AuthorityConfiguration represents the authority configuration for the identity provider.
19-
// It is used to configure the authority type and authority URL when requesting a token.
19+
// It is used to configure the authority type and authority URL when requesting a manager.
2020
type AuthorityConfiguration struct {
2121
// AuthorityType is the type of authority used to authenticate with the identity provider.
2222
// This can be either "default", "multi-tenant", or "custom".
@@ -28,7 +28,7 @@ type AuthorityConfiguration struct {
2828
Authority string
2929

3030
// TenantID is the tenant ID of the identity provider.
31-
// This is used to identify the tenant when requesting a token.
31+
// This is used to identify the tenant when requesting a manager.
3232
// This is typically the ID of the Azure Active Directory tenant.
3333
TenantID string
3434
}

authority_configuration_test.go renamed to identity/authority_configuration_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package entraid
1+
package identity
22

33
import (
44
"testing"

azure_default_identity_provider.go renamed to identity/azure_default_identity_provider.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package entraid
1+
package identity
22

33
import (
44
"context"
@@ -7,13 +7,14 @@ import (
77
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
88
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
99
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
10+
"github.com/redis-developer/go-redis-entraid/shared"
1011
)
1112

1213
// DefaultAzureIdentityProviderOptions represents the options for the DefaultAzureIdentityProvider.
1314
type DefaultAzureIdentityProviderOptions struct {
1415
// AzureOptions is the options used to configure the Azure identity provider.
1516
AzureOptions *azidentity.DefaultAzureCredentialOptions
16-
// Scopes is the list of scopes used to request a token from the identity provider.
17+
// Scopes is the list of scopes used to request a manager from the identity provider.
1718
Scopes []string
1819

1920
// credFactory is a factory for creating the default Azure credential.
@@ -23,16 +24,16 @@ type DefaultAzureIdentityProviderOptions struct {
2324
}
2425

2526
type credFactory interface {
26-
NewDefaultAzureCredential(options *azidentity.DefaultAzureCredentialOptions) (defaultAzureCredential, error)
27+
NewDefaultAzureCredential(options *azidentity.DefaultAzureCredentialOptions) (azureCredential, error)
2728
}
2829

29-
type defaultAzureCredential interface {
30+
type azureCredential interface {
3031
GetToken(ctx context.Context, options policy.TokenRequestOptions) (azcore.AccessToken, error)
3132
}
3233

3334
type defaultCredFactory struct{}
3435

35-
func (d *defaultCredFactory) NewDefaultAzureCredential(options *azidentity.DefaultAzureCredentialOptions) (defaultAzureCredential, error) {
36+
func (d *defaultCredFactory) NewDefaultAzureCredential(options *azidentity.DefaultAzureCredentialOptions) (azureCredential, error) {
3637
return azidentity.NewDefaultAzureCredential(options)
3738
}
3839

@@ -55,9 +56,9 @@ func NewDefaultAzureIdentityProvider(opts DefaultAzureIdentityProviderOptions) (
5556
}, nil
5657
}
5758

58-
// RequestToken requests a token from the Azure Default Identity provider.
59-
// It returns the token, the expiration time, and an error if any.
60-
func (a *DefaultAzureIdentityProvider) RequestToken() (IdentityProviderResponse, error) {
59+
// RequestToken requests a manager from the Azure Default Identity provider.
60+
// It returns the manager, the expiration time, and an error if any.
61+
func (a *DefaultAzureIdentityProvider) RequestToken() (shared.IdentityProviderResponse, error) {
6162
credFactory := a.credFactory
6263
if credFactory == nil {
6364
credFactory = &defaultCredFactory{}
@@ -69,8 +70,8 @@ func (a *DefaultAzureIdentityProvider) RequestToken() (IdentityProviderResponse,
6970

7071
token, err := cred.GetToken(context.TODO(), policy.TokenRequestOptions{Scopes: a.scopes})
7172
if err != nil {
72-
return nil, fmt.Errorf("failed to get token: %w", err)
73+
return nil, fmt.Errorf("failed to get manager: %w", err)
7374
}
7475

75-
return NewIDPResponse(ResponseTypeAccessToken, &token)
76+
return shared.NewIDPResponse(shared.ResponseTypeAccessToken, &token)
7677
}

0 commit comments

Comments
 (0)