diff --git a/internal/http/client.go b/internal/http/client.go index db491470..c3a8a862 100644 --- a/internal/http/client.go +++ b/internal/http/client.go @@ -20,6 +20,8 @@ import ( "golang.org/x/sync/errgroup" "golang.org/x/sync/semaphore" "gopkg.in/ini.v1" + "golang.org/x/oauth2" + "golang.org/x/oauth2/github ) // OVH API client @@ -34,6 +36,38 @@ func InitClientWithProfile(cfg *ini.File, profileOverride string) { // Init API client if runtime.GOARCH == "wasm" && runtime.GOOS == "js" { + +// Client wraps the OAuth2 client with token persistence +func (RefreshableClient)(ctx context.Context, token *oauth2.Token) *http.Client { + // TokenSource automatically refreshes expired tokens + tokenSource := oauthConfig.TokenSource(ctx, token) + savingSource := &SavingTokenSource{ + source: tokenSource, + store: tokenStore, + } + + return oauth2.NewClient(ctx, savingSource) +} + +// SavingTokenSource saves the token whenever it gets refreshed +type SavingTokenSource struct { + source oauth2.TokenSource + store *TokenStore +} + +func (s *SavingTokenSource) Token() (*oauth2.Token, error) { + token, err := s.source.Token() + if err != nil { + return nil, err + } + + // Save the potentially refreshed token + if err := s.store.Save(token); err != nil { + log.Printf("Warning: failed to save token: %v", err) + } + + return token, nil +} // In WASM mode, we use an unauthenticated client Client = &ovh.Client{ Client: &http.Client{},