Skip to content

Commit ca920c5

Browse files
committed
wip
1 parent 5a5d599 commit ca920c5

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

identity_provider.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package entraid
22

33
type IdentityProvider interface {
4-
// requestToken requests a token from the identity provider.
5-
requestToken() (string, error)
4+
// RequestToken requests a token from the identity provider.
5+
RequestToken() (string, error)
66
}

token_manager.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package entraid
22

33
import (
44
"fmt"
5+
"sync"
56
"time"
67
)
78

@@ -82,6 +83,16 @@ type entraidTokenManager struct {
8283
token *Token
8384
// TokenParser is a function that parses the token.
8485
TokenParser TokenParserFunc
86+
87+
// listener is the single listener for the token manager.
88+
// It is used to receive updates from the token manager.
89+
// The token manager will call the listener's OnTokenNext method with the updated token.
90+
// If an error occurs, the token manager will call the listener's OnTokenError method with the error.
91+
// if listener is set, Start will fail
92+
listener TokenListener
93+
94+
// lock locks the listener to prevent concurrent access.
95+
lock sync.Mutex
8596
}
8697

8798
func (e *entraidTokenManager) GetToken() (*Token, error) {
@@ -90,7 +101,7 @@ func (e *entraidTokenManager) GetToken() (*Token, error) {
90101
return copyToken(e.token), nil
91102
}
92103

93-
rawToken, err := e.idp.requestToken()
104+
rawToken, err := e.idp.RequestToken()
94105
if err != nil {
95106
return nil, fmt.Errorf("failed to request token: %w", err)
96107
}
@@ -123,14 +134,19 @@ type TokenListener interface {
123134
// The token manager will call the listener's OnTokenNext method with the updated token.
124135
// If an error occurs, the token manager will call the listener's OnError method with the error.
125136
func (e *entraidTokenManager) Start(listener TokenListener) (cancelFunc, error) {
126-
// Start the token manager and return a channel that will receive updates.
127-
// This is a placeholder implementation.
137+
e.lock.Lock()
138+
defer e.lock.Unlock()
139+
if e.listener != nil {
140+
return nil, fmt.Errorf("token manager already started")
141+
}
142+
e.listener = listener
143+
128144
token, err := e.GetToken()
129145
if err != nil {
130146
return nil, fmt.Errorf("failed to start token manager: %w", err)
131147
}
132148

133-
listener.OnTokenNext(token)
149+
go listener.OnTokenNext(token)
134150

135151
cancel := func() error {
136152
// Stop the token manager.

0 commit comments

Comments
 (0)