|
| 1 | +package manager |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/redis-developer/go-redis-entraid/internal" |
| 10 | + "github.com/redis-developer/go-redis-entraid/shared" |
| 11 | + "github.com/redis-developer/go-redis-entraid/token" |
| 12 | +) |
| 13 | + |
| 14 | +// entraidTokenManager is a struct that implements the TokenManager interface. |
| 15 | +type entraidTokenManager struct { |
| 16 | + // idp is the identity provider used to obtain the token. |
| 17 | + idp shared.IdentityProvider |
| 18 | + |
| 19 | + // token is the authentication token for the user which should be kept in memory if valid. |
| 20 | + token *token.Token |
| 21 | + |
| 22 | + // tokenRWLock is a read-write lock used to protect the token from concurrent access. |
| 23 | + tokenRWLock sync.RWMutex |
| 24 | + |
| 25 | + // identityProviderResponseParser is the parser used to parse the response from the identity provider. |
| 26 | + // It`s ParseResponse method will be called to parse the response and return the token. |
| 27 | + identityProviderResponseParser shared.IdentityProviderResponseParser |
| 28 | + |
| 29 | + // retryOptions is a struct that contains the options for retrying the token request. |
| 30 | + // It contains the maximum number of attempts, initial delay, maximum delay, and backoff multiplier. |
| 31 | + // The default values are 3 attempts, 1000 ms initial delay, 10000 ms maximum delay, and 2.0 backoff multiplier. |
| 32 | + // The values can be overridden by the user. |
| 33 | + retryOptions RetryOptions |
| 34 | + |
| 35 | + // listener is the single listener for the token manager. |
| 36 | + // It is used to receive updates from the token manager. |
| 37 | + // The token manager will call the listener's OnNext method with the updated token. |
| 38 | + // If an error occurs, the token manager will call the listener's OnError method with the error. |
| 39 | + // if listener is set, Start will fail |
| 40 | + listener TokenListener |
| 41 | + |
| 42 | + // lock locks the listener to prevent concurrent access. |
| 43 | + lock sync.Mutex |
| 44 | + |
| 45 | + // expirationRefreshRatio is the ratio of the token expiration time to refresh the token. |
| 46 | + // It is used to determine when to refresh the token. |
| 47 | + // The value should be between 0 and 1. |
| 48 | + // For example, if the expiration time is 1 hour and the ratio is 0.75, |
| 49 | + // the token will be refreshed after 45 minutes. (the token is refreshed when 75% of its lifetime has passed) |
| 50 | + expirationRefreshRatio float64 |
| 51 | + |
| 52 | + // lowerBoundDuration is the lower bound for the refresh time in time.Duration. |
| 53 | + lowerBoundDuration time.Duration |
| 54 | + |
| 55 | + // closedChan is a channel that is closedChan when the token manager is closedChan. |
| 56 | + // It is used to signal the token manager to stop requesting tokens. |
| 57 | + closedChan chan struct{} |
| 58 | + |
| 59 | + // context is the context used to request the token from the identity provider. |
| 60 | + ctx context.Context |
| 61 | + |
| 62 | + // ctxCancel is the cancel function for the context. |
| 63 | + ctxCancel context.CancelFunc |
| 64 | + |
| 65 | + // requestTimeout is the timeout for the request to the identity provider. |
| 66 | + requestTimeout time.Duration |
| 67 | +} |
| 68 | + |
| 69 | +func (e *entraidTokenManager) GetToken(forceRefresh bool) (*token.Token, error) { |
| 70 | + e.tokenRWLock.RLock() |
| 71 | + // check if the token is nil and if it is not expired |
| 72 | + t := e.token |
| 73 | + duration := e.durationToRenewal(t) |
| 74 | + if !forceRefresh && t != nil && duration > 0 { |
| 75 | + e.tokenRWLock.RUnlock() |
| 76 | + return t, nil |
| 77 | + } |
| 78 | + e.tokenRWLock.RUnlock() |
| 79 | + |
| 80 | + // start the context early, |
| 81 | + // since at heavy concurrent load |
| 82 | + // locks may take some time to acquire |
| 83 | + ctx, ctxCancel := context.WithTimeout(e.ctx, e.requestTimeout) |
| 84 | + defer ctxCancel() |
| 85 | + |
| 86 | + // Upgrade to write lock for token update |
| 87 | + e.tokenRWLock.Lock() |
| 88 | + defer e.tokenRWLock.Unlock() |
| 89 | + |
| 90 | + // Double-check pattern to avoid unnecessary token refresh |
| 91 | + t = e.token |
| 92 | + duration = e.durationToRenewal(t) |
| 93 | + if !forceRefresh && t != nil && duration > 0 { |
| 94 | + return t, nil |
| 95 | + } |
| 96 | + |
| 97 | + // Request a new token from the identity provider |
| 98 | + idpResult, err := e.idp.RequestToken(ctx) |
| 99 | + if err != nil { |
| 100 | + return nil, fmt.Errorf("failed to request token from idp: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | + t, err = e.identityProviderResponseParser.ParseResponse(idpResult) |
| 104 | + if err != nil { |
| 105 | + return nil, fmt.Errorf("failed to parse token: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + if t == nil { |
| 109 | + return nil, fmt.Errorf("failed to get token: token is nil") |
| 110 | + } |
| 111 | + |
| 112 | + // Store the token |
| 113 | + e.token = t |
| 114 | + // Return the token - no need to copy since it's immutable |
| 115 | + return t, nil |
| 116 | +} |
| 117 | + |
| 118 | +// Start starts the token manager and returns cancelFunc to stop the token manager. |
| 119 | +// It takes a TokenListener as an argument, which is used to receive updates. |
| 120 | +// The token manager will call the listener's OnNext method with the updated token. |
| 121 | +// If an error occurs, the token manager will call the listener's OnError method with the error. |
| 122 | +func (e *entraidTokenManager) Start(listener TokenListener) (StopFunc, error) { |
| 123 | + e.lock.Lock() |
| 124 | + defer e.lock.Unlock() |
| 125 | + if e.listener != nil { |
| 126 | + return nil, ErrTokenManagerAlreadyStarted |
| 127 | + } |
| 128 | + |
| 129 | + if e.closedChan != nil && !internal.IsClosed(e.closedChan) { |
| 130 | + // there is a hanging goroutine that is waiting for the closedChan to be closed |
| 131 | + // if the closedChan is not nil and not closed, close it |
| 132 | + close(e.closedChan) |
| 133 | + } |
| 134 | + |
| 135 | + ctx, ctxCancel := context.WithCancel(context.Background()) |
| 136 | + e.ctx = ctx |
| 137 | + e.ctxCancel = ctxCancel |
| 138 | + |
| 139 | + // make sure there is token in memory before starting the loop |
| 140 | + _, err := e.GetToken(false) |
| 141 | + if err != nil { |
| 142 | + return nil, fmt.Errorf("failed to get token: %w", err) |
| 143 | + } |
| 144 | + |
| 145 | + e.closedChan = make(chan struct{}) |
| 146 | + e.listener = listener |
| 147 | + |
| 148 | + go func(listener TokenListener, closed <-chan struct{}) { |
| 149 | + maxDelay := e.retryOptions.MaxDelay |
| 150 | + initialDelay := e.retryOptions.InitialDelay |
| 151 | + |
| 152 | + for { |
| 153 | + e.tokenRWLock.RLock() |
| 154 | + timeToRenewal := e.durationToRenewal(e.token) |
| 155 | + e.tokenRWLock.RUnlock() |
| 156 | + select { |
| 157 | + case <-closed: |
| 158 | + return |
| 159 | + case <-time.After(timeToRenewal): |
| 160 | + if timeToRenewal == 0 { |
| 161 | + // Token was requested immediately, guard against infinite loop |
| 162 | + select { |
| 163 | + case <-closed: |
| 164 | + return |
| 165 | + case <-time.After(initialDelay): |
| 166 | + // continue to attempt |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + // Token is about to expire, refresh it |
| 171 | + delay := initialDelay |
| 172 | + for i := 0; i < e.retryOptions.MaxAttempts; i++ { |
| 173 | + t, err := e.GetToken(true) |
| 174 | + if err == nil { |
| 175 | + listener.OnNext(t) |
| 176 | + break |
| 177 | + } |
| 178 | + |
| 179 | + // check if err is retriable |
| 180 | + if e.retryOptions.IsRetryable(err) { |
| 181 | + if i == e.retryOptions.MaxAttempts-1 { |
| 182 | + // last attempt, call OnError |
| 183 | + listener.OnError(fmt.Errorf("max attempts reached: %w", err)) |
| 184 | + return |
| 185 | + } |
| 186 | + |
| 187 | + // Exponential backoff |
| 188 | + if delay < maxDelay { |
| 189 | + delay = time.Duration(float64(delay) * e.retryOptions.BackoffMultiplier) |
| 190 | + } |
| 191 | + if delay > maxDelay { |
| 192 | + delay = maxDelay |
| 193 | + } |
| 194 | + |
| 195 | + select { |
| 196 | + case <-closed: |
| 197 | + return |
| 198 | + case <-time.After(delay): |
| 199 | + // continue to next attempt |
| 200 | + } |
| 201 | + } else { |
| 202 | + // not retriable |
| 203 | + listener.OnError(err) |
| 204 | + return |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + }(listener, e.closedChan) |
| 210 | + |
| 211 | + return e.stop, nil |
| 212 | +} |
| 213 | + |
| 214 | +// stop closes the token manager and releases any resources. |
| 215 | +func (e *entraidTokenManager) stop() (err error) { |
| 216 | + e.lock.Lock() |
| 217 | + defer e.lock.Unlock() |
| 218 | + defer func() { |
| 219 | + // recover from panic and return the error |
| 220 | + if r := recover(); r != nil { |
| 221 | + err = fmt.Errorf("failed to stop token manager: %s", r) |
| 222 | + } |
| 223 | + }() |
| 224 | + |
| 225 | + if e.closedChan == nil || e.listener == nil { |
| 226 | + return ErrTokenManagerAlreadyStopped |
| 227 | + } |
| 228 | + |
| 229 | + e.ctxCancel() |
| 230 | + e.listener = nil |
| 231 | + close(e.closedChan) |
| 232 | + |
| 233 | + return nil |
| 234 | +} |
| 235 | + |
| 236 | +// durationToRenewal calculates the duration to the next token renewal. |
| 237 | +// It returns the duration to the next token renewal based on the expiration refresh ratio and the lower bound duration. |
| 238 | +// If the token is nil, it returns 0. |
| 239 | +// If the time till expiration is less than the lower bound duration, it returns 0 to renew the token now. |
| 240 | +func (e *entraidTokenManager) durationToRenewal(t *token.Token) time.Duration { |
| 241 | + if t == nil { |
| 242 | + return 0 |
| 243 | + } |
| 244 | + expirationRefreshTime := t.ReceivedAt().Add(time.Duration(float64(t.TTL()) * float64(time.Second) * e.expirationRefreshRatio)) |
| 245 | + timeTillExpiration := time.Until(t.ExpirationOn()) |
| 246 | + now := time.Now().UTC() |
| 247 | + |
| 248 | + if expirationRefreshTime.Before(now) { |
| 249 | + return 0 |
| 250 | + } |
| 251 | + |
| 252 | + // if the timeTillExpiration is less than the lower bound (or 0), return 0 to renew the token NOW |
| 253 | + if timeTillExpiration <= e.lowerBoundDuration || timeTillExpiration <= 0 { |
| 254 | + return 0 |
| 255 | + } |
| 256 | + |
| 257 | + // Calculate the time to renew the token based on the expiration refresh ratio |
| 258 | + duration := time.Until(expirationRefreshTime) |
| 259 | + |
| 260 | + // if the duration will take us past the lower bound, return the duration to lower bound |
| 261 | + if timeTillExpiration-e.lowerBoundDuration < duration { |
| 262 | + return timeTillExpiration - e.lowerBoundDuration |
| 263 | + } |
| 264 | + |
| 265 | + // return the calculated duration |
| 266 | + return duration |
| 267 | +} |
0 commit comments