package qobuz import ( "context" "errors" "fmt" "net" "net/http" "net/url" "sync/atomic" "time" "github.com/bjarneo/cliamp/applog" "github.com/bjarneo/cliamp/internal/browser" ) // authURLObserver is invoked with the OAuth URL when interactive auth begins. // Used by the TUI to display the URL when the launched browser does not reach // the user (containers, headless environments). var authURLObserver atomic.Pointer[func(string)] // SetAuthURLObserver registers a callback invoked once with the OAuth URL at // the start of an interactive sign-in. Pass nil to remove. func SetAuthURLObserver(fn func(string)) { if fn == nil { authURLObserver.Store(nil) return } authURLObserver.Store(&fn) } func notifyAuthURL(u string) { applog.Info("qobuz: sign-in URL: %s", u) if p := authURLObserver.Load(); p != nil { (*p)(u) } } // oauthResult holds the data captured from a Qobuz OAuth redirect. type oauthResult struct { Token string UserID string Code string } // newClientSilent builds an authenticated client from stored credentials only. // It never opens a browser; if no usable credentials exist it returns an error. func newClientSilent(ctx context.Context) (*client, error) { creds, err := loadCreds() if err != nil { return nil, fmt.Errorf("qobuz: no stored credentials: %w", err) } if creds.AppID == "" || creds.UserAuthToken == "" { return nil, fmt.Errorf("qobuz: incomplete stored credentials") } c := newClient(creds.AppID, creds.Secrets) c.secret = creds.Secret c.uat = creds.UserAuthToken c.userID = creds.UserID c.label = creds.Label if err := c.authWithToken(ctx, creds.UserID, creds.UserAuthToken); err != nil { return nil, fmt.Errorf("qobuz: stored token rejected: %w", err) } if c.secret == "" { if err := c.validateSecret(ctx); err != nil { return nil, err } } // Re-persist in case the validated secret or label changed. _ = saveCreds(credsFromClient(c, creds.PrivateKey)) return c, nil } // newClientInteractive scrapes fresh credentials from the Qobuz web player and // runs the interactive OAuth browser flow, persisting the result on success. func newClientInteractive(ctx context.Context) (*client, error) { appID, secrets, privateKey, err := scrapeCredentials(ctx) if err != nil { return nil, fmt.Errorf("qobuz: scrape credentials: %w", err) } c := newClient(appID, secrets) // OAuth first: the secret-validation probe (track/getFileUrl) is an // authenticated endpoint and fails without a user_auth_token, so the // browser sign-in must complete before validateSecret runs. result, err := captureOAuthRedirect(ctx, appID) if err != nil { return nil, err } if err := c.loginWithOAuth(ctx, result, privateKey); err != nil { return nil, fmt.Errorf("qobuz: OAuth login: %w", err) } if err := c.validateSecret(ctx); err != nil { return nil, err } if err := saveCreds(credsFromClient(c, privateKey)); err != nil { applog.UserError("qobuz: failed to save credentials: %v", err) } return c, nil } func credsFromClient(c *client, privateKey string) *storedCreds { return &storedCreds{ AppID: c.appID, Secrets: c.secrets, Secret: c.secret, PrivateKey: privateKey, UserAuthToken: c.uat, UserID: c.userID, Label: c.label, } } // oauthCallbackHTML is shown in the browser once the redirect is captured. const oauthCallbackHTML = `
You can close this tab now.