Skip to content

Commit e86aae9

Browse files
committed
easier to extend
1 parent 51dab12 commit e86aae9

8 files changed

+38
-37
lines changed

azure_default_identity_provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ func (a *DefaultAzureIdentityProvider) RequestToken() (IdentityProviderResponse,
4343
return nil, fmt.Errorf("failed to get token: %w", err)
4444
}
4545

46-
return newIDPResponse(typeAccessToken, &token)
46+
return NewIDPResponse(ResponseTypeAccessToken, &token)
4747
}

confidential_identity_provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,5 @@ func (c *ConfidentialIdentityProvider) RequestToken() (IdentityProviderResponse,
127127
return nil, fmt.Errorf("failed to acquire token: %w", err)
128128
}
129129

130-
return newIDPResponse(typeAuthResult, &result)
130+
return NewIDPResponse(ResponseTypeAuthResult, &result)
131131
}

credentials.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

credentials_provider.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,35 +89,35 @@ func (e *entraidCredentialsProvider) Subscribe(listener auth.CredentialsListener
8989
}
9090
if len(e.listeners) == 0 {
9191
if e.cancelTokenManager != nil {
92-
e.cancelTokenManager()
92+
defer func() {
93+
e.cancelTokenManager = nil
94+
e.listeners = nil
95+
}()
96+
return e.cancelTokenManager()
9397
}
94-
e.cancelTokenManager = nil
95-
e.listeners = nil
9698
}
9799
return nil
98100
}
99101

100-
return credentials, cancel, nil
102+
return token, cancel, nil
101103
}
102104

103105
type entraidTokenListener struct {
104-
onTokenNext func(token *Token)
105-
onTokenError func(err error)
106+
cp *entraidCredentialsProvider
106107
}
107108

108-
func tokenListenerFromCP(cp *entraidCredentialsProvider) *entraidTokenListener {
109+
func tokenListenerFromCP(cp *entraidCredentialsProvider) TokenListener {
109110
return &entraidTokenListener{
110-
onTokenNext: cp.onTokenNext,
111-
onTokenError: cp.onTokenError,
111+
cp,
112112
}
113113
}
114114

115115
func (l *entraidTokenListener) OnTokenNext(token *Token) {
116-
l.onTokenNext(token)
116+
l.cp.onTokenNext(token)
117117
}
118118

119119
func (l *entraidTokenListener) OnTokenError(err error) {
120-
l.onTokenError(err)
120+
l.cp.onTokenError(err)
121121
}
122122

123123
// newCredentialsProvider creates a new credentials provider.

identity_provider.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import (
88
)
99

1010
const (
11-
// typeAuthResult is the type of the auth result.
12-
typeAuthResult = "AuthResult"
13-
// typeAccessToken is the type of the access token.
14-
typeAccessToken = "AccessToken"
11+
// ResponseTypeAuthResult is the type of the auth result.
12+
ResponseTypeAuthResult = "AuthResult"
13+
// ResponseTypeAccessToken is the type of the access token.
14+
ResponseTypeAccessToken = "AccessToken"
15+
// ResponseTypeRawToken is the type of the response when you have a raw string.
16+
ResponseTypeRawToken = "RawToken"
1517
)
1618

1719
// IdentityProviderResponse is an interface that defines the methods for an identity provider authentication result.
@@ -21,6 +23,7 @@ type IdentityProviderResponse interface {
2123
Type() string
2224
AuthResult() *public.AuthResult
2325
AccessToken() *azcore.AccessToken
26+
RawToken() string
2427
}
2528

2629
// IdentityProviderResponseParserFunc is a function that parses the token and returns the username and password.
@@ -39,6 +42,7 @@ type authResult struct {
3942
resultType string
4043
authResult *public.AuthResult
4144
accessToken *azcore.AccessToken
45+
rawToken string
4246
}
4347

4448
func (a *authResult) Type() string {
@@ -53,26 +57,36 @@ func (a *authResult) AccessToken() *azcore.AccessToken {
5357
return a.accessToken
5458
}
5559

56-
// newAuthResult creates a new auth result based on the type provided.
60+
func (a *authResult) RawToken() string {
61+
return a.rawToken
62+
}
63+
64+
// NewIDPResponse creates a new auth result based on the type provided.
5765
// It returns an IdentityProviderResponse interface.
58-
func newIDPResponse(t string, result interface{}) (IdentityProviderResponse, error) {
66+
func NewIDPResponse(t string, result interface{}) (IdentityProviderResponse, error) {
5967
r := &authResult{resultType: t}
6068

6169
switch t {
62-
case typeAuthResult:
70+
case ResponseTypeAuthResult:
6371
if typed, ok := result.(*public.AuthResult); !ok {
6472
return nil, fmt.Errorf("expected AuthResult, got %T", result)
6573
} else {
6674
r.authResult = typed
6775
}
68-
case typeAccessToken:
76+
case ResponseTypeAccessToken:
6977
if typed, ok := result.(*azcore.AccessToken); !ok {
7078
return nil, fmt.Errorf("expected AccessToken, got %T", result)
7179
} else {
7280
r.accessToken = typed
7381
}
82+
case ResponseTypeRawToken:
83+
if typed, ok := result.(string); !ok {
84+
return nil, fmt.Errorf("expected string, got %T", result)
85+
} else {
86+
r.rawToken = typed
87+
}
7488
default:
75-
return nil, fmt.Errorf("unknown type: %s", t)
89+
return nil, fmt.Errorf("unknown idp response type: %s", t)
7690
}
7791

7892
return r, nil

managed_identity_provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,5 @@ func (m *ManagedIdentityProvider) RequestToken() (IdentityProviderResponse, erro
9898
return nil, fmt.Errorf("coudn't acquire token: %w", err)
9999
}
100100

101-
return newIDPResponse(typeAuthResult, &authResult)
101+
return NewIDPResponse(ResponseTypeAuthResult, &authResult)
102102
}

token.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,6 @@ func (t *Token) RawCredentials() string {
3838
return t.rawToken
3939
}
4040

41-
// IsExpired checks if the token is expired.
42-
// It returns true if the token is expired, false otherwise.
43-
func (t *Token) IsExpired() bool {
44-
return t.expiresOn.Before(time.Now())
45-
}
46-
47-
// IsValid checks if the token is valid.
48-
// It returns true if the token is valid, false otherwise.
49-
func (t *Token) IsValid() bool {
50-
return !t.IsExpired()
51-
}
52-
5341
// ExpirationOn returns the expiration time of the token.
5442
func (t *Token) ExpirationOn() time.Time {
5543
return t.expiresOn

token_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ type entraidTokenManager struct {
225225
}
226226

227227
func (e *entraidTokenManager) GetToken() (*Token, error) {
228-
if e.token != nil && e.token.expiresOn.After(time.Now().Add(MinTokenTTL)) {
228+
if e.token != nil && e.token.expiresOn.Before(time.Now().Add(e.lowerBoundDuration)) {
229229
// copy the token so the caller can't modify it
230230
return copyToken(e.token), nil
231231
}

0 commit comments

Comments
 (0)