@@ -2,6 +2,7 @@ package entraid
2
2
3
3
import (
4
4
"fmt"
5
+ "sync"
5
6
"time"
6
7
)
7
8
@@ -82,6 +83,16 @@ type entraidTokenManager struct {
82
83
token * Token
83
84
// TokenParser is a function that parses the token.
84
85
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
85
96
}
86
97
87
98
func (e * entraidTokenManager ) GetToken () (* Token , error ) {
@@ -90,7 +101,7 @@ func (e *entraidTokenManager) GetToken() (*Token, error) {
90
101
return copyToken (e .token ), nil
91
102
}
92
103
93
- rawToken , err := e .idp .requestToken ()
104
+ rawToken , err := e .idp .RequestToken ()
94
105
if err != nil {
95
106
return nil , fmt .Errorf ("failed to request token: %w" , err )
96
107
}
@@ -123,14 +134,19 @@ type TokenListener interface {
123
134
// The token manager will call the listener's OnTokenNext method with the updated token.
124
135
// If an error occurs, the token manager will call the listener's OnError method with the error.
125
136
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
+
128
144
token , err := e .GetToken ()
129
145
if err != nil {
130
146
return nil , fmt .Errorf ("failed to start token manager: %w" , err )
131
147
}
132
148
133
- listener .OnTokenNext (token )
149
+ go listener .OnTokenNext (token )
134
150
135
151
cancel := func () error {
136
152
// Stop the token manager.
0 commit comments