Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions internal/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{},
Expand Down