|
| 1 | +package gotado |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + |
| 6 | + "golang.org/x/oauth2" |
| 7 | +) |
| 8 | + |
| 9 | +// TokenRefreshCallback is called whenever a token is automatically refreshed. |
| 10 | +// The callback receives the new token and should persist it to storage. |
| 11 | +// |
| 12 | +// The token passed to the callback contains the essential fields needed for |
| 13 | +// persistence (AccessToken, RefreshToken, TokenType, Expiry, ExpiresIn). |
| 14 | +// Extra fields stored via WithExtra() are intentionally not included to avoid |
| 15 | +// potential race conditions from sharing references to the original token's |
| 16 | +// internal data structures. |
| 17 | +// |
| 18 | +// IMPORTANT: The callback is called synchronously and should return quickly. |
| 19 | +// If heavy processing is needed, consider sending the token to a channel |
| 20 | +// or queue for asynchronous processing. |
| 21 | +type TokenRefreshCallback func(token *oauth2.Token) |
| 22 | + |
| 23 | +// callbackTokenSource wraps an oauth2.TokenSource and calls a callback |
| 24 | +// whenever Token() returns a different token than the previous call. |
| 25 | +// This is useful for persisting refreshed OAuth2 tokens to disk or other storage. |
| 26 | +type callbackTokenSource struct { |
| 27 | + src oauth2.TokenSource |
| 28 | + callback TokenRefreshCallback |
| 29 | + mu sync.Mutex |
| 30 | + lastToken *oauth2.Token |
| 31 | +} |
| 32 | + |
| 33 | +// Token implements the oauth2.TokenSource interface. |
| 34 | +// It retrieves a token from the underlying source and invokes the callback |
| 35 | +// if the token has changed (either access token or refresh token is different). |
| 36 | +func (cts *callbackTokenSource) Token() (*oauth2.Token, error) { |
| 37 | + cts.mu.Lock() |
| 38 | + defer cts.mu.Unlock() |
| 39 | + |
| 40 | + newToken, err := cts.src.Token() |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + |
| 45 | + // Check if token has changed (different access token or refresh token) |
| 46 | + // We check both because: |
| 47 | + // - Access tokens expire frequently (every 10 minutes for tado°) |
| 48 | + // - Refresh tokens may rotate (tado° uses refresh token rotation) |
| 49 | + tokenChanged := false |
| 50 | + if cts.lastToken == nil { |
| 51 | + tokenChanged = true |
| 52 | + } else { |
| 53 | + // Compare access tokens |
| 54 | + if cts.lastToken.AccessToken != newToken.AccessToken { |
| 55 | + tokenChanged = true |
| 56 | + } |
| 57 | + // Compare refresh tokens (important for token rotation) |
| 58 | + if cts.lastToken.RefreshToken != newToken.RefreshToken { |
| 59 | + tokenChanged = true |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Update lastToken if token changed |
| 64 | + if tokenChanged { |
| 65 | + cts.lastToken = copyToken(newToken) |
| 66 | + |
| 67 | + // Invoke callback if provided |
| 68 | + if cts.callback != nil { |
| 69 | + // Make a copy of the token to pass to the callback |
| 70 | + // This prevents the callback from modifying the token |
| 71 | + tokenCopy := copyToken(newToken) |
| 72 | + cts.callback(tokenCopy) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return newToken, nil |
| 77 | +} |
| 78 | + |
| 79 | +// NewCallbackTokenSource creates a TokenSource that invokes the provided |
| 80 | +// callback whenever the underlying token is refreshed. |
| 81 | +// |
| 82 | +// This is particularly useful for the tado° API which: |
| 83 | +// - Has short-lived access tokens (10 minutes) |
| 84 | +// - Uses refresh token rotation (old refresh token is invalidated when new one is issued) |
| 85 | +// - Requires offline_access scope for refresh tokens |
| 86 | +// |
| 87 | +// Example usage: |
| 88 | +// |
| 89 | +// config := gotado.AuthConfig(clientID, "offline_access") |
| 90 | +// token, _ := config.DeviceAccessToken(ctx, deviceAuth) |
| 91 | +// |
| 92 | +// callback := func(newToken *oauth2.Token) { |
| 93 | +// // Save token to encrypted storage |
| 94 | +// log.Println("Token refreshed, saving to disk") |
| 95 | +// SaveTokenToFile(newToken) |
| 96 | +// } |
| 97 | +// |
| 98 | +// tado := gotado.NewWithTokenRefreshCallback(ctx, config, token, callback) |
| 99 | +func NewCallbackTokenSource(src oauth2.TokenSource, callback TokenRefreshCallback) oauth2.TokenSource { |
| 100 | + return &callbackTokenSource{ |
| 101 | + src: src, |
| 102 | + callback: callback, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// copyToken creates a copy of an oauth2.Token. |
| 107 | +// This creates a new token with the same field values as the source token. |
| 108 | +// |
| 109 | +// Note: Extra fields stored via WithExtra() are intentionally not copied. |
| 110 | +// The purpose of the callback is to persist the access and refresh tokens |
| 111 | +// for later use. Extra fields are not needed for token storage and excluding |
| 112 | +// them avoids potential race conditions from sharing references to the |
| 113 | +// original token's internal data structures. |
| 114 | +func copyToken(src *oauth2.Token) *oauth2.Token { |
| 115 | + if src == nil { |
| 116 | + return nil |
| 117 | + } |
| 118 | + |
| 119 | + return &oauth2.Token{ |
| 120 | + AccessToken: src.AccessToken, |
| 121 | + TokenType: src.TokenType, |
| 122 | + RefreshToken: src.RefreshToken, |
| 123 | + Expiry: src.Expiry, |
| 124 | + ExpiresIn: src.ExpiresIn, |
| 125 | + } |
| 126 | +} |
0 commit comments