Files
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

110 lines
2.5 KiB
Go

package auth
import (
"context"
"fmt"
"time"
"github.com/pkg/browser"
"github.com/insights/insights/livestream/tui/config"
)
type Options struct {
Token string
Host string
LivestreamHost string
}
func Authenticate(opts Options) (*config.Credentials, error) {
host := opts.Host
if host == "" {
host = "https://app.hanzo.ai"
}
// Priority 1: direct token flag
if opts.Token != "" {
livestreamHost := opts.LivestreamHost
if livestreamHost == "" {
livestreamHost = DeriveStreamHost(host)
}
return &config.Credentials{
Host: host,
LivestreamHost: livestreamHost,
Token: opts.Token,
ExpiresAt: time.Now().Add(7 * 24 * time.Hour),
}, nil
}
// Priority 2: cached credentials
cfg := config.Load()
if cfg.Credentials != nil && !cfg.Credentials.IsExpired() {
creds := cfg.Credentials
if opts.LivestreamHost != "" {
creds.LivestreamHost = opts.LivestreamHost
}
if opts.Host != "" {
creds.Host = opts.Host
}
return creds, nil
}
// Priority 3: browser flow
return browserFlow(host, opts.LivestreamHost)
}
func browserFlow(host, livestreamHostOverride string) (*config.Credentials, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
port, resultCh, err := StartCallbackServer(ctx)
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/cli/live?port=%d", host, port)
fmt.Printf("Opening browser to authorize: %s\n", url)
if err := browser.OpenURL(url); err != nil {
fmt.Printf("Could not open browser automatically.\nPlease open this URL manually:\n %s\n", url)
}
fmt.Println("Waiting for authorization...")
select {
case result := <-resultCh:
if result.Err != nil {
return nil, result.Err
}
// Use the actual host the browser landed on (us/eu) to derive livestream host
actualHost := result.APIHost
if actualHost == "" {
actualHost = host
}
livestreamHost := livestreamHostOverride
if livestreamHost == "" {
livestreamHost = DeriveStreamHost(actualHost)
}
creds := &config.Credentials{
Host: actualHost,
LivestreamHost: livestreamHost,
Token: result.Token,
TeamID: result.TeamID,
TeamName: result.TeamName,
ExpiresAt: time.Now().Add(7 * 24 * time.Hour),
}
cfg := config.Load()
cfg.Credentials = creds
if err := config.Save(cfg); err != nil {
fmt.Printf("Warning: could not save credentials: %v\n", err)
}
return creds, nil
case <-ctx.Done():
return nil, fmt.Errorf("authorization timed out")
}
}