From b9dc3f0ec13d0b2d231e759e24e6d1bdf6dab0e4 Mon Sep 17 00:00:00 2001 From: dhkatz Date: Thu, 29 Oct 2026 03:45:52 -0700 Subject: [PATCH] Hub: accounts, IMVU linking by tagline, proxy sign-in, presence, console People sign up on the website and link IMVU accounts by putting a code in the account's tagline, which the hub reads from IMVU's public API (the most recent proof wins; link caps per person). IMVU's age verification or Access Pass make an account adult. T5DE's proxy signs in with a device code and keeps a WebSocket open, reporting the IMVU account in use and the community products it wears; /online shows those who opted in. Moderators get a console: who's online, people, link caps, bans, roles, and the audit log. Tests run the website and API against an embedded Postgres and a fake IMVU API. Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/ci.yml | 37 ++ .gitignore | 2 + CLAUDE.md | 10 + README.md | 33 +- cmd/t5de-hub/main.go | 106 +++++ docs/design.md | 11 +- go.mod | 21 + go.sum | 40 ++ internal/imvu/imvu.go | 144 ++++++ internal/imvu/imvu_test.go | 30 ++ internal/realtime/realtime.go | 229 +++++++++ internal/store/migrations/001_accounts.sql | 78 +++ internal/store/store.go | 529 +++++++++++++++++++++ internal/token/token.go | 117 +++++ internal/token/token_test.go | 44 ++ internal/web/account.go | 278 +++++++++++ internal/web/api.go | 216 +++++++++ internal/web/auth.go | 172 +++++++ internal/web/console.go | 141 ++++++ internal/web/render.go | 82 ++++ internal/web/server.go | 267 +++++++++++ internal/web/static/favicon.svg | 1 + internal/web/static/htmx.min.js | 1 + internal/web/static/style.css | 173 +++++++ internal/web/templates/account.html | 82 ++++ internal/web/templates/console.html | 45 ++ internal/web/templates/console_people.html | 25 + internal/web/templates/console_person.html | 61 +++ internal/web/templates/device.html | 20 + internal/web/templates/error.html | 8 + internal/web/templates/home.html | 33 ++ internal/web/templates/layout.html | 40 ++ internal/web/templates/online.html | 27 ++ internal/web/templates/signin.html | 19 + internal/web/templates/signup.html | 22 + internal/web/web_test.go | 409 ++++++++++++++++ 36 files changed, 3548 insertions(+), 5 deletions(-) create mode 100644 .gitea/workflows/ci.yml create mode 100644 .gitignore create mode 100644 cmd/t5de-hub/main.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/imvu/imvu.go create mode 100644 internal/imvu/imvu_test.go create mode 100644 internal/realtime/realtime.go create mode 100644 internal/store/migrations/001_accounts.sql create mode 100644 internal/store/store.go create mode 100644 internal/token/token.go create mode 100644 internal/token/token_test.go create mode 100644 internal/web/account.go create mode 100644 internal/web/api.go create mode 100644 internal/web/auth.go create mode 100644 internal/web/console.go create mode 100644 internal/web/render.go create mode 100644 internal/web/server.go create mode 100644 internal/web/static/favicon.svg create mode 100644 internal/web/static/htmx.min.js create mode 100644 internal/web/static/style.css create mode 100644 internal/web/templates/account.html create mode 100644 internal/web/templates/console.html create mode 100644 internal/web/templates/console_people.html create mode 100644 internal/web/templates/console_person.html create mode 100644 internal/web/templates/device.html create mode 100644 internal/web/templates/error.html create mode 100644 internal/web/templates/home.html create mode 100644 internal/web/templates/layout.html create mode 100644 internal/web/templates/online.html create mode 100644 internal/web/templates/signin.html create mode 100644 internal/web/templates/signup.html create mode 100644 internal/web/web_test.go diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..206f477 --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,37 @@ +# Tests the hub (against a real Postgres: embedded, downloaded by the tests and cached) and builds it. +name: CI + +on: + push: + branches: [main] + pull_request: + workflow_dispatch: + +jobs: + test: + runs-on: ubuntu-latest + env: + GOPRIVATE: git.t5de.dev + T5DE_PG_CACHE: /tmp/t5de-hub-postgres + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-go@v5 + with: + go-version-file: go.mod + + - name: Cache Postgres for the tests + uses: actions/cache@v4 + with: + path: /tmp/t5de-hub-postgres + key: embedded-postgres-${{ runner.os }}-${{ hashFiles('go.sum') }} + + - name: Test + run: | + unformatted=$(gofmt -l .) + if [ -n "$unformatted" ]; then echo "Not gofmt-formatted: $unformatted"; exit 1; fi + go vet ./... + go test ./... + + - name: Build + run: CGO_ENABLED=0 go build -trimpath -o dist/t5de-hub ./cmd/t5de-hub diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd42db8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/dist/ +/t5de-hub-data/ diff --git a/CLAUDE.md b/CLAUDE.md index d6ed46b..353e108 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -11,3 +11,13 @@ beside this one: `t5de` (patches), `t5de-proxy` (local proxy), later `t5de-catal - Commits: author "dhkatz "; never the user's real name or email. - The user prioritizes backend performance (Go), and wants moderation centered on the hub's website. - IMVU session keys never leave the user's machine; the hub never asks for them. + +## Notes + +- Go, net/http, html/template (server-rendered; htmx 2.0.4 vendored in `internal/web/static`, CSP without inline + scripts or styles), pgx with plain SQL (`internal/store`, migrations embedded), coder/websocket. +- Dev: `go run ./cmd/t5de-hub -dev` (embedded Postgres on port 54321). Tests use port 54339 and a database each. +- The look: IMVU Classic's dialog palette (dark grays, gold buttons with black text), like T5DE's setup. Copy is + short and plain. +- Calls to IMVU (`internal/imvu`, its public user API) are rare and rate-limited: linking and age re-checks only. + diff --git a/README.md b/README.md index 75323bc..3dd8f67 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,33 @@ # t5de-hub -T5DE's hub: accounts linked to IMVU accounts, the registry of community products and catalogs, presence, T5DE-hosted -rooms, and the website with the moderation console. Part of T5DE (git.t5de.dev/t5de). +T5DE's hub: accounts linked to IMVU accounts, presence, and (to come) the registry of community products and +catalogs, T5DE-hosted rooms, and the moderation console. Part of T5DE (git.t5de.dev/t5de). Design: +[docs/design.md](docs/design.md); what a room needs from a server: [docs/imq-rooms.md](docs/imq-rooms.md). -Design: [docs/design.md](docs/design.md) (draft). No code yet. +## What works + +- **Accounts**: sign-up and sign-in (Argon2id passwords), sessions with CSRF protection. The first account is an + admin. +- **Linking IMVU accounts**: the person puts a code in the account's tagline; the hub reads it from IMVU's public + API. The most recent proof wins (an account linked elsewhere moves). Link caps per person (default 10). +- **Age**: an account counts as adult if IMVU says it's age verified or has Access Pass, checked by the hub. +- **Proxy sign-in**: OAuth 2.0 device codes (`t5de-proxy signin` shows a code to enter at `/device`); Ed25519 JWT + access tokens (keys at `/.well-known/jwks.json`) and rotating refresh tokens. +- **Presence**: signed-in proxies keep a WebSocket (`/api/v1/realtime`) telling the hub which IMVU account is in + use and the community products it wears. `/online` shows accounts that opted in, with the opt-in T5DE badge. +- **Console** (moderators): counts, who's online (live), people search, link caps, bans, roles, audit log. + +## Running it + + go run ./cmd/t5de-hub -dev + +runs the hub at http://127.0.0.1:8080 with its own Postgres (downloaded once, kept in `t5de-hub-data/`). In +production: `t5de-hub -db postgres://... -public-url https://hub.t5de.dev -listen :8080 -data /var/lib/t5de-hub` +(the data directory holds the token signing key). + +To point a T5DE proxy at it: `t5de-proxy signin -hub http://127.0.0.1:8080`. + +## Tests + +`go test ./...` runs everything, the website and API against a real Postgres (embedded, downloaded on first run +and cached; `T5DE_PG_CACHE` sets where) and a fake IMVU API. diff --git a/cmd/t5de-hub/main.go b/cmd/t5de-hub/main.go new file mode 100644 index 0000000..49a9892 --- /dev/null +++ b/cmd/t5de-hub/main.go @@ -0,0 +1,106 @@ +// Command t5de-hub runs T5DE's hub: the website, the moderation console, and the API proxies sign in to. +// +// t5de-hub -db postgres://... -public-url https://hub.t5de.dev [-listen :8080] [-data dir] +// t5de-hub -dev runs its own Postgres in /postgres (for development) +package main + +import ( + "context" + "errors" + "flag" + "fmt" + "log" + "net/http" + "os" + "os/signal" + "path/filepath" + "strings" + "time" + + embeddedpostgres "github.com/fergusstrange/embedded-postgres" + + "git.t5de.dev/t5de/t5de-hub/internal/imvu" + "git.t5de.dev/t5de/t5de-hub/internal/realtime" + "git.t5de.dev/t5de/t5de-hub/internal/store" + "git.t5de.dev/t5de/t5de-hub/internal/token" + "git.t5de.dev/t5de/t5de-hub/internal/web" +) + +func main() { + listen := flag.String("listen", "127.0.0.1:8080", "address to listen on") + dbURL := flag.String("db", os.Getenv("T5DE_HUB_DB"), "Postgres URL (or $T5DE_HUB_DB)") + publicURL := flag.String("public-url", "", "the hub's address as people reach it (default: http://)") + data := flag.String("data", "t5de-hub-data", "where the hub keeps its signing key (and, with -dev, its database)") + dev := flag.Bool("dev", false, "run a Postgres of its own in /postgres, for development") + flag.Parse() + logger := log.New(os.Stderr, "", log.LstdFlags) + if err := run(logger, *listen, *dbURL, *publicURL, *data, *dev); err != nil { + logger.Fatal(err) + } +} + +func run(logger *log.Logger, listen, dbURL, publicURL, data string, dev bool) error { + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() + if publicURL == "" { + publicURL = "http://" + strings.Replace(listen, "0.0.0.0", "localhost", 1) + } + publicURL = strings.TrimSuffix(publicURL, "/") + if dev { + pg, url, err := startDevPostgres(data) + if err != nil { + return fmt.Errorf("starting Postgres: %w", err) + } + defer pg.Stop() + dbURL = url + } + if dbURL == "" { + return errors.New("no database: give -db (or $T5DE_HUB_DB), or -dev") + } + st, err := store.Open(ctx, dbURL) + if err != nil { + return err + } + defer st.Close() + keys, err := token.LoadOrCreate(filepath.Join(data, "signing.key"), publicURL) + if err != nil { + return err + } + server := &web.Server{Store: st, IMVU: imvu.New(), Keys: keys, PublicURL: publicURL, Log: logger} + server.Realtime = realtime.New(web.Owners{Store: st}, logger) + handler, err := server.Handler() + if err != nil { + return err + } + srv := &http.Server{Addr: listen, Handler: handler, ReadHeaderTimeout: 10 * time.Second} + go func() { + <-ctx.Done() + shutdown, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + srv.Shutdown(shutdown) + }() + logger.Printf("t5de-hub listening on %s (%s)", listen, publicURL) + if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { + return err + } + return nil +} + +// startDevPostgres runs Postgres from data/postgres (downloaded once into data/postgres-cache). +func startDevPostgres(data string) (*embeddedpostgres.EmbeddedPostgres, string, error) { + abs, err := filepath.Abs(data) + if err != nil { + return nil, "", err + } + const port = 54321 + pg := embeddedpostgres.NewDatabase(embeddedpostgres.DefaultConfig(). + Port(port).Database("t5de_hub"). + DataPath(filepath.Join(abs, "postgres", "data")). + RuntimePath(filepath.Join(abs, "postgres", "runtime")). + CachePath(filepath.Join(abs, "postgres-cache")). + StartTimeout(time.Minute)) + if err := pg.Start(); err != nil { + return nil, "", err + } + return pg, fmt.Sprintf("postgres://postgres:postgres@localhost:%d/t5de_hub?sslmode=disable", port), nil +} diff --git a/docs/design.md b/docs/design.md index 74ada39..dad62e2 100644 --- a/docs/design.md +++ b/docs/design.md @@ -279,8 +279,8 @@ One Go program in modules (split into services only if load requires it): - **Moderation**: reports, actions, blocklist, audit log. - **Website and console** (**Decided**: in this repo): browse catalogs, products, creators, rooms, news; manage accounts (links, privacy, data export and deletion); catalog operators' screens; moderation console. - **Decided**: server-rendered. Proposed: templ (Go templates) and htmx, built into the program: fast, cacheable at - Cloudflare, indexable, no JS build. How it looks comes from its CSS (a small design system of our own), not from + **Decided**: server-rendered. Built with Go's html/template (no code generation step) and htmx, embedded in the + program: fast, cacheable at Cloudflare, indexable, no JS build. How it looks comes from its CSS (a small design system of our own), not from rendering on the server; small scripts where a page needs richer interaction (image galleries, drag and drop). ### Data (sketch) @@ -333,6 +333,13 @@ The proxy streams and adds no waiting. - Catalogs see addresses of those fetching adult files (with a token) and of uploaders. - All stated in the setup and on the website. +## Status + +- Phase 1 (local packs) done: t5de-api, t5de-proxy `internal/products`. +- Phase 2 (hub identity) started: accounts, tagline linking, age, link caps, device sign-in, presence and badge, + console. Not yet: periodic age re-checks (only on demand), a sign-in UI in the client (the proxy's `signin` + command for now), deployment. + ## Phases 1. Spec (`t5de-api`); local product packs, removing community items from what IMVU sees, creator live preview. No diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..97f329e --- /dev/null +++ b/go.mod @@ -0,0 +1,21 @@ +module git.t5de.dev/t5de/t5de-hub + +go 1.27.0 + +require ( + github.com/coder/websocket v1.8.15 + github.com/fergusstrange/embedded-postgres v1.34.0 + github.com/jackc/pgx/v5 v5.11.0 + golang.org/x/crypto v0.58.0 +) + +require ( + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + github.com/lib/pq v1.10.9 // indirect + github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect + golang.org/x/sync v0.24.0 // indirect + golang.org/x/sys v0.49.0 // indirect + golang.org/x/text v0.43.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..eb7620a --- /dev/null +++ b/go.sum @@ -0,0 +1,40 @@ +github.com/coder/websocket v1.8.15 h1:6B2JPeOGlpff2Uz6vOEH1Vzpi0iUz20A+lPVhPHtNUA= +github.com/coder/websocket v1.8.15/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fergusstrange/embedded-postgres v1.34.0 h1:c6RKhPKFsLVU+Tdxsx8q0UxCHsvZZ/iShAnljRBXs6s= +github.com/fergusstrange/embedded-postgres v1.34.0/go.mod h1:w0YvnCgf19o6tskInrOOACtnqfVlOvluz3hlNLY7tRk= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg= +github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo= +github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54= +golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w= +golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA= +golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= +golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc= +golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/imvu/imvu.go b/internal/imvu/imvu.go new file mode 100644 index 0000000..c615dab --- /dev/null +++ b/internal/imvu/imvu.go @@ -0,0 +1,144 @@ +// Package imvu reads IMVU's public user API: who an account is, its tagline (for linking) and whether it's age +// verified or has Access Pass. The hub calls it rarely (linking, and age checks at most daily) and never depends on +// it for anything else. +package imvu + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "net/url" + "strconv" + "strings" + "sync" + "time" +) + +// User is an IMVU account, as the public API shows it. +type User struct { + ID int64 + Username string + Tagline string + AgeVerified bool + AccessPass bool +} + +// ErrNoSuchUser means IMVU has no account by that name or ID. +var ErrNoSuchUser = errors.New("no such IMVU account") + +// Client reads IMVU's API. +type Client struct { + // Base is the API's address; tests point it elsewhere. + Base string + HTTP *http.Client + + mu sync.Mutex + last time.Time +} + +// New is a client for api.imvu.com. +func New() *Client { + return &Client{Base: "https://api.imvu.com", HTTP: &http.Client{Timeout: 15 * time.Second}} +} + +// minInterval spaces out calls to IMVU. +const minInterval = 200 * time.Millisecond + +func (c *Client) wait(ctx context.Context) error { + c.mu.Lock() + next := c.last.Add(minInterval) + now := time.Now() + if next.Before(now) { + next = now + } + c.last = next + c.mu.Unlock() + select { + case <-time.After(time.Until(next)): + return nil + case <-ctx.Done(): + return ctx.Err() + } +} + +// ByUsername looks an account up by name. +func (c *Client) ByUsername(ctx context.Context, username string) (*User, error) { + username = strings.TrimSpace(username) + if username == "" || len(username) > 64 { + return nil, ErrNoSuchUser + } + return c.get(ctx, "/user?username="+url.QueryEscape(username)) +} + +// ByID reads an account (fresh, for checks). +func (c *Client) ByID(ctx context.Context, id int64) (*User, error) { + return c.get(ctx, "/user/user-"+strconv.FormatInt(id, 10)) +} + +func (c *Client) get(ctx context.Context, path string) (*User, error) { + if err := c.wait(ctx); err != nil { + return nil, err + } + req, err := http.NewRequestWithContext(ctx, "GET", c.Base+path, nil) + if err != nil { + return nil, err + } + req.Header.Set("User-Agent", "t5de-hub (https://git.t5de.dev/t5de/t5de-hub)") + resp, err := c.HTTP.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) + if err != nil { + return nil, err + } + if resp.StatusCode == http.StatusNotFound { + return nil, ErrNoSuchUser + } + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("IMVU answered %d", resp.StatusCode) + } + return parse(body) +} + +// parse reads IMVU's "denormalized" answer: the user is the entry the answer's id points to, or the only user in it. +func parse(body []byte) (*User, error) { + var answer struct { + Status string `json:"status"` + ID string `json:"id"` + Denormalized map[string]struct { + Data json.RawMessage `json:"data"` + } `json:"denormalized"` + } + if err := json.Unmarshal(body, &answer); err != nil { + return nil, fmt.Errorf("IMVU's answer: %w", err) + } + var found []json.RawMessage + for key, entry := range answer.Denormalized { + if strings.Contains(key, "/user/user-") { + found = append(found, entry.Data) + } + } + if len(found) != 1 { + return nil, ErrNoSuchUser + } + var data struct { + LegacyCID json.Number `json:"legacy_cid"` + Username string `json:"username"` + Tagline string `json:"tagline"` + AgeVerified bool `json:"is_ageverified"` + AP bool `json:"is_ap"` + } + if err := json.Unmarshal(found[0], &data); err != nil { + return nil, fmt.Errorf("IMVU's user: %w", err) + } + id, err := data.LegacyCID.Int64() + if err != nil || id <= 0 || data.Username == "" { + return nil, ErrNoSuchUser + } + return &User{ID: id, Username: data.Username, Tagline: data.Tagline, AgeVerified: data.AgeVerified, AccessPass: data.AP}, nil +} diff --git a/internal/imvu/imvu_test.go b/internal/imvu/imvu_test.go new file mode 100644 index 0000000..5990cd5 --- /dev/null +++ b/internal/imvu/imvu_test.go @@ -0,0 +1,30 @@ +package imvu + +import ( + "context" + "errors" + "net/http" + "net/http/httptest" + "testing" +) + +func TestByUsername(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Query().Get("username") == "Nobody" { + w.Write([]byte(`{"status":"success","id":"x","denormalized":{}}`)) + return + } + w.Write([]byte(`{"status":"success","id":"https://api.imvu.com/user?username=Someone","denormalized":{ + "https://api.imvu.com/user/user-42":{"data":{"legacy_cid":42,"username":"Someone","tagline":"hi t5de-ABCD", + "is_ageverified":true,"is_ap":false}}}}`)) + })) + defer srv.Close() + c := &Client{Base: srv.URL, HTTP: srv.Client()} + u, err := c.ByUsername(context.Background(), "Someone") + if err != nil || u.ID != 42 || u.Username != "Someone" || u.Tagline != "hi t5de-ABCD" || !u.AgeVerified || u.AccessPass { + t.Fatalf("%+v %v", u, err) + } + if _, err := c.ByUsername(context.Background(), "Nobody"); !errors.Is(err, ErrNoSuchUser) { + t.Fatalf("got %v", err) + } +} diff --git a/internal/realtime/realtime.go b/internal/realtime/realtime.go new file mode 100644 index 0000000..04644d2 --- /dev/null +++ b/internal/realtime/realtime.go @@ -0,0 +1,229 @@ +// Package realtime is the hub's live connection with signed-in proxies: which IMVU account each client is using +// (presence), and the community products it's wearing. Later, room channels. +// +// Protocol (JSON text messages over a WebSocket at /api/v1/realtime, authorized with the proxy's access token): +// +// proxy -> hub {"type": "session", "imvu_id": 123} the client signed in as this IMVU account +// proxy -> hub {"type": "outfit", "items": [{"id", "name"}]} the community products it wears now +// hub -> proxy {"type": "welcome"} | {"type": "error", "message": "..."} +package realtime + +import ( + "context" + "encoding/json" + "errors" + "log" + "net/http" + "sort" + "sync" + "time" + + "github.com/coder/websocket" +) + +// Item is a community product someone wears. +type Item struct { + ID string `json:"id"` + Name string `json:"name"` +} + +// Online is an IMVU account in use in a T5DE client now. +type Online struct { + PersonID int64 + IMVUID int64 + Since time.Time + Outfit []Item +} + +// Owners tells the hub which person an IMVU account is linked to. +type Owners interface { + LinkOwner(ctx context.Context, imvuID int64) (personID int64, ok bool, err error) +} + +// Hub holds the connected proxies. +type Hub struct { + owners Owners + log *log.Logger + + mu sync.Mutex + sessions map[*session]bool + changed chan struct{} // closed and replaced on every change, for live views +} + +type session struct { + personID int64 + conn *websocket.Conn + imvuID int64 + since time.Time + outfit []Item +} + +func New(owners Owners, logger *log.Logger) *Hub { + return &Hub{owners: owners, log: logger, sessions: map[*session]bool{}, changed: make(chan struct{})} +} + +// Changed is closed the next time anyone's presence or outfit changes. +func (h *Hub) Changed() <-chan struct{} { + h.mu.Lock() + defer h.mu.Unlock() + return h.changed +} + +func (h *Hub) notifyLocked() { + close(h.changed) + h.changed = make(chan struct{}) +} + +// Online lists the IMVU accounts in use now, one entry each (the most recent session), by IMVU ID. +func (h *Hub) Online() []Online { + h.mu.Lock() + defer h.mu.Unlock() + byID := map[int64]Online{} + for s := range h.sessions { + if s.imvuID == 0 { + continue + } + if prev, ok := byID[s.imvuID]; ok && prev.Since.After(s.since) { + continue + } + byID[s.imvuID] = Online{PersonID: s.personID, IMVUID: s.imvuID, Since: s.since, Outfit: append([]Item(nil), s.outfit...)} + } + out := make([]Online, 0, len(byID)) + for _, o := range byID { + out = append(out, o) + } + sort.Slice(out, func(i, j int) bool { return out[i].Since.Before(out[j].Since) }) + return out +} + +// Connections counts connected proxies (signed in or not to an IMVU account yet). +func (h *Hub) Connections() int { + h.mu.Lock() + defer h.mu.Unlock() + return len(h.sessions) +} + +// Disconnect closes a person's connections (when they're banned or sign their proxies out). +func (h *Hub) Disconnect(personID int64) { + h.mu.Lock() + var conns []*websocket.Conn + for s := range h.sessions { + if s.personID == personID { + conns = append(conns, s.conn) + } + } + h.mu.Unlock() + for _, c := range conns { + c.Close(websocket.StatusPolicyViolation, "signed out") + } +} + +type message struct { + Type string `json:"type"` + IMVUID int64 `json:"imvu_id,omitempty"` + Items []Item `json:"items,omitempty"` + Message string `json:"message,omitempty"` +} + +// Serve runs one proxy's connection, already authorized as personID, until it closes. +func (h *Hub) Serve(w http.ResponseWriter, r *http.Request, personID int64) { + conn, err := websocket.Accept(w, r, nil) + if err != nil { + return + } + conn.SetReadLimit(64 << 10) + s := &session{personID: personID, conn: conn, since: time.Now()} + h.mu.Lock() + h.sessions[s] = true + h.mu.Unlock() + defer func() { + h.mu.Lock() + delete(h.sessions, s) + if s.imvuID != 0 { + h.notifyLocked() + } + h.mu.Unlock() + conn.CloseNow() + }() + + ctx := r.Context() + send := func(m message) error { + b, _ := json.Marshal(m) + wctx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + return conn.Write(wctx, websocket.MessageText, b) + } + if send(message{Type: "welcome"}) != nil { + return + } + // Keep the connection alive through proxies and NATs. + go func() { + t := time.NewTicker(30 * time.Second) + defer t.Stop() + for { + select { + case <-ctx.Done(): + return + case <-t.C: + pctx, cancel := context.WithTimeout(ctx, 10*time.Second) + err := conn.Ping(pctx) + cancel() + if err != nil { + conn.CloseNow() + return + } + } + } + }() + for { + _, data, err := conn.Read(ctx) + if err != nil { + return + } + var m message + if json.Unmarshal(data, &m) != nil { + send(message{Type: "error", Message: "not a message"}) + continue + } + switch m.Type { + case "session": + owner, ok, err := h.owners.LinkOwner(ctx, m.IMVUID) + if err != nil { + send(message{Type: "error", Message: "try again later"}) + continue + } + if !ok || owner != personID { + // Not this person's account: no presence, no outfit for it. + h.set(s, func() { s.imvuID, s.outfit = 0, nil }) + send(message{Type: "error", Message: "not_linked"}) + continue + } + h.set(s, func() { s.imvuID, s.since, s.outfit = m.IMVUID, time.Now(), nil }) + case "outfit": + if len(m.Items) > 64 { + send(message{Type: "error", Message: "too many items"}) + continue + } + items := make([]Item, 0, len(m.Items)) + for _, it := range m.Items { + if it.ID == "" || len(it.ID) > 128 || len(it.Name) > 128 { + continue + } + items = append(items, it) + } + h.set(s, func() { s.outfit = items }) + default: + send(message{Type: "error", Message: "unknown message type"}) + } + } +} + +func (h *Hub) set(s *session, change func()) { + h.mu.Lock() + defer h.mu.Unlock() + change() + h.notifyLocked() +} + +// ErrUnauthorized is returned by Authorize funcs for a missing or bad token. +var ErrUnauthorized = errors.New("unauthorized") diff --git a/internal/store/migrations/001_accounts.sql b/internal/store/migrations/001_accounts.sql new file mode 100644 index 0000000..052a202 --- /dev/null +++ b/internal/store/migrations/001_accounts.sql @@ -0,0 +1,78 @@ +-- Accounts (people), their linked IMVU accounts, website sessions, sign-ins from proxies, and the audit log. + +create table persons ( + id bigserial primary key, + username text not null, + password_hash text not null, + role text not null default 'user' check (role in ('user', 'moderator', 'admin')), + -- How many IMVU accounts the person may link; moderators raise it on request. + link_cap integer not null default 10 check (link_cap >= 0), + created_at timestamptz not null default now(), + banned_at timestamptz, + ban_reason text not null default '' +); +create unique index persons_username on persons (lower(username)); + +create table sessions ( + token_hash bytea primary key, + person_id bigint not null references persons on delete cascade, + csrf text not null, + created_at timestamptz not null default now(), + expires_at timestamptz not null +); +create index sessions_person on sessions (person_id); + +-- A linked IMVU account. Linking it again from another person moves it (the most recent proof wins). +create table imvu_links ( + imvu_id bigint primary key, + person_id bigint not null references persons on delete cascade, + -- What catalogs and rooms see instead of the person, so a person's accounts can't be connected. + pairwise_id text not null unique, + username text not null, + linked_at timestamptz not null default now(), + age_verified boolean not null default false, + access_pass boolean not null default false, + checked_at timestamptz not null default now(), + show_badge boolean not null default false, + show_presence boolean not null default false +); +create index imvu_links_person on imvu_links (person_id); + +-- A code the person is to put in an IMVU account's tagline, proving it's theirs. +create table link_challenges ( + person_id bigint not null references persons on delete cascade, + imvu_id bigint not null, + username text not null, + code text not null, + created_at timestamptz not null default now(), + primary key (person_id, imvu_id) +); + +-- Signing a proxy in: the proxy shows user_code, the person approves it on the website. +create table device_codes ( + device_hash bytea primary key, + user_code text not null unique, + created_at timestamptz not null default now(), + expires_at timestamptz not null, + person_id bigint references persons on delete cascade, + consumed_at timestamptz +); + +create table refresh_tokens ( + token_hash bytea primary key, + person_id bigint not null references persons on delete cascade, + created_at timestamptz not null default now(), + last_used_at timestamptz not null default now(), + revoked_at timestamptz +); +create index refresh_tokens_person on refresh_tokens (person_id); + +create table audit ( + id bigserial primary key, + at timestamptz not null default now(), + actor_id bigint references persons on delete set null, + action text not null, + target text not null default '', + detail jsonb not null default '{}' +); +create index audit_at on audit (at desc); diff --git a/internal/store/store.go b/internal/store/store.go new file mode 100644 index 0000000..47cd139 --- /dev/null +++ b/internal/store/store.go @@ -0,0 +1,529 @@ +// Package store is the hub's data in Postgres: people, their linked IMVU accounts, sessions, proxy sign-ins and +// the audit log. Plain SQL with pgx. +package store + +import ( + "context" + "crypto/sha256" + "embed" + "errors" + "fmt" + "io/fs" + "sort" + "strings" + "time" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgxpool" +) + +//go:embed migrations/*.sql +var migrations embed.FS + +// Store is the hub's database. +type Store struct { + db *pgxpool.Pool +} + +// ErrNotFound means there's no such row. +var ErrNotFound = errors.New("not found") + +// ErrTaken means a unique value (a username) is in use. +var ErrTaken = errors.New("taken") + +// Open connects to Postgres and brings its schema up to date. +func Open(ctx context.Context, url string) (*Store, error) { + db, err := pgxpool.New(ctx, url) + if err != nil { + return nil, err + } + s := &Store{db: db} + if err := s.migrate(ctx); err != nil { + db.Close() + return nil, err + } + return s, nil +} + +func (s *Store) Close() { s.db.Close() } + +// migrate applies the migrations not applied yet, in name order, each in a transaction. +func (s *Store) migrate(ctx context.Context) error { + if _, err := s.db.Exec(ctx, `create table if not exists schema_migrations (name text primary key, applied_at timestamptz not null default now())`); err != nil { + return err + } + names, err := fs.Glob(migrations, "migrations/*.sql") + if err != nil { + return err + } + sort.Strings(names) + for _, name := range names { + var done bool + if err := s.db.QueryRow(ctx, `select exists (select 1 from schema_migrations where name = $1)`, name).Scan(&done); err != nil { + return err + } + if done { + continue + } + sql, _ := migrations.ReadFile(name) + err := pgx.BeginFunc(ctx, s.db, func(tx pgx.Tx) error { + if _, err := tx.Exec(ctx, string(sql)); err != nil { + return err + } + _, err := tx.Exec(ctx, `insert into schema_migrations (name) values ($1)`, name) + return err + }) + if err != nil { + return fmt.Errorf("migration %s: %w", name, err) + } + } + return nil +} + +// Hash is how tokens are stored: never the token itself. +func Hash(token string) []byte { + h := sha256.Sum256([]byte(token)) + return h[:] +} + +func isUniqueViolation(err error) bool { + var pg *pgconn.PgError + return errors.As(err, &pg) && pg.Code == "23505" +} + +// Person is a hub account: a person. +type Person struct { + ID int64 + Username string + PasswordHash string + Role string + LinkCap int + CreatedAt time.Time + BannedAt *time.Time + BanReason string +} + +func (p *Person) IsModerator() bool { return p.Role == "moderator" || p.Role == "admin" } +func (p *Person) IsAdmin() bool { return p.Role == "admin" } +func (p *Person) Banned() bool { return p.BannedAt != nil } + +const personColumns = `id, username, password_hash, role, link_cap, created_at, banned_at, ban_reason` + +func scanPerson(row pgx.Row) (*Person, error) { + var p Person + err := row.Scan(&p.ID, &p.Username, &p.PasswordHash, &p.Role, &p.LinkCap, &p.CreatedAt, &p.BannedAt, &p.BanReason) + if errors.Is(err, pgx.ErrNoRows) { + return nil, ErrNotFound + } + return &p, err +} + +// CreatePerson makes an account. The first account is an admin. +func (s *Store) CreatePerson(ctx context.Context, username, passwordHash string) (*Person, error) { + p, err := scanPerson(s.db.QueryRow(ctx, `insert into persons (username, password_hash, role) + values ($1, $2, case when exists (select 1 from persons) then 'user' else 'admin' end) + returning `+personColumns, username, passwordHash)) + if isUniqueViolation(err) { + return nil, ErrTaken + } + return p, err +} + +func (s *Store) PersonByID(ctx context.Context, id int64) (*Person, error) { + return scanPerson(s.db.QueryRow(ctx, `select `+personColumns+` from persons where id = $1`, id)) +} + +func (s *Store) PersonByUsername(ctx context.Context, username string) (*Person, error) { + return scanPerson(s.db.QueryRow(ctx, `select `+personColumns+` from persons where lower(username) = lower($1)`, username)) +} + +// People lists accounts, newest first, optionally filtered by a username search. +func (s *Store) People(ctx context.Context, search string, limit int) ([]Person, error) { + rows, err := s.db.Query(ctx, `select `+personColumns+` from persons + where $1 = '' or lower(username) like '%' || lower($1) || '%' + or id in (select person_id from imvu_links where lower(username) like '%' || lower($1) || '%') + order by id desc limit $2`, search, limit) + if err != nil { + return nil, err + } + defer rows.Close() + var out []Person + for rows.Next() { + p, err := scanPerson(rows) + if err != nil { + return nil, err + } + out = append(out, *p) + } + return out, rows.Err() +} + +func (s *Store) SetRole(ctx context.Context, id int64, role string) error { + _, err := s.db.Exec(ctx, `update persons set role = $2 where id = $1`, id, role) + return err +} + +func (s *Store) SetLinkCap(ctx context.Context, id int64, cap int) error { + _, err := s.db.Exec(ctx, `update persons set link_cap = $2 where id = $1`, id, cap) + return err +} + +// SetBanned bans (with a reason) or unbans a person; a ban also ends their sessions and sign-ins. +func (s *Store) SetBanned(ctx context.Context, id int64, banned bool, reason string) error { + return pgx.BeginFunc(ctx, s.db, func(tx pgx.Tx) error { + if !banned { + _, err := tx.Exec(ctx, `update persons set banned_at = null, ban_reason = '' where id = $1`, id) + return err + } + if _, err := tx.Exec(ctx, `update persons set banned_at = now(), ban_reason = $2 where id = $1`, id, reason); err != nil { + return err + } + if _, err := tx.Exec(ctx, `delete from sessions where person_id = $1`, id); err != nil { + return err + } + _, err := tx.Exec(ctx, `update refresh_tokens set revoked_at = now() where person_id = $1 and revoked_at is null`, id) + return err + }) +} + +// Sessions + +func (s *Store) CreateSession(ctx context.Context, token string, personID int64, csrf string, expires time.Time) error { + _, err := s.db.Exec(ctx, `insert into sessions (token_hash, person_id, csrf, expires_at) values ($1, $2, $3, $4)`, + Hash(token), personID, csrf, expires) + return err +} + +// Session is the person and CSRF token of a live session. +func (s *Store) Session(ctx context.Context, token string) (*Person, string, error) { + var csrf string + var p Person + err := s.db.QueryRow(ctx, `select s.csrf, `+prefixed("p", personColumns)+` from sessions s join persons p on p.id = s.person_id + where s.token_hash = $1 and s.expires_at > now()`, Hash(token)). + Scan(&csrf, &p.ID, &p.Username, &p.PasswordHash, &p.Role, &p.LinkCap, &p.CreatedAt, &p.BannedAt, &p.BanReason) + if errors.Is(err, pgx.ErrNoRows) { + return nil, "", ErrNotFound + } + return &p, csrf, err +} + +func (s *Store) DeleteSession(ctx context.Context, token string) error { + _, err := s.db.Exec(ctx, `delete from sessions where token_hash = $1`, Hash(token)) + return err +} + +func prefixed(alias, columns string) string { + parts := strings.Split(columns, ", ") + for i := range parts { + parts[i] = alias + "." + parts[i] + } + return strings.Join(parts, ", ") +} + +// Linked IMVU accounts + +// Link is a linked IMVU account. +type Link struct { + IMVUID int64 + PersonID int64 + PairwiseID string + Username string + LinkedAt time.Time + AgeVerified bool + AccessPass bool + CheckedAt time.Time + ShowBadge bool + ShowPresence bool +} + +// Adult reports whether the account may see adult content: age verified by IMVU, or with Access Pass. +func (l *Link) Adult() bool { return l.AgeVerified || l.AccessPass } + +const linkColumns = `imvu_id, person_id, pairwise_id, username, linked_at, age_verified, access_pass, checked_at, show_badge, show_presence` + +func scanLink(row pgx.Row) (*Link, error) { + var l Link + err := row.Scan(&l.IMVUID, &l.PersonID, &l.PairwiseID, &l.Username, &l.LinkedAt, &l.AgeVerified, &l.AccessPass, &l.CheckedAt, &l.ShowBadge, &l.ShowPresence) + if errors.Is(err, pgx.ErrNoRows) { + return nil, ErrNotFound + } + return &l, err +} + +func (s *Store) Links(ctx context.Context, personID int64) ([]Link, error) { + rows, err := s.db.Query(ctx, `select `+linkColumns+` from imvu_links where person_id = $1 order by linked_at`, personID) + if err != nil { + return nil, err + } + defer rows.Close() + var out []Link + for rows.Next() { + l, err := scanLink(rows) + if err != nil { + return nil, err + } + out = append(out, *l) + } + return out, rows.Err() +} + +func (s *Store) LinkByIMVUID(ctx context.Context, imvuID int64) (*Link, error) { + return scanLink(s.db.QueryRow(ctx, `select `+linkColumns+` from imvu_links where imvu_id = $1`, imvuID)) +} + +// ErrLinkCap means the person has linked as many accounts as they may. +var ErrLinkCap = errors.New("link cap reached") + +// LinkAccount links an IMVU account to a person, moving it from whoever had it (the most recent proof wins). +// It returns the person it was moved from, if any. +func (s *Store) LinkAccount(ctx context.Context, personID, imvuID int64, username, pairwiseID string, ageVerified, accessPass bool) (movedFrom int64, err error) { + err = pgx.BeginFunc(ctx, s.db, func(tx pgx.Tx) error { + var cap, count int + if err := tx.QueryRow(ctx, `select link_cap, (select count(*) from imvu_links where person_id = $1 and imvu_id <> $2) + from persons where id = $1 for update`, personID, imvuID).Scan(&cap, &count); err != nil { + return err + } + if count >= cap { + return ErrLinkCap + } + err := tx.QueryRow(ctx, `select person_id from imvu_links where imvu_id = $1`, imvuID).Scan(&movedFrom) + if err != nil && !errors.Is(err, pgx.ErrNoRows) { + return err + } + if movedFrom == personID { + movedFrom = 0 + } + // A moved account gets a new pairwise ID: nothing connects its new owner to the old one. + _, err = tx.Exec(ctx, `insert into imvu_links (imvu_id, person_id, pairwise_id, username, age_verified, access_pass) + values ($1, $2, $3, $4, $5, $6) + on conflict (imvu_id) do update set person_id = excluded.person_id, + pairwise_id = case when imvu_links.person_id = excluded.person_id then imvu_links.pairwise_id else excluded.pairwise_id end, + username = excluded.username, age_verified = excluded.age_verified, access_pass = excluded.access_pass, + checked_at = now(), + linked_at = case when imvu_links.person_id = excluded.person_id then imvu_links.linked_at else now() end, + show_badge = case when imvu_links.person_id = excluded.person_id then imvu_links.show_badge else false end, + show_presence = case when imvu_links.person_id = excluded.person_id then imvu_links.show_presence else false end`, + imvuID, personID, pairwiseID, username, ageVerified, accessPass) + if err != nil { + return err + } + _, err = tx.Exec(ctx, `delete from link_challenges where imvu_id = $1`, imvuID) + return err + }) + return movedFrom, err +} + +func (s *Store) Unlink(ctx context.Context, personID, imvuID int64) error { + _, err := s.db.Exec(ctx, `delete from imvu_links where person_id = $1 and imvu_id = $2`, personID, imvuID) + return err +} + +// UpdateAge records a fresh age check of a linked account. +func (s *Store) UpdateAge(ctx context.Context, imvuID int64, username string, ageVerified, accessPass bool) error { + _, err := s.db.Exec(ctx, `update imvu_links set username = $2, age_verified = $3, access_pass = $4, checked_at = now() where imvu_id = $1`, + imvuID, username, ageVerified, accessPass) + return err +} + +func (s *Store) SetLinkSettings(ctx context.Context, personID, imvuID int64, badge, presence bool) error { + _, err := s.db.Exec(ctx, `update imvu_links set show_badge = $3, show_presence = $4 where person_id = $1 and imvu_id = $2`, + personID, imvuID, badge, presence) + return err +} + +// Challenge is a pending tagline code. +type Challenge struct { + IMVUID int64 + Username string + Code string + CreatedAt time.Time +} + +// SetChallenge stores (or replaces) the code for linking an account. +func (s *Store) SetChallenge(ctx context.Context, personID, imvuID int64, username, code string) error { + _, err := s.db.Exec(ctx, `insert into link_challenges (person_id, imvu_id, username, code) values ($1, $2, $3, $4) + on conflict (person_id, imvu_id) do update set code = excluded.code, username = excluded.username, created_at = now()`, + personID, imvuID, username, code) + return err +} + +func (s *Store) Challenges(ctx context.Context, personID int64) ([]Challenge, error) { + rows, err := s.db.Query(ctx, `select imvu_id, username, code, created_at from link_challenges + where person_id = $1 and created_at > now() - interval '1 day' order by created_at`, personID) + if err != nil { + return nil, err + } + defer rows.Close() + var out []Challenge + for rows.Next() { + var c Challenge + if err := rows.Scan(&c.IMVUID, &c.Username, &c.Code, &c.CreatedAt); err != nil { + return nil, err + } + out = append(out, c) + } + return out, rows.Err() +} + +func (s *Store) Challenge(ctx context.Context, personID, imvuID int64) (*Challenge, error) { + var c Challenge + err := s.db.QueryRow(ctx, `select imvu_id, username, code, created_at from link_challenges + where person_id = $1 and imvu_id = $2 and created_at > now() - interval '1 day'`, personID, imvuID). + Scan(&c.IMVUID, &c.Username, &c.Code, &c.CreatedAt) + if errors.Is(err, pgx.ErrNoRows) { + return nil, ErrNotFound + } + return &c, err +} + +func (s *Store) DeleteChallenge(ctx context.Context, personID, imvuID int64) error { + _, err := s.db.Exec(ctx, `delete from link_challenges where person_id = $1 and imvu_id = $2`, personID, imvuID) + return err +} + +// Proxy sign-in (device codes) and refresh tokens + +func (s *Store) CreateDeviceCode(ctx context.Context, deviceCode, userCode string, expires time.Time) error { + _, err := s.db.Exec(ctx, `insert into device_codes (device_hash, user_code, expires_at) values ($1, $2, $3)`, + Hash(deviceCode), userCode, expires) + if isUniqueViolation(err) { + return ErrTaken + } + return err +} + +// ApproveDeviceCode lets the person's proxy waiting with userCode sign in. +func (s *Store) ApproveDeviceCode(ctx context.Context, userCode string, personID int64) error { + tag, err := s.db.Exec(ctx, `update device_codes set person_id = $2 + where user_code = $1 and person_id is null and consumed_at is null and expires_at > now()`, userCode, personID) + if err == nil && tag.RowsAffected() == 0 { + return ErrNotFound + } + return err +} + +// DeviceStatus is a waiting proxy's sign-in: approved (with the person), pending, or gone. +type DeviceStatus int + +const ( + DevicePending DeviceStatus = iota + DeviceApproved + DeviceGone +) + +// ConsumeDeviceCode reports a device code's state; an approved one is used up by this call. +func (s *Store) ConsumeDeviceCode(ctx context.Context, deviceCode string) (DeviceStatus, int64, error) { + var personID *int64 + var expires time.Time + var consumed *time.Time + err := s.db.QueryRow(ctx, `select person_id, expires_at, consumed_at from device_codes where device_hash = $1`, Hash(deviceCode)). + Scan(&personID, &expires, &consumed) + if errors.Is(err, pgx.ErrNoRows) || err == nil && (consumed != nil || time.Now().After(expires)) { + return DeviceGone, 0, nil + } + if err != nil { + return 0, 0, err + } + if personID == nil { + return DevicePending, 0, nil + } + tag, err := s.db.Exec(ctx, `update device_codes set consumed_at = now() where device_hash = $1 and consumed_at is null`, Hash(deviceCode)) + if err != nil { + return 0, 0, err + } + if tag.RowsAffected() == 0 { + return DeviceGone, 0, nil + } + return DeviceApproved, *personID, nil +} + +func (s *Store) CreateRefreshToken(ctx context.Context, token string, personID int64) error { + _, err := s.db.Exec(ctx, `insert into refresh_tokens (token_hash, person_id) values ($1, $2)`, Hash(token), personID) + return err +} + +// RotateRefreshToken trades a live refresh token for a new one, returning the person it's for. +func (s *Store) RotateRefreshToken(ctx context.Context, old, replacement string) (int64, error) { + var personID int64 + err := pgx.BeginFunc(ctx, s.db, func(tx pgx.Tx) error { + err := tx.QueryRow(ctx, `update refresh_tokens set revoked_at = now() + where token_hash = $1 and revoked_at is null returning person_id`, Hash(old)).Scan(&personID) + if errors.Is(err, pgx.ErrNoRows) { + return ErrNotFound + } + if err != nil { + return err + } + _, err = tx.Exec(ctx, `insert into refresh_tokens (token_hash, person_id) values ($1, $2)`, Hash(replacement), personID) + return err + }) + return personID, err +} + +// Signins counts a person's signed-in proxies. +func (s *Store) Signins(ctx context.Context, personID int64) (int, error) { + var n int + err := s.db.QueryRow(ctx, `select count(*) from refresh_tokens where person_id = $1 and revoked_at is null`, personID).Scan(&n) + return n, err +} + +// SignOutProxies ends all of a person's proxy sign-ins. +func (s *Store) SignOutProxies(ctx context.Context, personID int64) error { + _, err := s.db.Exec(ctx, `update refresh_tokens set revoked_at = now() where person_id = $1 and revoked_at is null`, personID) + return err +} + +// Audit log + +// Audit records something a person (or the hub, actor 0) did. +func (s *Store) Audit(ctx context.Context, actorID int64, action, target string, detail map[string]any) error { + var actor *int64 + if actorID != 0 { + actor = &actorID + } + if detail == nil { + detail = map[string]any{} + } + _, err := s.db.Exec(ctx, `insert into audit (actor_id, action, target, detail) values ($1, $2, $3, $4)`, actor, action, target, detail) + return err +} + +// AuditEntry is a line of the audit log. +type AuditEntry struct { + At time.Time + Actor string + Action string + Target string + Detail map[string]any +} + +func (s *Store) AuditLog(ctx context.Context, limit int) ([]AuditEntry, error) { + rows, err := s.db.Query(ctx, `select a.at, coalesce(p.username, ''), a.action, a.target, a.detail + from audit a left join persons p on p.id = a.actor_id order by a.id desc limit $1`, limit) + if err != nil { + return nil, err + } + defer rows.Close() + var out []AuditEntry + for rows.Next() { + var e AuditEntry + if err := rows.Scan(&e.At, &e.Actor, &e.Action, &e.Target, &e.Detail); err != nil { + return nil, err + } + out = append(out, e) + } + return out, rows.Err() +} + +// Counts are totals for the console. +type Counts struct { + People, Links, Signins int +} + +func (s *Store) Counts(ctx context.Context) (Counts, error) { + var c Counts + err := s.db.QueryRow(ctx, `select (select count(*) from persons), (select count(*) from imvu_links), + (select count(*) from refresh_tokens where revoked_at is null)`).Scan(&c.People, &c.Links, &c.Signins) + return c, err +} diff --git a/internal/token/token.go b/internal/token/token.go new file mode 100644 index 0000000..17b4b15 --- /dev/null +++ b/internal/token/token.go @@ -0,0 +1,117 @@ +// Package token issues and checks the hub's access tokens: JWTs signed with Ed25519 ("EdDSA"), short-lived. The +// public keys are published (JWKS), so catalogs can check tokens without asking the hub. +package token + +import ( + "crypto/ed25519" + "crypto/rand" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + "strings" + "time" +) + +// Claims are what a token says. +type Claims struct { + Issuer string `json:"iss"` + Subject string `json:"sub"` // "person:" for the hub's own API + Audience string `json:"aud"` + IssuedAt int64 `json:"iat"` + Expires int64 `json:"exp"` + Scope string `json:"scope,omitempty"` +} + +// Keys signs and checks tokens. +type Keys struct { + id string + private ed25519.PrivateKey + issuer string +} + +// LoadOrCreate reads the signing key from path, making one if there's none. +func LoadOrCreate(path, issuer string) (*Keys, error) { + seed, err := os.ReadFile(path) + if errors.Is(err, os.ErrNotExist) { + seed = make([]byte, ed25519.SeedSize) + if _, err := rand.Read(seed); err != nil { + return nil, err + } + if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { + return nil, err + } + if err := os.WriteFile(path, seed, 0o600); err != nil { + return nil, err + } + } else if err != nil { + return nil, err + } + if len(seed) != ed25519.SeedSize { + return nil, fmt.Errorf("%s: not an Ed25519 seed", path) + } + key := ed25519.NewKeyFromSeed(seed) + pub := key.Public().(ed25519.PublicKey) + return &Keys{id: b64(pub[:8]), private: key, issuer: issuer}, nil +} + +func b64(b []byte) string { return base64.RawURLEncoding.EncodeToString(b) } + +// Issue signs a token for subject and audience, valid for ttl. +func (k *Keys) Issue(subject, audience, scope string, ttl time.Duration) (string, error) { + now := time.Now() + header, _ := json.Marshal(map[string]string{"alg": "EdDSA", "typ": "JWT", "kid": k.id}) + claims, _ := json.Marshal(Claims{Issuer: k.issuer, Subject: subject, Audience: audience, IssuedAt: now.Unix(), + Expires: now.Add(ttl).Unix(), Scope: scope}) + signing := b64(header) + "." + b64(claims) + return signing + "." + b64(ed25519.Sign(k.private, []byte(signing))), nil +} + +// ErrInvalid means a token is malformed, forged, for someone else, or expired. +var ErrInvalid = errors.New("invalid token") + +// Check verifies a token for audience and returns its claims. +func (k *Keys) Check(token, audience string) (*Claims, error) { + parts := strings.Split(token, ".") + if len(parts) != 3 { + return nil, ErrInvalid + } + headerJSON, err1 := base64.RawURLEncoding.DecodeString(parts[0]) + claimsJSON, err2 := base64.RawURLEncoding.DecodeString(parts[1]) + sig, err3 := base64.RawURLEncoding.DecodeString(parts[2]) + if err1 != nil || err2 != nil || err3 != nil { + return nil, ErrInvalid + } + var header struct{ Alg, Kid string } + if json.Unmarshal(headerJSON, &header) != nil || header.Alg != "EdDSA" || header.Kid != k.id { + return nil, ErrInvalid + } + if !ed25519.Verify(k.private.Public().(ed25519.PublicKey), []byte(parts[0]+"."+parts[1]), sig) { + return nil, ErrInvalid + } + var c Claims + if json.Unmarshal(claimsJSON, &c) != nil || c.Issuer != k.issuer || c.Audience != audience || time.Now().Unix() >= c.Expires { + return nil, ErrInvalid + } + return &c, nil +} + +// JWKS is the public key set, as JSON (RFC 8037 OKP keys). +func (k *Keys) JWKS() []byte { + pub := k.private.Public().(ed25519.PublicKey) + b, _ := json.Marshal(map[string]any{"keys": []map[string]string{{ + "kty": "OKP", "crv": "Ed25519", "alg": "EdDSA", "use": "sig", "kid": k.id, "x": b64(pub), + }}}) + return b +} + +// Random is a random URL-safe string of n bytes' entropy, for session and refresh tokens. +func Random(n int) string { + b := make([]byte, n) + if _, err := rand.Read(b); err != nil { + panic(err) + } + return b64(b) +} diff --git a/internal/token/token_test.go b/internal/token/token_test.go new file mode 100644 index 0000000..c472b1e --- /dev/null +++ b/internal/token/token_test.go @@ -0,0 +1,44 @@ +package token + +import ( + "encoding/json" + "path/filepath" + "strings" + "testing" + "time" +) + +func TestIssueAndCheck(t *testing.T) { + path := filepath.Join(t.TempDir(), "key") + k, err := LoadOrCreate(path, "https://hub.test") + if err != nil { + t.Fatal(err) + } + tok, _ := k.Issue("person:1", "t5de-hub", "proxy", time.Minute) + c, err := k.Check(tok, "t5de-hub") + if err != nil || c.Subject != "person:1" || c.Scope != "proxy" { + t.Fatalf("%+v %v", c, err) + } + if _, err := k.Check(tok, "a-catalog"); err == nil { + t.Fatal("accepted for another audience") + } + // The same key comes back from the file. + k2, _ := LoadOrCreate(path, "https://hub.test") + if _, err := k2.Check(tok, "t5de-hub"); err != nil { + t.Fatal(err) + } + // Tampering and expiry are caught. + parts := strings.Split(tok, ".") + forged := parts[0] + "." + b64([]byte(`{"iss":"https://hub.test","sub":"person:2","aud":"t5de-hub","exp":9999999999}`)) + "." + parts[2] + if _, err := k.Check(forged, "t5de-hub"); err == nil { + t.Fatal("accepted a forged token") + } + old, _ := k.Issue("person:1", "t5de-hub", "", -time.Second) + if _, err := k.Check(old, "t5de-hub"); err == nil { + t.Fatal("accepted an expired token") + } + var jwks struct{ Keys []map[string]string } + if json.Unmarshal(k.JWKS(), &jwks) != nil || len(jwks.Keys) != 1 || jwks.Keys[0]["crv"] != "Ed25519" { + t.Fatalf("%s", k.JWKS()) + } +} diff --git a/internal/web/account.go b/internal/web/account.go new file mode 100644 index 0000000..b1a2ecf --- /dev/null +++ b/internal/web/account.go @@ -0,0 +1,278 @@ +package web + +import ( + "crypto/rand" + "errors" + "fmt" + "net/http" + "net/url" + "strconv" + "strings" + "time" + + "git.t5de.dev/t5de/t5de-hub/internal/imvu" + "git.t5de.dev/t5de/t5de-hub/internal/realtime" + "git.t5de.dev/t5de/t5de-hub/internal/store" + "git.t5de.dev/t5de/t5de-hub/internal/token" +) + +func (s *Server) home(q *request) error { + data := map[string]any{"OnlineCount": len(s.visibleOnline(q))} + if q.person != nil { + links, err := s.Store.Links(s.ctx(q), q.person.ID) + if err != nil { + return err + } + data["Links"] = links + } + s.render(q, "home", data) + return nil +} + +// linkView is a linked account on the account page. +type linkView struct { + store.Link + Online bool + Outfit []realtime.Item +} + +func (s *Server) account(q *request) error { + ctx := s.ctx(q) + links, err := s.Store.Links(ctx, q.person.ID) + if err != nil { + return err + } + challenges, err := s.Store.Challenges(ctx, q.person.ID) + if err != nil { + return err + } + signins, err := s.Store.Signins(ctx, q.person.ID) + if err != nil { + return err + } + online := map[int64]realtime.Online{} + for _, o := range s.Realtime.Online() { + online[o.IMVUID] = o + } + views := make([]linkView, len(links)) + for i, l := range links { + o, ok := online[l.IMVUID] + views[i] = linkView{Link: l, Online: ok, Outfit: o.Outfit} + } + s.render(q, "account", map[string]any{ + "Links": views, "Challenges": challenges, "Signins": signins, + "CanLink": len(links) < q.person.LinkCap, "Error": q.r.URL.Query().Get("error"), + }) + return nil +} + +// linkCode is what goes in the tagline: "t5de-" and eight letters and digits (no look-alikes). +func linkCode() string { + const alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789" + b := make([]byte, 8) + rand.Read(b) + for i := range b { + b[i] = alphabet[int(b[i])%len(alphabet)] + } + return "t5de-" + string(b[:4]) + "-" + string(b[4:]) +} + +func (s *Server) startLink(q *request) error { + username := strings.TrimSpace(q.r.PostFormValue("username")) + if !s.limiter.allow("imvu:"+strconv.FormatInt(q.person.ID, 10), 20, time.Hour) { + return q.redirect("/account?error="+queryEscape("Too many lookups. Try again in a while."), "") + } + u, err := s.IMVU.ByUsername(s.ctx(q), username) + if errors.Is(err, imvu.ErrNoSuchUser) { + return q.redirect("/account?error="+queryEscape(fmt.Sprintf("IMVU has no account named %q.", username)), "") + } + if err != nil { + s.Log.Printf("looking up %q on IMVU: %v", username, err) + return q.redirect("/account?error="+queryEscape("IMVU didn't answer. Try again in a moment."), "") + } + if l, err := s.Store.LinkByIMVUID(s.ctx(q), u.ID); err == nil && l.PersonID == q.person.ID { + return q.redirect("/account", u.Username+" is already linked.") + } + if err := s.Store.SetChallenge(s.ctx(q), q.person.ID, u.ID, u.Username, linkCode()); err != nil { + return err + } + return q.redirect("/account#link-"+strconv.FormatInt(u.ID, 10), "") +} + +func imvuIDParam(q *request) (int64, error) { + id, err := strconv.ParseInt(q.r.PathValue("imvu"), 10, 64) + if err != nil || id <= 0 { + return 0, fail(http.StatusNotFound, "No such account.") + } + return id, nil +} + +// checkLink reads the account's tagline from IMVU and links it if the code is there. +func (s *Server) checkLink(q *request) error { + ctx := s.ctx(q) + id, err := imvuIDParam(q) + if err != nil { + return err + } + c, err := s.Store.Challenge(ctx, q.person.ID, id) + if errors.Is(err, store.ErrNotFound) { + return q.redirect("/account?error="+queryEscape("That code expired. Start again."), "") + } + if err != nil { + return err + } + if !s.limiter.allow("imvu:"+strconv.FormatInt(q.person.ID, 10), 20, time.Hour) { + return q.redirect("/account?error="+queryEscape("Too many checks. Try again in a while."), "") + } + u, err := s.IMVU.ByID(ctx, id) + if err != nil { + s.Log.Printf("checking IMVU account %d: %v", id, err) + return q.redirect("/account?error="+queryEscape("IMVU didn't answer. Try again in a moment."), "") + } + if !strings.Contains(strings.ToLower(u.Tagline), strings.ToLower(c.Code)) { + return q.redirect("/account?error="+queryEscape(fmt.Sprintf("%s's tagline doesn't have the code yet. IMVU can take a minute to show a new tagline.", u.Username))+"#link-"+strconv.FormatInt(id, 10), "") + } + moved, err := s.Store.LinkAccount(ctx, q.person.ID, u.ID, u.Username, token.Random(16), u.AgeVerified, u.AccessPass) + if errors.Is(err, store.ErrLinkCap) { + return q.redirect("/account?error="+queryEscape(fmt.Sprintf("You can link up to %d IMVU accounts. Ask a moderator if you need more.", q.person.LinkCap)), "") + } + if err != nil { + return err + } + detail := map[string]any{"imvu": u.Username} + if moved != 0 { + detail["moved_from"] = moved + } + s.Store.Audit(ctx, q.person.ID, "link.add", strconv.FormatInt(u.ID, 10), detail) + return q.redirect("/account", fmt.Sprintf("Linked %s. You can take the code out of your tagline now.", u.Username)) +} + +func (s *Server) cancelLink(q *request) error { + id, err := imvuIDParam(q) + if err != nil { + return err + } + if err := s.Store.DeleteChallenge(s.ctx(q), q.person.ID, id); err != nil { + return err + } + return q.redirect("/account", "") +} + +func (s *Server) ownLink(q *request) (*store.Link, error) { + id, err := imvuIDParam(q) + if err != nil { + return nil, err + } + l, err := s.Store.LinkByIMVUID(s.ctx(q), id) + if errors.Is(err, store.ErrNotFound) || err == nil && l.PersonID != q.person.ID { + return nil, fail(http.StatusNotFound, "That account isn't linked to you.") + } + return l, err +} + +func (s *Server) linkSettings(q *request) error { + l, err := s.ownLink(q) + if err != nil { + return err + } + badge, presence := q.r.PostFormValue("badge") == "on", q.r.PostFormValue("presence") == "on" + if err := s.Store.SetLinkSettings(s.ctx(q), q.person.ID, l.IMVUID, badge, presence); err != nil { + return err + } + return q.redirect("/account", "Saved settings for "+l.Username+".") +} + +// recheckLink reads the account's age status from IMVU again. +func (s *Server) recheckLink(q *request) error { + l, err := s.ownLink(q) + if err != nil { + return err + } + if !s.limiter.allow("imvu:"+strconv.FormatInt(q.person.ID, 10), 20, time.Hour) { + return q.redirect("/account?error="+queryEscape("Too many checks. Try again in a while."), "") + } + u, err := s.IMVU.ByID(s.ctx(q), l.IMVUID) + if err != nil { + return q.redirect("/account?error="+queryEscape("IMVU didn't answer. Try again in a moment."), "") + } + if err := s.Store.UpdateAge(s.ctx(q), l.IMVUID, u.Username, u.AgeVerified, u.AccessPass); err != nil { + return err + } + return q.redirect("/account", "Checked "+u.Username+" with IMVU.") +} + +func (s *Server) unlink(q *request) error { + l, err := s.ownLink(q) + if err != nil { + return err + } + if err := s.Store.Unlink(s.ctx(q), q.person.ID, l.IMVUID); err != nil { + return err + } + s.Store.Audit(s.ctx(q), q.person.ID, "link.remove", strconv.FormatInt(l.IMVUID, 10), map[string]any{"imvu": l.Username}) + return q.redirect("/account", "Unlinked "+l.Username+".") +} + +func (s *Server) signOutProxies(q *request) error { + if err := s.Store.SignOutProxies(s.ctx(q), q.person.ID); err != nil { + return err + } + s.Realtime.Disconnect(q.person.ID) + return q.redirect("/account", "Signed out of T5DE on all your computers.") +} + +func (s *Server) deviceForm(q *request) error { + s.render(q, "device", map[string]any{"Code": strings.ToUpper(q.r.URL.Query().Get("code"))}) + return nil +} + +func (s *Server) approveDevice(q *request) error { + code := normalizeUserCode(q.r.PostFormValue("code")) + err := s.Store.ApproveDeviceCode(s.ctx(q), code, q.person.ID) + if errors.Is(err, store.ErrNotFound) { + q.w.WriteHeader(http.StatusBadRequest) + s.render(q, "device", map[string]any{"Code": code, "Error": "That code isn't right, or it expired. Check the code T5DE shows."}) + return nil + } + if err != nil { + return err + } + s.Store.Audit(s.ctx(q), q.person.ID, "proxy.signin", "", nil) + s.render(q, "device", map[string]any{"Approved": true}) + return nil +} + +// visibleOnline is who the viewer may see online in T5DE: accounts that opted in to presence (moderators see all). +func (s *Server) visibleOnline(q *request) []onlineView { + var out []onlineView + for _, o := range s.Realtime.Online() { + l, err := s.Store.LinkByIMVUID(s.ctx(q), o.IMVUID) + if err != nil { + continue + } + mod := q.person != nil && q.person.IsModerator() + if !l.ShowPresence && !mod { + continue + } + out = append(out, onlineView{Link: *l, Online: o, Hidden: !l.ShowPresence}) + } + return out +} + +type onlineView struct { + Link store.Link + Online realtime.Online + Hidden bool // shown to a moderator only +} + +func (s *Server) online(q *request) error { + s.render(q, "online", map[string]any{"Online": s.visibleOnline(q)}) + return nil +} + +func (s *Server) onlineList(q *request) error { + s.fragment(q, "online", "online-list", map[string]any{"Online": s.visibleOnline(q)}) + return nil +} + +func queryEscape(s string) string { return url.QueryEscape(s) } diff --git a/internal/web/api.go b/internal/web/api.go new file mode 100644 index 0000000..3f8b992 --- /dev/null +++ b/internal/web/api.go @@ -0,0 +1,216 @@ +package web + +import ( + "context" + "crypto/rand" + "encoding/json" + "errors" + "net/http" + "strconv" + "strings" + "time" + + "git.t5de.dev/t5de/t5de-hub/internal/store" + "git.t5de.dev/t5de/t5de-hub/internal/token" +) + +// The proxy signs in like a TV app (OAuth 2.0 device authorization, RFC 8628): it asks for a code, the person +// enters it on the website, and the proxy gets tokens: a short-lived access token (Ed25519 JWT) for the API, and +// a refresh token it trades for new ones. + +const ( + deviceLifetime = 10 * time.Minute + accessLifetime = 15 * time.Minute +) + +func writeJSON(w http.ResponseWriter, status int, v any) { + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Cache-Control", "no-store") + w.WriteHeader(status) + json.NewEncoder(w).Encode(v) +} + +func apiError(w http.ResponseWriter, status int, code, description string) { + writeJSON(w, status, map[string]string{"error": code, "error_description": description}) +} + +// userCode is what a person types: 8 letters, no look-alikes, shown as XXXX-XXXX. +func userCode() string { + const alphabet = "BCDFGHJKLMNPQRSTVWXZ" + b := make([]byte, 8) + rand.Read(b) + for i := range b { + b[i] = alphabet[int(b[i])%len(alphabet)] + } + return string(b[:4]) + "-" + string(b[4:]) +} + +func normalizeUserCode(code string) string { + code = strings.ToUpper(strings.Map(func(r rune) rune { + if r == ' ' || r == '-' { + return -1 + } + return r + }, code)) + if len(code) == 8 { + code = code[:4] + "-" + code[4:] + } + return code +} + +func (s *Server) deviceCode(w http.ResponseWriter, r *http.Request) { + if !s.limiter.allow("device:"+clientIP(r), 20, time.Hour) { + apiError(w, http.StatusTooManyRequests, "slow_down", "too many sign-ins from here") + return + } + device := token.Random(32) + var code string + for range 5 { + code = userCode() + err := s.Store.CreateDeviceCode(r.Context(), device, code, time.Now().Add(deviceLifetime)) + if err == nil { + break + } + if !errors.Is(err, store.ErrTaken) { + s.Log.Printf("device code: %v", err) + apiError(w, http.StatusInternalServerError, "server_error", "") + return + } + } + writeJSON(w, http.StatusOK, map[string]any{ + "device_code": device, "user_code": code, "expires_in": int(deviceLifetime.Seconds()), "interval": 3, + "verification_uri": s.PublicURL + "/device", + "verification_uri_complete": s.PublicURL + "/device?code=" + code, + }) +} + +func (s *Server) issue(ctx context.Context, w http.ResponseWriter, personID int64, refresh string) { + access, err := s.Keys.Issue("person:"+strconv.FormatInt(personID, 10), Audience, "proxy", accessLifetime) + if err != nil { + apiError(w, http.StatusInternalServerError, "server_error", "") + return + } + writeJSON(w, http.StatusOK, map[string]any{"access_token": access, "token_type": "Bearer", + "expires_in": int(accessLifetime.Seconds()), "refresh_token": refresh}) +} + +func (s *Server) deviceToken(w http.ResponseWriter, r *http.Request) { + status, personID, err := s.Store.ConsumeDeviceCode(r.Context(), r.PostFormValue("device_code")) + if err != nil { + s.Log.Printf("device token: %v", err) + apiError(w, http.StatusInternalServerError, "server_error", "") + return + } + switch status { + case store.DevicePending: + apiError(w, http.StatusBadRequest, "authorization_pending", "waiting for the code to be entered") + return + case store.DeviceGone: + apiError(w, http.StatusBadRequest, "expired_token", "the code expired or was used") + return + } + p, err := s.Store.PersonByID(r.Context(), personID) + if err != nil || p.Banned() { + apiError(w, http.StatusForbidden, "access_denied", "") + return + } + refresh := token.Random(32) + if err := s.Store.CreateRefreshToken(r.Context(), refresh, personID); err != nil { + apiError(w, http.StatusInternalServerError, "server_error", "") + return + } + s.issue(r.Context(), w, personID, refresh) +} + +func (s *Server) refresh(w http.ResponseWriter, r *http.Request) { + replacement := token.Random(32) + personID, err := s.Store.RotateRefreshToken(r.Context(), r.PostFormValue("refresh_token"), replacement) + if errors.Is(err, store.ErrNotFound) { + apiError(w, http.StatusUnauthorized, "invalid_grant", "signed out") + return + } + if err != nil { + apiError(w, http.StatusInternalServerError, "server_error", "") + return + } + p, err := s.Store.PersonByID(r.Context(), personID) + if err != nil || p.Banned() { + apiError(w, http.StatusForbidden, "access_denied", "") + return + } + s.issue(r.Context(), w, personID, replacement) +} + +// bearer is the person an API request's access token is for. +func (s *Server) bearer(r *http.Request) (*store.Person, error) { + auth := r.Header.Get("Authorization") + tok, ok := strings.CutPrefix(auth, "Bearer ") + if !ok { + return nil, errors.New("no token") + } + c, err := s.Keys.Check(tok, Audience) + if err != nil { + return nil, err + } + id, err := strconv.ParseInt(strings.TrimPrefix(c.Subject, "person:"), 10, 64) + if err != nil { + return nil, err + } + p, err := s.Store.PersonByID(r.Context(), id) + if err != nil || p.Banned() { + return nil, errors.New("no such person") + } + return p, nil +} + +func (s *Server) me(w http.ResponseWriter, r *http.Request) { + p, err := s.bearer(r) + if err != nil { + apiError(w, http.StatusUnauthorized, "invalid_token", "") + return + } + links, err := s.Store.Links(r.Context(), p.ID) + if err != nil { + apiError(w, http.StatusInternalServerError, "server_error", "") + return + } + type link struct { + IMVUID int64 `json:"imvu_id"` + Username string `json:"username"` + Adult bool `json:"adult"` + } + out := []link{} + for _, l := range links { + out = append(out, link{l.IMVUID, l.Username, l.Adult()}) + } + writeJSON(w, http.StatusOK, map[string]any{"username": p.Username, "links": out}) +} + +func (s *Server) realtime(w http.ResponseWriter, r *http.Request) { + p, err := s.bearer(r) + if err != nil { + apiError(w, http.StatusUnauthorized, "invalid_token", "") + return + } + s.Realtime.Serve(w, r, p.ID) +} + +func (s *Server) jwks(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Cache-Control", "public, max-age=300") + w.Write(s.Keys.JWKS()) +} + +// LinkOwner implements realtime.Owners. +type Owners struct{ Store *store.Store } + +func (o Owners) LinkOwner(ctx context.Context, imvuID int64) (int64, bool, error) { + l, err := o.Store.LinkByIMVUID(ctx, imvuID) + if errors.Is(err, store.ErrNotFound) { + return 0, false, nil + } + if err != nil { + return 0, false, err + } + return l.PersonID, true, nil +} diff --git a/internal/web/auth.go b/internal/web/auth.go new file mode 100644 index 0000000..567af97 --- /dev/null +++ b/internal/web/auth.go @@ -0,0 +1,172 @@ +package web + +import ( + "crypto/rand" + "crypto/subtle" + "encoding/base64" + "errors" + "fmt" + "net/http" + "regexp" + "strings" + "time" + "unicode/utf8" + + "golang.org/x/crypto/argon2" + + "git.t5de.dev/t5de/t5de-hub/internal/store" + "git.t5de.dev/t5de/t5de-hub/internal/token" +) + +// Passwords are hashed with Argon2id (OWASP's minimum: 19 MiB, 2 passes). +const ( + argonTime = 2 + argonMemory = 19 * 1024 + argonThreads = 1 + argonKeyLen = 32 +) + +func hashPassword(password string) string { + salt := make([]byte, 16) + rand.Read(salt) + key := argon2.IDKey([]byte(password), salt, argonTime, argonMemory, argonThreads, argonKeyLen) + enc := base64.RawStdEncoding.EncodeToString + return fmt.Sprintf("$argon2id$v=19$m=%d,t=%d,p=%d$%s$%s", argonMemory, argonTime, argonThreads, enc(salt), enc(key)) +} + +func checkPassword(hash, password string) bool { + parts := strings.Split(hash, "$") + if len(parts) != 6 || parts[1] != "argon2id" { + return false + } + var m, t uint32 + var p uint8 + if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &m, &t, &p); err != nil { + return false + } + salt, err1 := base64.RawStdEncoding.DecodeString(parts[4]) + want, err2 := base64.RawStdEncoding.DecodeString(parts[5]) + if err1 != nil || err2 != nil { + return false + } + got := argon2.IDKey([]byte(password), salt, t, m, p, uint32(len(want))) + return subtle.ConstantTimeCompare(got, want) == 1 +} + +// dummyHash is checked against for unknown usernames, so sign-in takes as long either way. +var dummyHash = hashPassword("not a real password") + +var usernamePattern = regexp.MustCompile(`^[A-Za-z0-9_]{3,32}$`) + +func (s *Server) signupForm(q *request) error { + if q.person != nil { + return q.redirect("/account", "") + } + s.render(q, "signup", nil) + return nil +} + +func (s *Server) signup(q *request) error { + username := strings.TrimSpace(q.r.PostFormValue("username")) + password := q.r.PostFormValue("password") + problem := "" + switch { + case !usernamePattern.MatchString(username): + problem = "Usernames are 3 to 32 letters, digits or underscores." + case utf8.RuneCountInString(password) < 10: + problem = "Use a password of at least 10 characters." + case len(password) > 256: + problem = "That password is too long." + case password != q.r.PostFormValue("password2"): + problem = "The passwords don't match." + case !s.limiter.allow("signup:"+clientIP(q.r), 5, time.Hour): + problem = "Too many new accounts from here. Try again later." + } + if problem != "" { + q.w.WriteHeader(http.StatusBadRequest) + s.render(q, "signup", map[string]any{"Error": problem, "Username": username}) + return nil + } + p, err := s.Store.CreatePerson(s.ctx(q), username, hashPassword(password)) + if errors.Is(err, store.ErrTaken) { + q.w.WriteHeader(http.StatusBadRequest) + s.render(q, "signup", map[string]any{"Error": "That username is taken.", "Username": username}) + return nil + } + if err != nil { + return err + } + s.Store.Audit(s.ctx(q), p.ID, "account.create", p.Username, nil) + if err := s.startSession(q, p.ID); err != nil { + return err + } + return q.redirect("/account", "Welcome to T5DE! Link your IMVU account to get started.") +} + +func (s *Server) signinForm(q *request) error { + if q.person != nil { + return q.redirect("/account", "") + } + s.render(q, "signin", map[string]any{"Next": safeNext(q.r.URL.Query().Get("next"))}) + return nil +} + +func (s *Server) signin(q *request) error { + username := strings.TrimSpace(q.r.PostFormValue("username")) + password := q.r.PostFormValue("password") + next := safeNext(q.r.PostFormValue("next")) + again := func(problem string) error { + q.w.WriteHeader(http.StatusBadRequest) + s.render(q, "signin", map[string]any{"Error": problem, "Username": username, "Next": next}) + return nil + } + if !s.limiter.allow("signin:"+clientIP(q.r), 20, 15*time.Minute) || !s.limiter.allow("signin-user:"+strings.ToLower(username), 10, 15*time.Minute) { + return again("Too many tries. Wait a few minutes and try again.") + } + p, err := s.Store.PersonByUsername(s.ctx(q), username) + if errors.Is(err, store.ErrNotFound) { + checkPassword(dummyHash, password) + return again("Wrong username or password.") + } + if err != nil { + return err + } + if !checkPassword(p.PasswordHash, password) { + return again("Wrong username or password.") + } + if p.Banned() { + return again("This account is banned. " + p.BanReason) + } + if err := s.startSession(q, p.ID); err != nil { + return err + } + return q.redirect(next, "") +} + +// safeNext keeps redirects after signing in on the hub. +func safeNext(next string) string { + if !strings.HasPrefix(next, "/") || strings.HasPrefix(next, "//") || strings.HasPrefix(next, "/\\") { + return "/account" + } + return next +} + +const sessionLifetime = 30 * 24 * time.Hour + +func (s *Server) startSession(q *request, personID int64) error { + tok, csrf := token.Random(32), token.Random(24) + if err := s.Store.CreateSession(s.ctx(q), tok, personID, csrf, time.Now().Add(sessionLifetime)); err != nil { + return err + } + http.SetCookie(q.w, &http.Cookie{Name: sessionCookie, Value: tok, Path: "/", HttpOnly: true, SameSite: http.SameSiteLaxMode, + Secure: strings.HasPrefix(s.PublicURL, "https://"), MaxAge: int(sessionLifetime.Seconds())}) + return nil +} + +func (s *Server) signout(q *request) error { + if q.token != "" { + s.Store.DeleteSession(s.ctx(q), q.token) + } + http.SetCookie(q.w, &http.Cookie{Name: sessionCookie, Value: "", Path: "/", MaxAge: -1, HttpOnly: true}) + return q.redirect("/", "Signed out.") +} diff --git a/internal/web/console.go b/internal/web/console.go new file mode 100644 index 0000000..33729ce --- /dev/null +++ b/internal/web/console.go @@ -0,0 +1,141 @@ +package web + +import ( + "errors" + "fmt" + "net/http" + "strconv" + "strings" + + "git.t5de.dev/t5de/t5de-hub/internal/store" +) + +// The moderation console: counts, who's online, people (search, link caps, bans, roles), and the audit log. + +func (s *Server) console(q *request) error { + counts, err := s.Store.Counts(s.ctx(q)) + if err != nil { + return err + } + log, err := s.Store.AuditLog(s.ctx(q), 50) + if err != nil { + return err + } + s.render(q, "console", map[string]any{"Counts": counts, "Connections": s.Realtime.Connections(), + "Online": s.visibleOnline(q), "Audit": log}) + return nil +} + +func (s *Server) consoleOnline(q *request) error { + s.fragment(q, "console", "console-online", map[string]any{"Online": s.visibleOnline(q), "Connections": s.Realtime.Connections()}) + return nil +} + +func (s *Server) consolePeople(q *request) error { + search := strings.TrimSpace(q.r.URL.Query().Get("q")) + people, err := s.Store.People(s.ctx(q), search, 100) + if err != nil { + return err + } + s.render(q, "console_people", map[string]any{"People": people, "Search": search}) + return nil +} + +func (s *Server) personParam(q *request) (*store.Person, error) { + id, err := strconv.ParseInt(q.r.PathValue("id"), 10, 64) + if err != nil { + return nil, fail(http.StatusNotFound, "No such person.") + } + p, err := s.Store.PersonByID(s.ctx(q), id) + if errors.Is(err, store.ErrNotFound) { + return nil, fail(http.StatusNotFound, "No such person.") + } + return p, err +} + +func (s *Server) consolePerson(q *request) error { + p, err := s.personParam(q) + if err != nil { + return err + } + links, err := s.Store.Links(s.ctx(q), p.ID) + if err != nil { + return err + } + signins, err := s.Store.Signins(s.ctx(q), p.ID) + if err != nil { + return err + } + s.render(q, "console_person", map[string]any{"Subject": p, "Links": links, "Signins": signins}) + return nil +} + +func (s *Server) setCap(q *request) error { + p, err := s.personParam(q) + if err != nil { + return err + } + cap, err := strconv.Atoi(q.r.PostFormValue("cap")) + if err != nil || cap < 0 || cap > 1000 { + return fail(http.StatusBadRequest, "The link cap is a number from 0 to 1000.") + } + if err := s.Store.SetLinkCap(s.ctx(q), p.ID, cap); err != nil { + return err + } + s.Store.Audit(s.ctx(q), q.person.ID, "person.link_cap", p.Username, map[string]any{"from": p.LinkCap, "to": cap}) + return q.redirect(fmt.Sprintf("/console/people/%d", p.ID), fmt.Sprintf("%s can link %d accounts.", p.Username, cap)) +} + +func (s *Server) ban(q *request) error { + p, err := s.personParam(q) + if err != nil { + return err + } + if p.ID == q.person.ID { + return fail(http.StatusBadRequest, "You can't ban yourself.") + } + if p.IsModerator() && !q.person.IsAdmin() { + return fail(http.StatusForbidden, "Only admins can ban moderators.") + } + reason := strings.TrimSpace(q.r.PostFormValue("reason")) + if reason == "" || len(reason) > 500 { + return fail(http.StatusBadRequest, "Give a reason (up to 500 characters); the person sees it.") + } + if err := s.Store.SetBanned(s.ctx(q), p.ID, true, reason); err != nil { + return err + } + s.Realtime.Disconnect(p.ID) + s.Store.Audit(s.ctx(q), q.person.ID, "person.ban", p.Username, map[string]any{"reason": reason}) + return q.redirect(fmt.Sprintf("/console/people/%d", p.ID), "Banned "+p.Username+".") +} + +func (s *Server) unban(q *request) error { + p, err := s.personParam(q) + if err != nil { + return err + } + if err := s.Store.SetBanned(s.ctx(q), p.ID, false, ""); err != nil { + return err + } + s.Store.Audit(s.ctx(q), q.person.ID, "person.unban", p.Username, nil) + return q.redirect(fmt.Sprintf("/console/people/%d", p.ID), "Unbanned "+p.Username+".") +} + +func (s *Server) setRole(q *request) error { + p, err := s.personParam(q) + if err != nil { + return err + } + role := q.r.PostFormValue("role") + if role != "user" && role != "moderator" && role != "admin" { + return fail(http.StatusBadRequest, "No such role.") + } + if p.ID == q.person.ID { + return fail(http.StatusBadRequest, "Another admin has to change your role.") + } + if err := s.Store.SetRole(s.ctx(q), p.ID, role); err != nil { + return err + } + s.Store.Audit(s.ctx(q), q.person.ID, "person.role", p.Username, map[string]any{"from": p.Role, "to": role}) + return q.redirect(fmt.Sprintf("/console/people/%d", p.ID), p.Username+" is now "+role+".") +} diff --git a/internal/web/render.go b/internal/web/render.go new file mode 100644 index 0000000..e5b8714 --- /dev/null +++ b/internal/web/render.go @@ -0,0 +1,82 @@ +package web + +import ( + "bytes" + "fmt" + "html/template" + "net/http" + "strings" + "time" +) + +// pageNames are the templates with a full page each; "layout" wraps them. Fragments (for htmx) are defined inside +// page files and rendered on their own. +var pageNames = []string{"home", "signin", "signup", "account", "device", "online", "console", "console_people", + "console_person", "error"} + +var funcs = template.FuncMap{ + "ago": func(t time.Time) string { + d := time.Since(t) + switch { + case d < time.Minute: + return "just now" + case d < time.Hour: + return fmt.Sprintf("%d min ago", int(d.Minutes())) + case d < 48*time.Hour: + return fmt.Sprintf("%d h ago", int(d.Hours())) + } + return t.Format("2 Jan 2006") + }, + "date": func(t time.Time) string { return t.Format("2 Jan 2006 15:04") }, + "upper": strings.ToUpper, +} + +func (s *Server) parseTemplates() error { + s.pages = map[string]*template.Template{} + for _, name := range pageNames { + t, err := template.New("layout.html").Funcs(funcs).ParseFS(templateFiles, "templates/layout.html", "templates/"+name+".html") + if err != nil { + return err + } + s.pages[name] = t + } + return nil +} + +// render shows a page in the layout. data is the page's own values; the layout gets the person and CSRF token. +func (s *Server) render(q *request, name string, data map[string]any) { + if data == nil { + data = map[string]any{} + } + data["Person"] = q.person + data["CSRF"] = q.csrf + data["Flash"] = q.flash + data["Page"] = name + var buf bytes.Buffer + if err := s.pages[name].ExecuteTemplate(&buf, "layout.html", data); err != nil { + s.Log.Printf("rendering %s: %v", name, err) + http.Error(q.w, "Something went wrong on our side.", http.StatusInternalServerError) + return + } + q.w.Header().Set("Content-Type", "text/html; charset=utf-8") + q.w.Header().Set("Cache-Control", "no-store") + buf.WriteTo(q.w) +} + +// fragment shows one template from a page's file, without the layout (htmx swaps it in). +func (s *Server) fragment(q *request, page, name string, data map[string]any) { + if data == nil { + data = map[string]any{} + } + data["Person"] = q.person + data["CSRF"] = q.csrf + var buf bytes.Buffer + if err := s.pages[page].ExecuteTemplate(&buf, name, data); err != nil { + s.Log.Printf("rendering %s/%s: %v", page, name, err) + http.Error(q.w, "Something went wrong on our side.", http.StatusInternalServerError) + return + } + q.w.Header().Set("Content-Type", "text/html; charset=utf-8") + q.w.Header().Set("Cache-Control", "no-store") + buf.WriteTo(q.w) +} diff --git a/internal/web/server.go b/internal/web/server.go new file mode 100644 index 0000000..9e1bae9 --- /dev/null +++ b/internal/web/server.go @@ -0,0 +1,267 @@ +// Package web is the hub's website (server-rendered pages, htmx for the live parts) and its API for proxies. +package web + +import ( + "context" + "crypto/subtle" + "embed" + "errors" + "html/template" + "io/fs" + "log" + "net" + "net/http" + "strings" + "sync" + "time" + + "git.t5de.dev/t5de/t5de-hub/internal/imvu" + "git.t5de.dev/t5de/t5de-hub/internal/realtime" + "git.t5de.dev/t5de/t5de-hub/internal/store" + "git.t5de.dev/t5de/t5de-hub/internal/token" +) + +//go:embed templates/*.html +var templateFiles embed.FS + +//go:embed static +var staticFiles embed.FS + +// Audience is the hub's own API, in its access tokens. +const Audience = "t5de-hub" + +// Server is the website and API. +type Server struct { + Store *store.Store + IMVU *imvu.Client + Keys *token.Keys + Realtime *realtime.Hub + PublicURL string // e.g. https://hub.t5de.dev, for links shown to proxies + Log *log.Logger + + pages map[string]*template.Template + limiter *limiter +} + +// Handler is the hub's HTTP handler. +func (s *Server) Handler() (http.Handler, error) { + if err := s.parseTemplates(); err != nil { + return nil, err + } + s.limiter = &limiter{hits: map[string][]time.Time{}} + mux := http.NewServeMux() + static, _ := fs.Sub(staticFiles, "static") + mux.Handle("GET /static/", http.StripPrefix("/static/", cacheStatic(http.FileServerFS(static)))) + + mux.HandleFunc("GET /favicon.ico", func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "/static/favicon.svg", http.StatusMovedPermanently) + }) + mux.HandleFunc("GET /{$}", s.page(s.home)) + mux.HandleFunc("GET /signup", s.page(s.signupForm)) + mux.HandleFunc("POST /signup", s.page(s.signup)) + mux.HandleFunc("GET /signin", s.page(s.signinForm)) + mux.HandleFunc("POST /signin", s.page(s.signin)) + mux.HandleFunc("POST /signout", s.page(s.signout)) + mux.HandleFunc("GET /online", s.page(s.online)) + mux.HandleFunc("GET /online/list", s.page(s.onlineList)) + + mux.HandleFunc("GET /account", s.page(s.signedIn(s.account))) + mux.HandleFunc("POST /account/link", s.page(s.signedIn(s.startLink))) + mux.HandleFunc("POST /account/link/{imvu}/check", s.page(s.signedIn(s.checkLink))) + mux.HandleFunc("POST /account/link/{imvu}/cancel", s.page(s.signedIn(s.cancelLink))) + mux.HandleFunc("POST /account/links/{imvu}/settings", s.page(s.signedIn(s.linkSettings))) + mux.HandleFunc("POST /account/links/{imvu}/recheck", s.page(s.signedIn(s.recheckLink))) + mux.HandleFunc("POST /account/links/{imvu}/unlink", s.page(s.signedIn(s.unlink))) + mux.HandleFunc("POST /account/proxies/signout", s.page(s.signedIn(s.signOutProxies))) + mux.HandleFunc("GET /device", s.page(s.signedIn(s.deviceForm))) + mux.HandleFunc("POST /device", s.page(s.signedIn(s.approveDevice))) + + mux.HandleFunc("GET /console", s.page(s.moderator(s.console))) + mux.HandleFunc("GET /console/online", s.page(s.moderator(s.consoleOnline))) + mux.HandleFunc("GET /console/people", s.page(s.moderator(s.consolePeople))) + mux.HandleFunc("GET /console/people/{id}", s.page(s.moderator(s.consolePerson))) + mux.HandleFunc("POST /console/people/{id}/cap", s.page(s.moderator(s.setCap))) + mux.HandleFunc("POST /console/people/{id}/ban", s.page(s.moderator(s.ban))) + mux.HandleFunc("POST /console/people/{id}/unban", s.page(s.moderator(s.unban))) + mux.HandleFunc("POST /console/people/{id}/role", s.page(s.admin(s.setRole))) + + mux.HandleFunc("GET /.well-known/jwks.json", s.jwks) + mux.HandleFunc("POST /api/v1/device/code", s.deviceCode) + mux.HandleFunc("POST /api/v1/device/token", s.deviceToken) + mux.HandleFunc("POST /api/v1/token/refresh", s.refresh) + mux.HandleFunc("GET /api/v1/me", s.me) + mux.HandleFunc("GET /api/v1/realtime", s.realtime) + return securityHeaders(mux), nil +} + +func securityHeaders(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := w.Header() + h.Set("Content-Security-Policy", "default-src 'self'; img-src 'self' https: data:; style-src 'self'; script-src 'self'; "+ + "connect-src 'self'; frame-ancestors 'none'; form-action 'self'; base-uri 'none'; object-src 'none'") + h.Set("X-Content-Type-Options", "nosniff") + h.Set("Referrer-Policy", "same-origin") + h.Set("X-Frame-Options", "DENY") + next.ServeHTTP(w, r) + }) +} + +func cacheStatic(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Cache-Control", "public, max-age=3600") + next.ServeHTTP(w, r) + }) +} + +// request is one page request: who's asking, and what the page shows. +type request struct { + w http.ResponseWriter + r *http.Request + person *store.Person // nil when signed out + csrf string + token string // session token + flash string +} + +type pageFunc func(q *request) error + +// errStatus is an error with the HTTP status to answer with, shown to the person. +type errStatus struct { + status int + message string +} + +func (e errStatus) Error() string { return e.message } + +func fail(status int, message string) error { return errStatus{status, message} } + +const sessionCookie = "t5de_session" + +// page wraps a page handler: it finds the session, checks CSRF on POSTs, and shows errors. +func (s *Server) page(fn pageFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + q := &request{w: w, r: r, flash: r.URL.Query().Get("done")} + if c, err := r.Cookie(sessionCookie); err == nil { + p, csrf, err := s.Store.Session(r.Context(), c.Value) + if err == nil && !p.Banned() { + q.person, q.csrf, q.token = p, csrf, c.Value + } + } + if r.Method == http.MethodPost { + r.Body = http.MaxBytesReader(w, r.Body, 64<<10) + sent := r.Header.Get("X-CSRF-Token") + if sent == "" { + sent = r.PostFormValue("csrf") + } + // Signed in, a form must carry the session's token. Signed out (sign-in and sign-up), browsers' same-site + // checks apply: the form must come from the hub itself. + if q.person != nil && subtle.ConstantTimeCompare([]byte(sent), []byte(q.csrf)) != 1 || + q.person == nil && !sameOrigin(r) { + s.showError(q, fail(http.StatusForbidden, "That form expired. Reload the page and try again.")) + return + } + } + if err := fn(q); err != nil { + s.showError(q, err) + } + } +} + +func sameOrigin(r *http.Request) bool { + if site := r.Header.Get("Sec-Fetch-Site"); site != "" { + return site == "same-origin" || site == "none" + } + origin := r.Header.Get("Origin") + return origin == "" || strings.TrimPrefix(strings.TrimPrefix(origin, "https://"), "http://") == r.Host +} + +func (s *Server) signedIn(fn pageFunc) pageFunc { + return func(q *request) error { + if q.person == nil { + http.Redirect(q.w, q.r, "/signin?next="+q.r.URL.RequestURI(), http.StatusSeeOther) + return nil + } + return fn(q) + } +} + +func (s *Server) moderator(fn pageFunc) pageFunc { + return s.signedIn(func(q *request) error { + if !q.person.IsModerator() { + return fail(http.StatusForbidden, "Only moderators can see this.") + } + return fn(q) + }) +} + +func (s *Server) admin(fn pageFunc) pageFunc { + return s.signedIn(func(q *request) error { + if !q.person.IsAdmin() { + return fail(http.StatusForbidden, "Only admins can do this.") + } + return fn(q) + }) +} + +func (s *Server) showError(q *request, err error) { + var e errStatus + if !errors.As(err, &e) { + s.Log.Printf("%s %s: %v", q.r.Method, q.r.URL.Path, err) + e = errStatus{http.StatusInternalServerError, "Something went wrong on our side. Try again in a moment."} + } + q.w.WriteHeader(e.status) + s.render(q, "error", map[string]any{"Message": e.message}) +} + +// redirect sends the browser on after a form, with a short note to show. +func (q *request) redirect(path, done string) error { + if done != "" { + sep := "?" + if strings.Contains(path, "?") { + sep = "&" + } + path += sep + "done=" + template.URLQueryEscaper(done) + } + http.Redirect(q.w, q.r, path, http.StatusSeeOther) + return nil +} + +func clientIP(r *http.Request) string { + if ip := r.Header.Get("CF-Connecting-IP"); ip != "" { + return ip + } + host, _, err := net.SplitHostPort(r.RemoteAddr) + if err != nil { + return r.RemoteAddr + } + return host +} + +// limiter allows a number of attempts per key in a window (sign-ins, sign-ups, IMVU lookups). +type limiter struct { + mu sync.Mutex + hits map[string][]time.Time +} + +func (l *limiter) allow(key string, n int, window time.Duration) bool { + l.mu.Lock() + defer l.mu.Unlock() + now := time.Now() + recent := l.hits[key][:0] + for _, t := range l.hits[key] { + if now.Sub(t) < window { + recent = append(recent, t) + } + } + if len(recent) >= n { + l.hits[key] = recent + return false + } + l.hits[key] = append(recent, now) + if len(l.hits) > 100000 { // forget everything rather than grow without end + l.hits = map[string][]time.Time{} + } + return true +} + +func (s *Server) ctx(q *request) context.Context { return q.r.Context() } diff --git a/internal/web/static/favicon.svg b/internal/web/static/favicon.svg new file mode 100644 index 0000000..dddb290 --- /dev/null +++ b/internal/web/static/favicon.svg @@ -0,0 +1 @@ +T5 diff --git a/internal/web/static/htmx.min.js b/internal/web/static/htmx.min.js new file mode 100644 index 0000000..59937d7 --- /dev/null +++ b/internal/web/static/htmx.min.js @@ -0,0 +1 @@ +var htmx=function(){"use strict";const Q={onLoad:null,process:null,on:null,off:null,trigger:null,ajax:null,find:null,findAll:null,closest:null,values:function(e,t){const n=cn(e,t||"post");return n.values},remove:null,addClass:null,removeClass:null,toggleClass:null,takeClass:null,swap:null,defineExtension:null,removeExtension:null,logAll:null,logNone:null,logger:null,config:{historyEnabled:true,historyCacheSize:10,refreshOnHistoryMiss:false,defaultSwapStyle:"innerHTML",defaultSwapDelay:0,defaultSettleDelay:20,includeIndicatorStyles:true,indicatorClass:"htmx-indicator",requestClass:"htmx-request",addedClass:"htmx-added",settlingClass:"htmx-settling",swappingClass:"htmx-swapping",allowEval:true,allowScriptTags:true,inlineScriptNonce:"",inlineStyleNonce:"",attributesToSettle:["class","style","width","height"],withCredentials:false,timeout:0,wsReconnectDelay:"full-jitter",wsBinaryType:"blob",disableSelector:"[hx-disable], [data-hx-disable]",scrollBehavior:"instant",defaultFocusScroll:false,getCacheBusterParam:false,globalViewTransitions:false,methodsThatUseUrlParams:["get","delete"],selfRequestsOnly:true,ignoreTitle:false,scrollIntoViewOnBoost:true,triggerSpecsCache:null,disableInheritance:false,responseHandling:[{code:"204",swap:false},{code:"[23]..",swap:true},{code:"[45]..",swap:false,error:true}],allowNestedOobSwaps:true},parseInterval:null,_:null,version:"2.0.4"};Q.onLoad=j;Q.process=kt;Q.on=ye;Q.off=be;Q.trigger=he;Q.ajax=Rn;Q.find=u;Q.findAll=x;Q.closest=g;Q.remove=z;Q.addClass=K;Q.removeClass=G;Q.toggleClass=W;Q.takeClass=Z;Q.swap=$e;Q.defineExtension=Fn;Q.removeExtension=Bn;Q.logAll=V;Q.logNone=_;Q.parseInterval=d;Q._=e;const n={addTriggerHandler:St,bodyContains:le,canAccessLocalStorage:B,findThisElement:Se,filterValues:hn,swap:$e,hasAttribute:s,getAttributeValue:te,getClosestAttributeValue:re,getClosestMatch:o,getExpressionVars:En,getHeaders:fn,getInputValues:cn,getInternalData:ie,getSwapSpecification:gn,getTriggerSpecs:st,getTarget:Ee,makeFragment:P,mergeObjects:ce,makeSettleInfo:xn,oobSwap:He,querySelectorExt:ae,settleImmediately:Kt,shouldCancel:ht,triggerEvent:he,triggerErrorEvent:fe,withExtensions:Ft};const r=["get","post","put","delete","patch"];const H=r.map(function(e){return"[hx-"+e+"], [data-hx-"+e+"]"}).join(", ");function d(e){if(e==undefined){return undefined}let t=NaN;if(e.slice(-2)=="ms"){t=parseFloat(e.slice(0,-2))}else if(e.slice(-1)=="s"){t=parseFloat(e.slice(0,-1))*1e3}else if(e.slice(-1)=="m"){t=parseFloat(e.slice(0,-1))*1e3*60}else{t=parseFloat(e)}return isNaN(t)?undefined:t}function ee(e,t){return e instanceof Element&&e.getAttribute(t)}function s(e,t){return!!e.hasAttribute&&(e.hasAttribute(t)||e.hasAttribute("data-"+t))}function te(e,t){return ee(e,t)||ee(e,"data-"+t)}function c(e){const t=e.parentElement;if(!t&&e.parentNode instanceof ShadowRoot)return e.parentNode;return t}function ne(){return document}function m(e,t){return e.getRootNode?e.getRootNode({composed:t}):ne()}function o(e,t){while(e&&!t(e)){e=c(e)}return e||null}function i(e,t,n){const r=te(t,n);const o=te(t,"hx-disinherit");var i=te(t,"hx-inherit");if(e!==t){if(Q.config.disableInheritance){if(i&&(i==="*"||i.split(" ").indexOf(n)>=0)){return r}else{return null}}if(o&&(o==="*"||o.split(" ").indexOf(n)>=0)){return"unset"}}return r}function re(t,n){let r=null;o(t,function(e){return!!(r=i(t,ue(e),n))});if(r!=="unset"){return r}}function h(e,t){const n=e instanceof Element&&(e.matches||e.matchesSelector||e.msMatchesSelector||e.mozMatchesSelector||e.webkitMatchesSelector||e.oMatchesSelector);return!!n&&n.call(e,t)}function T(e){const t=/<([a-z][^\/\0>\x20\t\r\n\f]*)/i;const n=t.exec(e);if(n){return n[1].toLowerCase()}else{return""}}function q(e){const t=new DOMParser;return t.parseFromString(e,"text/html")}function L(e,t){while(t.childNodes.length>0){e.append(t.childNodes[0])}}function A(e){const t=ne().createElement("script");se(e.attributes,function(e){t.setAttribute(e.name,e.value)});t.textContent=e.textContent;t.async=false;if(Q.config.inlineScriptNonce){t.nonce=Q.config.inlineScriptNonce}return t}function N(e){return e.matches("script")&&(e.type==="text/javascript"||e.type==="module"||e.type==="")}function I(e){Array.from(e.querySelectorAll("script")).forEach(e=>{if(N(e)){const t=A(e);const n=e.parentNode;try{n.insertBefore(t,e)}catch(e){O(e)}finally{e.remove()}}})}function P(e){const t=e.replace(/]*)?>[\s\S]*?<\/head>/i,"");const n=T(t);let r;if(n==="html"){r=new DocumentFragment;const i=q(e);L(r,i.body);r.title=i.title}else if(n==="body"){r=new DocumentFragment;const i=q(t);L(r,i.body);r.title=i.title}else{const i=q('");r=i.querySelector("template").content;r.title=i.title;var o=r.querySelector("title");if(o&&o.parentNode===r){o.remove();r.title=o.innerText}}if(r){if(Q.config.allowScriptTags){I(r)}else{r.querySelectorAll("script").forEach(e=>e.remove())}}return r}function oe(e){if(e){e()}}function t(e,t){return Object.prototype.toString.call(e)==="[object "+t+"]"}function k(e){return typeof e==="function"}function D(e){return t(e,"Object")}function ie(e){const t="htmx-internal-data";let n=e[t];if(!n){n=e[t]={}}return n}function M(t){const n=[];if(t){for(let e=0;e=0}function le(e){return e.getRootNode({composed:true})===document}function F(e){return e.trim().split(/\s+/)}function ce(e,t){for(const n in t){if(t.hasOwnProperty(n)){e[n]=t[n]}}return e}function S(e){try{return JSON.parse(e)}catch(e){O(e);return null}}function B(){const e="htmx:localStorageTest";try{localStorage.setItem(e,e);localStorage.removeItem(e);return true}catch(e){return false}}function U(t){try{const e=new URL(t);if(e){t=e.pathname+e.search}if(!/^\/$/.test(t)){t=t.replace(/\/+$/,"")}return t}catch(e){return t}}function e(e){return vn(ne().body,function(){return eval(e)})}function j(t){const e=Q.on("htmx:load",function(e){t(e.detail.elt)});return e}function V(){Q.logger=function(e,t,n){if(console){console.log(t,e,n)}}}function _(){Q.logger=null}function u(e,t){if(typeof e!=="string"){return e.querySelector(t)}else{return u(ne(),e)}}function x(e,t){if(typeof e!=="string"){return e.querySelectorAll(t)}else{return x(ne(),e)}}function E(){return window}function z(e,t){e=y(e);if(t){E().setTimeout(function(){z(e);e=null},t)}else{c(e).removeChild(e)}}function ue(e){return e instanceof Element?e:null}function $(e){return e instanceof HTMLElement?e:null}function J(e){return typeof e==="string"?e:null}function f(e){return e instanceof Element||e instanceof Document||e instanceof DocumentFragment?e:null}function K(e,t,n){e=ue(y(e));if(!e){return}if(n){E().setTimeout(function(){K(e,t);e=null},n)}else{e.classList&&e.classList.add(t)}}function G(e,t,n){let r=ue(y(e));if(!r){return}if(n){E().setTimeout(function(){G(r,t);r=null},n)}else{if(r.classList){r.classList.remove(t);if(r.classList.length===0){r.removeAttribute("class")}}}}function W(e,t){e=y(e);e.classList.toggle(t)}function Z(e,t){e=y(e);se(e.parentElement.children,function(e){G(e,t)});K(ue(e),t)}function g(e,t){e=ue(y(e));if(e&&e.closest){return e.closest(t)}else{do{if(e==null||h(e,t)){return e}}while(e=e&&ue(c(e)));return null}}function l(e,t){return e.substring(0,t.length)===t}function Y(e,t){return e.substring(e.length-t.length)===t}function ge(e){const t=e.trim();if(l(t,"<")&&Y(t,"/>")){return t.substring(1,t.length-2)}else{return t}}function p(t,r,n){if(r.indexOf("global ")===0){return p(t,r.slice(7),true)}t=y(t);const o=[];{let t=0;let n=0;for(let e=0;e"){t--}}if(n0){const r=ge(o.shift());let e;if(r.indexOf("closest ")===0){e=g(ue(t),ge(r.substr(8)))}else if(r.indexOf("find ")===0){e=u(f(t),ge(r.substr(5)))}else if(r==="next"||r==="nextElementSibling"){e=ue(t).nextElementSibling}else if(r.indexOf("next ")===0){e=pe(t,ge(r.substr(5)),!!n)}else if(r==="previous"||r==="previousElementSibling"){e=ue(t).previousElementSibling}else if(r.indexOf("previous ")===0){e=me(t,ge(r.substr(9)),!!n)}else if(r==="document"){e=document}else if(r==="window"){e=window}else if(r==="body"){e=document.body}else if(r==="root"){e=m(t,!!n)}else if(r==="host"){e=t.getRootNode().host}else{s.push(r)}if(e){i.push(e)}}if(s.length>0){const e=s.join(",");const c=f(m(t,!!n));i.push(...M(c.querySelectorAll(e)))}return i}var pe=function(t,e,n){const r=f(m(t,n)).querySelectorAll(e);for(let e=0;e=0;e--){const o=r[e];if(o.compareDocumentPosition(t)===Node.DOCUMENT_POSITION_FOLLOWING){return o}}};function ae(e,t){if(typeof e!=="string"){return p(e,t)[0]}else{return p(ne().body,e)[0]}}function y(e,t){if(typeof e==="string"){return u(f(t)||document,e)}else{return e}}function xe(e,t,n,r){if(k(t)){return{target:ne().body,event:J(e),listener:t,options:n}}else{return{target:y(e),event:J(t),listener:n,options:r}}}function ye(t,n,r,o){Vn(function(){const e=xe(t,n,r,o);e.target.addEventListener(e.event,e.listener,e.options)});const e=k(n);return e?n:r}function be(t,n,r){Vn(function(){const e=xe(t,n,r);e.target.removeEventListener(e.event,e.listener)});return k(n)?n:r}const ve=ne().createElement("output");function we(e,t){const n=re(e,t);if(n){if(n==="this"){return[Se(e,t)]}else{const r=p(e,n);if(r.length===0){O('The selector "'+n+'" on '+t+" returned no matches!");return[ve]}else{return r}}}}function Se(e,t){return ue(o(e,function(e){return te(ue(e),t)!=null}))}function Ee(e){const t=re(e,"hx-target");if(t){if(t==="this"){return Se(e,"hx-target")}else{return ae(e,t)}}else{const n=ie(e);if(n.boosted){return ne().body}else{return e}}}function Ce(t){const n=Q.config.attributesToSettle;for(let e=0;e0){s=e.substring(0,e.indexOf(":"));n=e.substring(e.indexOf(":")+1)}else{s=e}o.removeAttribute("hx-swap-oob");o.removeAttribute("data-hx-swap-oob");const r=p(t,n,false);if(r){se(r,function(e){let t;const n=o.cloneNode(true);t=ne().createDocumentFragment();t.appendChild(n);if(!Re(s,e)){t=f(n)}const r={shouldSwap:true,target:e,fragment:t};if(!he(e,"htmx:oobBeforeSwap",r))return;e=r.target;if(r.shouldSwap){qe(t);_e(s,e,e,t,i);Te()}se(i.elts,function(e){he(e,"htmx:oobAfterSwap",r)})});o.parentNode.removeChild(o)}else{o.parentNode.removeChild(o);fe(ne().body,"htmx:oobErrorNoTarget",{content:o})}return e}function Te(){const e=u("#--htmx-preserve-pantry--");if(e){for(const t of[...e.children]){const n=u("#"+t.id);n.parentNode.moveBefore(t,n);n.remove()}e.remove()}}function qe(e){se(x(e,"[hx-preserve], [data-hx-preserve]"),function(e){const t=te(e,"id");const n=ne().getElementById(t);if(n!=null){if(e.moveBefore){let e=u("#--htmx-preserve-pantry--");if(e==null){ne().body.insertAdjacentHTML("afterend","
");e=u("#--htmx-preserve-pantry--")}e.moveBefore(n,null)}else{e.parentNode.replaceChild(n,e)}}})}function Le(l,e,c){se(e.querySelectorAll("[id]"),function(t){const n=ee(t,"id");if(n&&n.length>0){const r=n.replace("'","\\'");const o=t.tagName.replace(":","\\:");const e=f(l);const i=e&&e.querySelector(o+"[id='"+r+"']");if(i&&i!==e){const s=t.cloneNode();Oe(t,i);c.tasks.push(function(){Oe(t,s)})}}})}function Ae(e){return function(){G(e,Q.config.addedClass);kt(ue(e));Ne(f(e));he(e,"htmx:load")}}function Ne(e){const t="[autofocus]";const n=$(h(e,t)?e:e.querySelector(t));if(n!=null){n.focus()}}function a(e,t,n,r){Le(e,n,r);while(n.childNodes.length>0){const o=n.firstChild;K(ue(o),Q.config.addedClass);e.insertBefore(o,t);if(o.nodeType!==Node.TEXT_NODE&&o.nodeType!==Node.COMMENT_NODE){r.tasks.push(Ae(o))}}}function Ie(e,t){let n=0;while(n0}function $e(e,t,r,o){if(!o){o={}}e=y(e);const i=o.contextElement?m(o.contextElement,false):ne();const n=document.activeElement;let s={};try{s={elt:n,start:n?n.selectionStart:null,end:n?n.selectionEnd:null}}catch(e){}const l=xn(e);if(r.swapStyle==="textContent"){e.textContent=t}else{let n=P(t);l.title=n.title;if(o.selectOOB){const u=o.selectOOB.split(",");for(let t=0;t0){E().setTimeout(c,r.settleDelay)}else{c()}}function Je(e,t,n){const r=e.getResponseHeader(t);if(r.indexOf("{")===0){const o=S(r);for(const i in o){if(o.hasOwnProperty(i)){let e=o[i];if(D(e)){n=e.target!==undefined?e.target:n}else{e={value:e}}he(n,i,e)}}}else{const s=r.split(",");for(let e=0;e0){const s=o[0];if(s==="]"){e--;if(e===0){if(n===null){t=t+"true"}o.shift();t+=")})";try{const l=vn(r,function(){return Function(t)()},function(){return true});l.source=t;return l}catch(e){fe(ne().body,"htmx:syntax:error",{error:e,source:t});return null}}}else if(s==="["){e++}if(tt(s,n,i)){t+="(("+i+"."+s+") ? ("+i+"."+s+") : (window."+s+"))"}else{t=t+s}n=o.shift()}}}function C(e,t){let n="";while(e.length>0&&!t.test(e[0])){n+=e.shift()}return n}function rt(e){let t;if(e.length>0&&Ye.test(e[0])){e.shift();t=C(e,Qe).trim();e.shift()}else{t=C(e,v)}return t}const ot="input, textarea, select";function it(e,t,n){const r=[];const o=et(t);do{C(o,w);const l=o.length;const c=C(o,/[,\[\s]/);if(c!==""){if(c==="every"){const u={trigger:"every"};C(o,w);u.pollInterval=d(C(o,/[,\[\s]/));C(o,w);var i=nt(e,o,"event");if(i){u.eventFilter=i}r.push(u)}else{const a={trigger:c};var i=nt(e,o,"event");if(i){a.eventFilter=i}C(o,w);while(o.length>0&&o[0]!==","){const f=o.shift();if(f==="changed"){a.changed=true}else if(f==="once"){a.once=true}else if(f==="consume"){a.consume=true}else if(f==="delay"&&o[0]===":"){o.shift();a.delay=d(C(o,v))}else if(f==="from"&&o[0]===":"){o.shift();if(Ye.test(o[0])){var s=rt(o)}else{var s=C(o,v);if(s==="closest"||s==="find"||s==="next"||s==="previous"){o.shift();const h=rt(o);if(h.length>0){s+=" "+h}}}a.from=s}else if(f==="target"&&o[0]===":"){o.shift();a.target=rt(o)}else if(f==="throttle"&&o[0]===":"){o.shift();a.throttle=d(C(o,v))}else if(f==="queue"&&o[0]===":"){o.shift();a.queue=C(o,v)}else if(f==="root"&&o[0]===":"){o.shift();a[f]=rt(o)}else if(f==="threshold"&&o[0]===":"){o.shift();a[f]=C(o,v)}else{fe(e,"htmx:syntax:error",{token:o.shift()})}C(o,w)}r.push(a)}}if(o.length===l){fe(e,"htmx:syntax:error",{token:o.shift()})}C(o,w)}while(o[0]===","&&o.shift());if(n){n[t]=r}return r}function st(e){const t=te(e,"hx-trigger");let n=[];if(t){const r=Q.config.triggerSpecsCache;n=r&&r[t]||it(e,t,r)}if(n.length>0){return n}else if(h(e,"form")){return[{trigger:"submit"}]}else if(h(e,'input[type="button"], input[type="submit"]')){return[{trigger:"click"}]}else if(h(e,ot)){return[{trigger:"change"}]}else{return[{trigger:"click"}]}}function lt(e){ie(e).cancelled=true}function ct(e,t,n){const r=ie(e);r.timeout=E().setTimeout(function(){if(le(e)&&r.cancelled!==true){if(!gt(n,e,Mt("hx:poll:trigger",{triggerSpec:n,target:e}))){t(e)}ct(e,t,n)}},n.pollInterval)}function ut(e){return location.hostname===e.hostname&&ee(e,"href")&&ee(e,"href").indexOf("#")!==0}function at(e){return g(e,Q.config.disableSelector)}function ft(t,n,e){if(t instanceof HTMLAnchorElement&&ut(t)&&(t.target===""||t.target==="_self")||t.tagName==="FORM"&&String(ee(t,"method")).toLowerCase()!=="dialog"){n.boosted=true;let r,o;if(t.tagName==="A"){r="get";o=ee(t,"href")}else{const i=ee(t,"method");r=i?i.toLowerCase():"get";o=ee(t,"action");if(o==null||o===""){o=ne().location.href}if(r==="get"&&o.includes("?")){o=o.replace(/\?[^#]+/,"")}}e.forEach(function(e){pt(t,function(e,t){const n=ue(e);if(at(n)){b(n);return}de(r,o,n,t)},n,e,true)})}}function ht(e,t){const n=ue(t);if(!n){return false}if(e.type==="submit"||e.type==="click"){if(n.tagName==="FORM"){return true}if(h(n,'input[type="submit"], button')&&(h(n,"[form]")||g(n,"form")!==null)){return true}if(n instanceof HTMLAnchorElement&&n.href&&(n.getAttribute("href")==="#"||n.getAttribute("href").indexOf("#")!==0)){return true}}return false}function dt(e,t){return ie(e).boosted&&e instanceof HTMLAnchorElement&&t.type==="click"&&(t.ctrlKey||t.metaKey)}function gt(e,t,n){const r=e.eventFilter;if(r){try{return r.call(t,n)!==true}catch(e){const o=r.source;fe(ne().body,"htmx:eventFilter:error",{error:e,source:o});return true}}return false}function pt(l,c,e,u,a){const f=ie(l);let t;if(u.from){t=p(l,u.from)}else{t=[l]}if(u.changed){if(!("lastValue"in f)){f.lastValue=new WeakMap}t.forEach(function(e){if(!f.lastValue.has(u)){f.lastValue.set(u,new WeakMap)}f.lastValue.get(u).set(e,e.value)})}se(t,function(i){const s=function(e){if(!le(l)){i.removeEventListener(u.trigger,s);return}if(dt(l,e)){return}if(a||ht(e,l)){e.preventDefault()}if(gt(u,l,e)){return}const t=ie(e);t.triggerSpec=u;if(t.handledFor==null){t.handledFor=[]}if(t.handledFor.indexOf(l)<0){t.handledFor.push(l);if(u.consume){e.stopPropagation()}if(u.target&&e.target){if(!h(ue(e.target),u.target)){return}}if(u.once){if(f.triggeredOnce){return}else{f.triggeredOnce=true}}if(u.changed){const n=event.target;const r=n.value;const o=f.lastValue.get(u);if(o.has(n)&&o.get(n)===r){return}o.set(n,r)}if(f.delayed){clearTimeout(f.delayed)}if(f.throttle){return}if(u.throttle>0){if(!f.throttle){he(l,"htmx:trigger");c(l,e);f.throttle=E().setTimeout(function(){f.throttle=null},u.throttle)}}else if(u.delay>0){f.delayed=E().setTimeout(function(){he(l,"htmx:trigger");c(l,e)},u.delay)}else{he(l,"htmx:trigger");c(l,e)}}};if(e.listenerInfos==null){e.listenerInfos=[]}e.listenerInfos.push({trigger:u.trigger,listener:s,on:i});i.addEventListener(u.trigger,s)})}let mt=false;let xt=null;function yt(){if(!xt){xt=function(){mt=true};window.addEventListener("scroll",xt);window.addEventListener("resize",xt);setInterval(function(){if(mt){mt=false;se(ne().querySelectorAll("[hx-trigger*='revealed'],[data-hx-trigger*='revealed']"),function(e){bt(e)})}},200)}}function bt(e){if(!s(e,"data-hx-revealed")&&X(e)){e.setAttribute("data-hx-revealed","true");const t=ie(e);if(t.initHash){he(e,"revealed")}else{e.addEventListener("htmx:afterProcessNode",function(){he(e,"revealed")},{once:true})}}}function vt(e,t,n,r){const o=function(){if(!n.loaded){n.loaded=true;he(e,"htmx:trigger");t(e)}};if(r>0){E().setTimeout(o,r)}else{o()}}function wt(t,n,e){let i=false;se(r,function(r){if(s(t,"hx-"+r)){const o=te(t,"hx-"+r);i=true;n.path=o;n.verb=r;e.forEach(function(e){St(t,e,n,function(e,t){const n=ue(e);if(g(n,Q.config.disableSelector)){b(n);return}de(r,o,n,t)})})}});return i}function St(r,e,t,n){if(e.trigger==="revealed"){yt();pt(r,n,t,e);bt(ue(r))}else if(e.trigger==="intersect"){const o={};if(e.root){o.root=ae(r,e.root)}if(e.threshold){o.threshold=parseFloat(e.threshold)}const i=new IntersectionObserver(function(t){for(let e=0;e0){t.polling=true;ct(ue(r),n,e)}else{pt(r,n,t,e)}}function Et(e){const t=ue(e);if(!t){return false}const n=t.attributes;for(let e=0;e", "+e).join(""));return o}else{return[]}}function Tt(e){const t=g(ue(e.target),"button, input[type='submit']");const n=Lt(e);if(n){n.lastButtonClicked=t}}function qt(e){const t=Lt(e);if(t){t.lastButtonClicked=null}}function Lt(e){const t=g(ue(e.target),"button, input[type='submit']");if(!t){return}const n=y("#"+ee(t,"form"),t.getRootNode())||g(t,"form");if(!n){return}return ie(n)}function At(e){e.addEventListener("click",Tt);e.addEventListener("focusin",Tt);e.addEventListener("focusout",qt)}function Nt(t,e,n){const r=ie(t);if(!Array.isArray(r.onHandlers)){r.onHandlers=[]}let o;const i=function(e){vn(t,function(){if(at(t)){return}if(!o){o=new Function("event",n)}o.call(t,e)})};t.addEventListener(e,i);r.onHandlers.push({event:e,listener:i})}function It(t){ke(t);for(let e=0;eQ.config.historyCacheSize){i.shift()}while(i.length>0){try{localStorage.setItem("htmx-history-cache",JSON.stringify(i));break}catch(e){fe(ne().body,"htmx:historyCacheError",{cause:e,cache:i});i.shift()}}}function Vt(t){if(!B()){return null}t=U(t);const n=S(localStorage.getItem("htmx-history-cache"))||[];for(let e=0;e=200&&this.status<400){he(ne().body,"htmx:historyCacheMissLoad",i);const e=P(this.response);const t=e.querySelector("[hx-history-elt],[data-hx-history-elt]")||e;const n=Ut();const r=xn(n);kn(e.title);qe(e);Ve(n,t,r);Te();Kt(r.tasks);Bt=o;he(ne().body,"htmx:historyRestore",{path:o,cacheMiss:true,serverResponse:this.response})}else{fe(ne().body,"htmx:historyCacheMissLoadError",i)}};e.send()}function Wt(e){zt();e=e||location.pathname+location.search;const t=Vt(e);if(t){const n=P(t.content);const r=Ut();const o=xn(r);kn(t.title);qe(n);Ve(r,n,o);Te();Kt(o.tasks);E().setTimeout(function(){window.scrollTo(0,t.scroll)},0);Bt=e;he(ne().body,"htmx:historyRestore",{path:e,item:t})}else{if(Q.config.refreshOnHistoryMiss){window.location.reload(true)}else{Gt(e)}}}function Zt(e){let t=we(e,"hx-indicator");if(t==null){t=[e]}se(t,function(e){const t=ie(e);t.requestCount=(t.requestCount||0)+1;e.classList.add.call(e.classList,Q.config.requestClass)});return t}function Yt(e){let t=we(e,"hx-disabled-elt");if(t==null){t=[]}se(t,function(e){const t=ie(e);t.requestCount=(t.requestCount||0)+1;e.setAttribute("disabled","");e.setAttribute("data-disabled-by-htmx","")});return t}function Qt(e,t){se(e.concat(t),function(e){const t=ie(e);t.requestCount=(t.requestCount||1)-1});se(e,function(e){const t=ie(e);if(t.requestCount===0){e.classList.remove.call(e.classList,Q.config.requestClass)}});se(t,function(e){const t=ie(e);if(t.requestCount===0){e.removeAttribute("disabled");e.removeAttribute("data-disabled-by-htmx")}})}function en(t,n){for(let e=0;en.indexOf(e)<0)}else{e=e.filter(e=>e!==n)}r.delete(t);se(e,e=>r.append(t,e))}}function on(t,n,r,o,i){if(o==null||en(t,o)){return}else{t.push(o)}if(tn(o)){const s=ee(o,"name");let e=o.value;if(o instanceof HTMLSelectElement&&o.multiple){e=M(o.querySelectorAll("option:checked")).map(function(e){return e.value})}if(o instanceof HTMLInputElement&&o.files){e=M(o.files)}nn(s,e,n);if(i){sn(o,r)}}if(o instanceof HTMLFormElement){se(o.elements,function(e){if(t.indexOf(e)>=0){rn(e.name,e.value,n)}else{t.push(e)}if(i){sn(e,r)}});new FormData(o).forEach(function(e,t){if(e instanceof File&&e.name===""){return}nn(t,e,n)})}}function sn(e,t){const n=e;if(n.willValidate){he(n,"htmx:validation:validate");if(!n.checkValidity()){t.push({elt:n,message:n.validationMessage,validity:n.validity});he(n,"htmx:validation:failed",{message:n.validationMessage,validity:n.validity})}}}function ln(n,e){for(const t of e.keys()){n.delete(t)}e.forEach(function(e,t){n.append(t,e)});return n}function cn(e,t){const n=[];const r=new FormData;const o=new FormData;const i=[];const s=ie(e);if(s.lastButtonClicked&&!le(s.lastButtonClicked)){s.lastButtonClicked=null}let l=e instanceof HTMLFormElement&&e.noValidate!==true||te(e,"hx-validate")==="true";if(s.lastButtonClicked){l=l&&s.lastButtonClicked.formNoValidate!==true}if(t!=="get"){on(n,o,i,g(e,"form"),l)}on(n,r,i,e,l);if(s.lastButtonClicked||e.tagName==="BUTTON"||e.tagName==="INPUT"&&ee(e,"type")==="submit"){const u=s.lastButtonClicked||e;const a=ee(u,"name");nn(a,u.value,o)}const c=we(e,"hx-include");se(c,function(e){on(n,r,i,ue(e),l);if(!h(e,"form")){se(f(e).querySelectorAll(ot),function(e){on(n,r,i,e,l)})}});ln(r,o);return{errors:i,formData:r,values:An(r)}}function un(e,t,n){if(e!==""){e+="&"}if(String(n)==="[object Object]"){n=JSON.stringify(n)}const r=encodeURIComponent(n);e+=encodeURIComponent(t)+"="+r;return e}function an(e){e=qn(e);let n="";e.forEach(function(e,t){n=un(n,t,e)});return n}function fn(e,t,n){const r={"HX-Request":"true","HX-Trigger":ee(e,"id"),"HX-Trigger-Name":ee(e,"name"),"HX-Target":te(t,"id"),"HX-Current-URL":ne().location.href};bn(e,"hx-headers",false,r);if(n!==undefined){r["HX-Prompt"]=n}if(ie(e).boosted){r["HX-Boosted"]="true"}return r}function hn(n,e){const t=re(e,"hx-params");if(t){if(t==="none"){return new FormData}else if(t==="*"){return n}else if(t.indexOf("not ")===0){se(t.slice(4).split(","),function(e){e=e.trim();n.delete(e)});return n}else{const r=new FormData;se(t.split(","),function(t){t=t.trim();if(n.has(t)){n.getAll(t).forEach(function(e){r.append(t,e)})}});return r}}else{return n}}function dn(e){return!!ee(e,"href")&&ee(e,"href").indexOf("#")>=0}function gn(e,t){const n=t||re(e,"hx-swap");const r={swapStyle:ie(e).boosted?"innerHTML":Q.config.defaultSwapStyle,swapDelay:Q.config.defaultSwapDelay,settleDelay:Q.config.defaultSettleDelay};if(Q.config.scrollIntoViewOnBoost&&ie(e).boosted&&!dn(e)){r.show="top"}if(n){const s=F(n);if(s.length>0){for(let e=0;e0?o.join(":"):null;r.scroll=u;r.scrollTarget=i}else if(l.indexOf("show:")===0){const a=l.slice(5);var o=a.split(":");const f=o.pop();var i=o.length>0?o.join(":"):null;r.show=f;r.showTarget=i}else if(l.indexOf("focus-scroll:")===0){const h=l.slice("focus-scroll:".length);r.focusScroll=h=="true"}else if(e==0){r.swapStyle=l}else{O("Unknown modifier in hx-swap: "+l)}}}}return r}function pn(e){return re(e,"hx-encoding")==="multipart/form-data"||h(e,"form")&&ee(e,"enctype")==="multipart/form-data"}function mn(t,n,r){let o=null;Ft(n,function(e){if(o==null){o=e.encodeParameters(t,r,n)}});if(o!=null){return o}else{if(pn(n)){return ln(new FormData,qn(r))}else{return an(r)}}}function xn(e){return{tasks:[],elts:[e]}}function yn(e,t){const n=e[0];const r=e[e.length-1];if(t.scroll){var o=null;if(t.scrollTarget){o=ue(ae(n,t.scrollTarget))}if(t.scroll==="top"&&(n||o)){o=o||n;o.scrollTop=0}if(t.scroll==="bottom"&&(r||o)){o=o||r;o.scrollTop=o.scrollHeight}}if(t.show){var o=null;if(t.showTarget){let e=t.showTarget;if(t.showTarget==="window"){e="body"}o=ue(ae(n,e))}if(t.show==="top"&&(n||o)){o=o||n;o.scrollIntoView({block:"start",behavior:Q.config.scrollBehavior})}if(t.show==="bottom"&&(r||o)){o=o||r;o.scrollIntoView({block:"end",behavior:Q.config.scrollBehavior})}}}function bn(r,e,o,i){if(i==null){i={}}if(r==null){return i}const s=te(r,e);if(s){let e=s.trim();let t=o;if(e==="unset"){return null}if(e.indexOf("javascript:")===0){e=e.slice(11);t=true}else if(e.indexOf("js:")===0){e=e.slice(3);t=true}if(e.indexOf("{")!==0){e="{"+e+"}"}let n;if(t){n=vn(r,function(){return Function("return ("+e+")")()},{})}else{n=S(e)}for(const l in n){if(n.hasOwnProperty(l)){if(i[l]==null){i[l]=n[l]}}}}return bn(ue(c(r)),e,o,i)}function vn(e,t,n){if(Q.config.allowEval){return t()}else{fe(e,"htmx:evalDisallowedError");return n}}function wn(e,t){return bn(e,"hx-vars",true,t)}function Sn(e,t){return bn(e,"hx-vals",false,t)}function En(e){return ce(wn(e),Sn(e))}function Cn(t,n,r){if(r!==null){try{t.setRequestHeader(n,r)}catch(e){t.setRequestHeader(n,encodeURIComponent(r));t.setRequestHeader(n+"-URI-AutoEncoded","true")}}}function On(t){if(t.responseURL&&typeof URL!=="undefined"){try{const e=new URL(t.responseURL);return e.pathname+e.search}catch(e){fe(ne().body,"htmx:badResponseUrl",{url:t.responseURL})}}}function R(e,t){return t.test(e.getAllResponseHeaders())}function Rn(t,n,r){t=t.toLowerCase();if(r){if(r instanceof Element||typeof r==="string"){return de(t,n,null,null,{targetOverride:y(r)||ve,returnPromise:true})}else{let e=y(r.target);if(r.target&&!e||r.source&&!e&&!y(r.source)){e=ve}return de(t,n,y(r.source),r.event,{handler:r.handler,headers:r.headers,values:r.values,targetOverride:e,swapOverride:r.swap,select:r.select,returnPromise:true})}}else{return de(t,n,null,null,{returnPromise:true})}}function Hn(e){const t=[];while(e){t.push(e);e=e.parentElement}return t}function Tn(e,t,n){let r;let o;if(typeof URL==="function"){o=new URL(t,document.location.href);const i=document.location.origin;r=i===o.origin}else{o=t;r=l(t,document.location.origin)}if(Q.config.selfRequestsOnly){if(!r){return false}}return he(e,"htmx:validateUrl",ce({url:o,sameHost:r},n))}function qn(e){if(e instanceof FormData)return e;const t=new FormData;for(const n in e){if(e.hasOwnProperty(n)){if(e[n]&&typeof e[n].forEach==="function"){e[n].forEach(function(e){t.append(n,e)})}else if(typeof e[n]==="object"&&!(e[n]instanceof Blob)){t.append(n,JSON.stringify(e[n]))}else{t.append(n,e[n])}}}return t}function Ln(r,o,e){return new Proxy(e,{get:function(t,e){if(typeof e==="number")return t[e];if(e==="length")return t.length;if(e==="push"){return function(e){t.push(e);r.append(o,e)}}if(typeof t[e]==="function"){return function(){t[e].apply(t,arguments);r.delete(o);t.forEach(function(e){r.append(o,e)})}}if(t[e]&&t[e].length===1){return t[e][0]}else{return t[e]}},set:function(e,t,n){e[t]=n;r.delete(o);e.forEach(function(e){r.append(o,e)});return true}})}function An(o){return new Proxy(o,{get:function(e,t){if(typeof t==="symbol"){const r=Reflect.get(e,t);if(typeof r==="function"){return function(){return r.apply(o,arguments)}}else{return r}}if(t==="toJSON"){return()=>Object.fromEntries(o)}if(t in e){if(typeof e[t]==="function"){return function(){return o[t].apply(o,arguments)}}else{return e[t]}}const n=o.getAll(t);if(n.length===0){return undefined}else if(n.length===1){return n[0]}else{return Ln(e,t,n)}},set:function(t,n,e){if(typeof n!=="string"){return false}t.delete(n);if(e&&typeof e.forEach==="function"){e.forEach(function(e){t.append(n,e)})}else if(typeof e==="object"&&!(e instanceof Blob)){t.append(n,JSON.stringify(e))}else{t.append(n,e)}return true},deleteProperty:function(e,t){if(typeof t==="string"){e.delete(t)}return true},ownKeys:function(e){return Reflect.ownKeys(Object.fromEntries(e))},getOwnPropertyDescriptor:function(e,t){return Reflect.getOwnPropertyDescriptor(Object.fromEntries(e),t)}})}function de(t,n,r,o,i,D){let s=null;let l=null;i=i!=null?i:{};if(i.returnPromise&&typeof Promise!=="undefined"){var e=new Promise(function(e,t){s=e;l=t})}if(r==null){r=ne().body}const M=i.handler||Dn;const X=i.select||null;if(!le(r)){oe(s);return e}const c=i.targetOverride||ue(Ee(r));if(c==null||c==ve){fe(r,"htmx:targetError",{target:te(r,"hx-target")});oe(l);return e}let u=ie(r);const a=u.lastButtonClicked;if(a){const L=ee(a,"formaction");if(L!=null){n=L}const A=ee(a,"formmethod");if(A!=null){if(A.toLowerCase()!=="dialog"){t=A}}}const f=re(r,"hx-confirm");if(D===undefined){const K=function(e){return de(t,n,r,o,i,!!e)};const G={target:c,elt:r,path:n,verb:t,triggeringEvent:o,etc:i,issueRequest:K,question:f};if(he(r,"htmx:confirm",G)===false){oe(s);return e}}let h=r;let d=re(r,"hx-sync");let g=null;let F=false;if(d){const N=d.split(":");const I=N[0].trim();if(I==="this"){h=Se(r,"hx-sync")}else{h=ue(ae(r,I))}d=(N[1]||"drop").trim();u=ie(h);if(d==="drop"&&u.xhr&&u.abortable!==true){oe(s);return e}else if(d==="abort"){if(u.xhr){oe(s);return e}else{F=true}}else if(d==="replace"){he(h,"htmx:abort")}else if(d.indexOf("queue")===0){const W=d.split(" ");g=(W[1]||"last").trim()}}if(u.xhr){if(u.abortable){he(h,"htmx:abort")}else{if(g==null){if(o){const P=ie(o);if(P&&P.triggerSpec&&P.triggerSpec.queue){g=P.triggerSpec.queue}}if(g==null){g="last"}}if(u.queuedRequests==null){u.queuedRequests=[]}if(g==="first"&&u.queuedRequests.length===0){u.queuedRequests.push(function(){de(t,n,r,o,i)})}else if(g==="all"){u.queuedRequests.push(function(){de(t,n,r,o,i)})}else if(g==="last"){u.queuedRequests=[];u.queuedRequests.push(function(){de(t,n,r,o,i)})}oe(s);return e}}const p=new XMLHttpRequest;u.xhr=p;u.abortable=F;const m=function(){u.xhr=null;u.abortable=false;if(u.queuedRequests!=null&&u.queuedRequests.length>0){const e=u.queuedRequests.shift();e()}};const B=re(r,"hx-prompt");if(B){var x=prompt(B);if(x===null||!he(r,"htmx:prompt",{prompt:x,target:c})){oe(s);m();return e}}if(f&&!D){if(!confirm(f)){oe(s);m();return e}}let y=fn(r,c,x);if(t!=="get"&&!pn(r)){y["Content-Type"]="application/x-www-form-urlencoded"}if(i.headers){y=ce(y,i.headers)}const U=cn(r,t);let b=U.errors;const j=U.formData;if(i.values){ln(j,qn(i.values))}const V=qn(En(r));const v=ln(j,V);let w=hn(v,r);if(Q.config.getCacheBusterParam&&t==="get"){w.set("org.htmx.cache-buster",ee(c,"id")||"true")}if(n==null||n===""){n=ne().location.href}const S=bn(r,"hx-request");const _=ie(r).boosted;let E=Q.config.methodsThatUseUrlParams.indexOf(t)>=0;const C={boosted:_,useUrlParams:E,formData:w,parameters:An(w),unfilteredFormData:v,unfilteredParameters:An(v),headers:y,target:c,verb:t,errors:b,withCredentials:i.credentials||S.credentials||Q.config.withCredentials,timeout:i.timeout||S.timeout||Q.config.timeout,path:n,triggeringEvent:o};if(!he(r,"htmx:configRequest",C)){oe(s);m();return e}n=C.path;t=C.verb;y=C.headers;w=qn(C.parameters);b=C.errors;E=C.useUrlParams;if(b&&b.length>0){he(r,"htmx:validation:halted",C);oe(s);m();return e}const z=n.split("#");const $=z[0];const O=z[1];let R=n;if(E){R=$;const Z=!w.keys().next().done;if(Z){if(R.indexOf("?")<0){R+="?"}else{R+="&"}R+=an(w);if(O){R+="#"+O}}}if(!Tn(r,R,C)){fe(r,"htmx:invalidPath",C);oe(l);return e}p.open(t.toUpperCase(),R,true);p.overrideMimeType("text/html");p.withCredentials=C.withCredentials;p.timeout=C.timeout;if(S.noHeaders){}else{for(const k in y){if(y.hasOwnProperty(k)){const Y=y[k];Cn(p,k,Y)}}}const H={xhr:p,target:c,requestConfig:C,etc:i,boosted:_,select:X,pathInfo:{requestPath:n,finalRequestPath:R,responsePath:null,anchor:O}};p.onload=function(){try{const t=Hn(r);H.pathInfo.responsePath=On(p);M(r,H);if(H.keepIndicators!==true){Qt(T,q)}he(r,"htmx:afterRequest",H);he(r,"htmx:afterOnLoad",H);if(!le(r)){let e=null;while(t.length>0&&e==null){const n=t.shift();if(le(n)){e=n}}if(e){he(e,"htmx:afterRequest",H);he(e,"htmx:afterOnLoad",H)}}oe(s);m()}catch(e){fe(r,"htmx:onLoadError",ce({error:e},H));throw e}};p.onerror=function(){Qt(T,q);fe(r,"htmx:afterRequest",H);fe(r,"htmx:sendError",H);oe(l);m()};p.onabort=function(){Qt(T,q);fe(r,"htmx:afterRequest",H);fe(r,"htmx:sendAbort",H);oe(l);m()};p.ontimeout=function(){Qt(T,q);fe(r,"htmx:afterRequest",H);fe(r,"htmx:timeout",H);oe(l);m()};if(!he(r,"htmx:beforeRequest",H)){oe(s);m();return e}var T=Zt(r);var q=Yt(r);se(["loadstart","loadend","progress","abort"],function(t){se([p,p.upload],function(e){e.addEventListener(t,function(e){he(r,"htmx:xhr:"+t,{lengthComputable:e.lengthComputable,loaded:e.loaded,total:e.total})})})});he(r,"htmx:beforeSend",H);const J=E?null:mn(p,r,w);p.send(J);return e}function Nn(e,t){const n=t.xhr;let r=null;let o=null;if(R(n,/HX-Push:/i)){r=n.getResponseHeader("HX-Push");o="push"}else if(R(n,/HX-Push-Url:/i)){r=n.getResponseHeader("HX-Push-Url");o="push"}else if(R(n,/HX-Replace-Url:/i)){r=n.getResponseHeader("HX-Replace-Url");o="replace"}if(r){if(r==="false"){return{}}else{return{type:o,path:r}}}const i=t.pathInfo.finalRequestPath;const s=t.pathInfo.responsePath;const l=re(e,"hx-push-url");const c=re(e,"hx-replace-url");const u=ie(e).boosted;let a=null;let f=null;if(l){a="push";f=l}else if(c){a="replace";f=c}else if(u){a="push";f=s||i}if(f){if(f==="false"){return{}}if(f==="true"){f=s||i}if(t.pathInfo.anchor&&f.indexOf("#")===-1){f=f+"#"+t.pathInfo.anchor}return{type:a,path:f}}else{return{}}}function In(e,t){var n=new RegExp(e.code);return n.test(t.toString(10))}function Pn(e){for(var t=0;t0){E().setTimeout(e,x.swapDelay)}else{e()}}if(f){fe(o,"htmx:responseError",ce({error:"Response Status Error Code "+s.status+" from "+i.pathInfo.requestPath},i))}}const Mn={};function Xn(){return{init:function(e){return null},getSelectors:function(){return null},onEvent:function(e,t){return true},transformResponse:function(e,t,n){return e},isInlineSwap:function(e){return false},handleSwap:function(e,t,n,r){return false},encodeParameters:function(e,t,n){return null}}}function Fn(e,t){if(t.init){t.init(n)}Mn[e]=ce(Xn(),t)}function Bn(e){delete Mn[e]}function Un(e,n,r){if(n==undefined){n=[]}if(e==undefined){return n}if(r==undefined){r=[]}const t=te(e,"hx-ext");if(t){se(t.split(","),function(e){e=e.replace(/ /g,"");if(e.slice(0,7)=="ignore:"){r.push(e.slice(7));return}if(r.indexOf(e)<0){const t=Mn[e];if(t&&n.indexOf(t)<0){n.push(t)}}})}return Un(ue(c(e)),n,r)}var jn=false;ne().addEventListener("DOMContentLoaded",function(){jn=true});function Vn(e){if(jn||ne().readyState==="complete"){e()}else{ne().addEventListener("DOMContentLoaded",e)}}function _n(){if(Q.config.includeIndicatorStyles!==false){const e=Q.config.inlineStyleNonce?` nonce="${Q.config.inlineStyleNonce}"`:"";ne().head.insertAdjacentHTML("beforeend"," ."+Q.config.indicatorClass+"{opacity:0} ."+Q.config.requestClass+" ."+Q.config.indicatorClass+"{opacity:1; transition: opacity 200ms ease-in;} ."+Q.config.requestClass+"."+Q.config.indicatorClass+"{opacity:1; transition: opacity 200ms ease-in;} ")}}function zn(){const e=ne().querySelector('meta[name="htmx-config"]');if(e){return S(e.content)}else{return null}}function $n(){const e=zn();if(e){Q.config=ce(Q.config,e)}}Vn(function(){$n();_n();let e=ne().body;kt(e);const t=ne().querySelectorAll("[hx-trigger='restored'],[data-hx-trigger='restored']");e.addEventListener("htmx:abort",function(e){const t=e.target;const n=ie(t);if(n&&n.xhr){n.xhr.abort()}});const n=window.onpopstate?window.onpopstate.bind(window):null;window.onpopstate=function(e){if(e.state&&e.state.htmx){Wt();se(t,function(e){he(e,"htmx:restored",{document:ne(),triggerEvent:he})})}else{if(n){n(e)}}};E().setTimeout(function(){he(e,"htmx:load",{});e=null},0)});return Q}(); \ No newline at end of file diff --git a/internal/web/static/style.css b/internal/web/static/style.css new file mode 100644 index 0000000..8770a0b --- /dev/null +++ b/internal/web/static/style.css @@ -0,0 +1,173 @@ +/* T5DE Hub: IMVU Classic's dialog palette (dark grays, gold buttons with black text), always dark. */ + +:root { + --bg: #262626; + --panel: #383838; + --panel-2: #303030; + --line: #4a4a4a; + --text: #ededed; + --muted: #a8a8a8; + --gold: #ffd200; + --gold-light: #ffe795; + --green: #6fd36f; + --red: #ff7b72; + --blue: #7cc4ff; + --radius: 8px; + color-scheme: dark; +} + +* { box-sizing: border-box; } + +html { background: var(--bg); } + +body { + margin: 0; + color: var(--text); + font: 15px/1.5 "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif; +} + +a { color: var(--gold); text-decoration: none; } +a:hover { text-decoration: underline; } + +.wrap { max-width: 960px; margin: 0 auto; padding: 0 20px; } + +/* Header */ +.top { + background: linear-gradient(#3a3a3a, #2c2c2c); + border-bottom: 1px solid #000; + box-shadow: 0 1px 0 #4a4a4a inset, 0 2px 8px rgba(0, 0, 0, .4); +} +.top .wrap { display: flex; align-items: center; gap: 24px; height: 56px; } +.brand { color: var(--text); font-weight: 600; font-size: 17px; display: flex; align-items: center; gap: 8px; } +.brand:hover { text-decoration: none; } +.logo { + background: linear-gradient(var(--gold-light), var(--gold)); + color: #000; font-weight: 800; letter-spacing: .5px; + padding: 2px 8px; border-radius: 5px; font-size: 15px; +} +nav { display: flex; gap: 4px; align-items: center; flex: 1; } +nav a { color: var(--text); padding: 6px 12px; border-radius: 6px; } +nav a:hover, nav a.here { background: #4a4a4a; text-decoration: none; } +nav .right { margin-left: auto; display: flex; gap: 8px; align-items: center; } +nav form { margin: 0; } + +main { padding-top: 28px; padding-bottom: 40px; min-height: 70vh; } +footer { padding: 24px 20px 40px; font-size: 13px; } + +h1 { font-size: 26px; margin: 0 0 6px; font-weight: 600; } +h2 { font-size: 18px; margin: 0 0 12px; font-weight: 600; } +p.lead { color: var(--muted); margin: 0 0 24px; font-size: 16px; } +.muted { color: var(--muted); } +.small { font-size: 13px; } + +/* Panels */ +.panel { + background: var(--panel); + border: 1px solid var(--line); + border-radius: var(--radius); + padding: 20px; + margin-bottom: 20px; + box-shadow: 0 2px 10px rgba(0, 0, 0, .25); +} +.panel.narrow { max-width: 420px; margin-left: auto; margin-right: auto; } +.panel h2 .count { color: var(--muted); font-weight: 400; font-size: 14px; margin-left: 6px; } +.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 20px; } +.stat { font-size: 30px; font-weight: 600; } + +/* Buttons and forms */ +.button, button { + display: inline-block; + font: inherit; font-weight: 600; font-size: 14px; + padding: 7px 16px; + border-radius: 6px; + border: 1px solid #b38f00; + background: linear-gradient(var(--gold-light), var(--gold)); + color: #000; + cursor: pointer; +} +.button:hover, button:hover { filter: brightness(1.06); text-decoration: none; } +button.secondary, .button.secondary { + background: linear-gradient(#616161, #3e3e3e); + border-color: #2a2a2a; + color: var(--text); +} +button.danger { background: linear-gradient(#b8443c, #8c2a24); border-color: #5c1a16; color: #fff; } +button.link { background: none; border: none; color: var(--gold); padding: 0; font-weight: 400; } + +label { display: block; font-size: 13px; color: var(--muted); margin: 14px 0 4px; } +input[type=text], input[type=password], input[type=number], input[type=search], textarea, select { + width: 100%; + font: inherit; + padding: 8px 10px; + color: var(--text); + background: #262626; + border: 1px solid #555; + border-radius: 6px; +} +input:focus, textarea:focus, select:focus { outline: 2px solid var(--gold); outline-offset: -1px; border-color: transparent; } +.inline { display: flex; gap: 8px; align-items: center; } +.inline input { flex: 1; } +.actions { display: flex; gap: 8px; margin-top: 18px; align-items: center; flex-wrap: wrap; } +.check { display: flex; gap: 8px; align-items: center; color: var(--text); font-size: 14px; margin: 8px 0; } + +/* Messages */ +.flash, .error { + padding: 10px 14px; border-radius: 6px; margin-bottom: 20px; +} +.flash { background: #2f3d2f; border: 1px solid #4b6b4b; } +.error { background: #4a2a28; border: 1px solid #7a3b36; } + +/* Linked accounts */ +.account-row { + display: flex; gap: 16px; align-items: flex-start; + padding: 16px 0; border-top: 1px solid var(--line); +} +.account-row:first-of-type { border-top: none; padding-top: 4px; } +.avatar { + width: 44px; height: 44px; border-radius: 50%; flex: none; + background: linear-gradient(135deg, #5a5a5a, #3a3a3a); + display: flex; align-items: center; justify-content: center; + font-weight: 700; color: var(--gold); + border: 1px solid #222; +} +.account-main { flex: 1; min-width: 0; } +.account-name { font-weight: 600; font-size: 16px; } +.tags { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0; } +.tag { + font-size: 12px; padding: 1px 8px; border-radius: 999px; + background: #2a2a2a; border: 1px solid #555; color: var(--muted); +} +.tag.ok { color: var(--green); border-color: #3f6b3f; } +.tag.adult { color: var(--blue); border-color: #3b5f7a; } +.tag.online { color: #000; background: var(--green); border-color: var(--green); } +.tag.gold { color: #000; background: var(--gold); border-color: var(--gold); } +.tag.warn { color: var(--red); border-color: #7a3b36; } +.settings { display: flex; gap: 18px; flex-wrap: wrap; align-items: center; margin-top: 4px; } +.settings form { display: flex; gap: 18px; align-items: center; flex-wrap: wrap; margin: 0; } +.row-actions { display: flex; gap: 8px; flex: none; } +.row-actions form { margin: 0; } + +.code { + font: 600 22px/1.2 ui-monospace, "Cascadia Mono", Consolas, monospace; + letter-spacing: 1px; + background: #222; border: 1px dashed var(--gold); color: var(--gold); + padding: 10px 14px; border-radius: 6px; display: inline-block; margin: 6px 0; + user-select: all; +} +ol.steps { padding-left: 20px; margin: 8px 0 0; } +ol.steps li { margin: 6px 0; } + +.outfit { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; } +.item { font-size: 12px; padding: 2px 8px; border-radius: 4px; background: #2a2a2a; border: 1px solid #555; } + +/* Tables */ +table { width: 100%; border-collapse: collapse; font-size: 14px; } +th { text-align: left; color: var(--muted); font-weight: 400; padding: 6px 8px; border-bottom: 1px solid var(--line); } +td { padding: 8px; border-bottom: 1px solid #424242; vertical-align: top; } +tr:last-child td { border-bottom: none; } + +.empty { color: var(--muted); padding: 12px 0; } + +.hero { text-align: center; padding: 30px 0 34px; } +.hero h1 { font-size: 34px; } +.hero .actions { justify-content: center; } diff --git a/internal/web/templates/account.html b/internal/web/templates/account.html new file mode 100644 index 0000000..e328789 --- /dev/null +++ b/internal/web/templates/account.html @@ -0,0 +1,82 @@ +{{define "title"}}Account{{end}} +{{define "content"}} +

Your account

+

Signed in as {{.Person.Username}}{{if .Person.IsModerator}} {{.Person.Role}}{{end}}

+{{if .Error}}
{{.Error}}
{{end}} + +
+

IMVU accounts {{len .Links}} of {{.Person.LinkCap}}

+ {{range .Links}} + + {{else}} +

No IMVU accounts linked yet.

+ {{end}} +
+ +{{range .Challenges}} + +{{end}} + +{{if .CanLink}} +
+

Link an IMVU account

+
+ + + +
+

Other people can't see which IMVU accounts are yours. Age-verified IMVU accounts (or with Access Pass) + can see adult community products; each account is checked on its own.

+
+{{else}} +

You've linked as many IMVU accounts as you can ({{.Person.LinkCap}}). Ask a moderator if you need more.

+{{end}} + +
+

T5DE on your computers

+

{{if .Signins}}Signed in on {{.Signins}} computer{{if ne .Signins 1}}s{{end}}.{{else}}Not signed in on any computer yet.{{end}} + To sign T5DE in, it shows a code to enter at /device.

+ {{if .Signins}} +
+ {{end}} +
+{{end}} diff --git a/internal/web/templates/console.html b/internal/web/templates/console.html new file mode 100644 index 0000000..f33b0f1 --- /dev/null +++ b/internal/web/templates/console.html @@ -0,0 +1,45 @@ +{{define "title"}}Console{{end}} +{{define "content"}} +

Console

+

Moderation. People

+
+

People

{{.Counts.People}}

accounts

+

IMVU accounts

{{.Counts.Links}}

linked

+

Computers

{{.Counts.Signins}}

signed in

+
+
+ {{template "console-online" .}} +
+
+

Audit log

+ + + {{range .Audit}} + + + {{else}} + + {{end}} +
WhenWhoWhatTargetDetail
{{date .At}}{{if .Actor}}{{.Actor}}{{else}}hub{{end}}{{.Action}}{{.Target}}{{range $k, $v := .Detail}}{{$k}}: {{$v}} {{end}}
Nothing yet.
+
+{{end}} + +{{define "console-online"}} +

Online now {{len .Online}} IMVU accounts, {{.Connections}} connections

+{{range .Online}} + +{{else}} +

Nobody.

+{{end}} +{{end}} diff --git a/internal/web/templates/console_people.html b/internal/web/templates/console_people.html new file mode 100644 index 0000000..bdd5735 --- /dev/null +++ b/internal/web/templates/console_people.html @@ -0,0 +1,25 @@ +{{define "title"}}People{{end}} +{{define "content"}} +

People

+

Console

+
+
+ + +
+
+
+ + + {{range .People}} + + + + + + {{else}} + + {{end}} +
UsernameRoleLink capJoined
{{.Username}}{{.Role}}{{.LinkCap}}{{date .CreatedAt}}{{if .Banned}}banned{{end}}
Nobody found.
+
+{{end}} diff --git a/internal/web/templates/console_person.html b/internal/web/templates/console_person.html new file mode 100644 index 0000000..c5dcdd4 --- /dev/null +++ b/internal/web/templates/console_person.html @@ -0,0 +1,61 @@ +{{define "title"}}{{.Subject.Username}}{{end}} +{{define "content"}} +

{{.Subject.Username}}

+

People · {{.Subject.Role}} · joined {{date .Subject.CreatedAt}} + {{if .Subject.Banned}}banned: {{.Subject.BanReason}}{{end}}

+ +
+

IMVU accounts {{len .Links}} of {{.Subject.LinkCap}}

+

Only moderators see which IMVU accounts belong to one person.

+ + + {{range .Links}} + + + + {{else}} + + {{end}} +
IMVU accountIDLinkedAgeShows
{{.Username}}{{.IMVUID}}{{date .LinkedAt}}{{if .AgeVerified}}verified{{else if .AccessPass}}Access Pass{{else}}no{{end}}{{if .ShowBadge}}badge {{end}}{{if .ShowPresence}}presence{{end}}
None.
+

Signed in on {{.Signins}} computer{{if ne .Signins 1}}s{{end}}.

+
+ +
+
+

Link cap

+
+ + + +
+

How many IMVU accounts they can link (normally 10).

+
+
+

Ban

+ {{if .Subject.Banned}} +
+ {{else}} +
+ + +
+
+

Covers all their IMVU accounts, and signs them out everywhere.

+ {{end}} +
+ {{if .Person.IsAdmin}} +
+

Role

+
+ + + +
+
+ {{end}} +
+{{end}} diff --git a/internal/web/templates/device.html b/internal/web/templates/device.html new file mode 100644 index 0000000..c49824d --- /dev/null +++ b/internal/web/templates/device.html @@ -0,0 +1,20 @@ +{{define "title"}}Sign T5DE in{{end}} +{{define "content"}} +
+ {{if .Approved}} +

T5DE is signed in

+

You can close this page and go back to T5DE.

+ {{else}} +

Sign T5DE in

+

Enter the code T5DE shows, to sign it in as {{.Person.Username}}.

+ {{if .Error}}
{{.Error}}
{{end}} +
+ + + +
+
+

Only enter a code you got from T5DE yourself.

+ {{end}} +
+{{end}} diff --git a/internal/web/templates/error.html b/internal/web/templates/error.html new file mode 100644 index 0000000..6146096 --- /dev/null +++ b/internal/web/templates/error.html @@ -0,0 +1,8 @@ +{{define "title"}}Sorry{{end}} +{{define "content"}} +
+

Sorry

+

{{.Message}}

+ +
+{{end}} diff --git a/internal/web/templates/home.html b/internal/web/templates/home.html new file mode 100644 index 0000000..a57e15f --- /dev/null +++ b/internal/web/templates/home.html @@ -0,0 +1,33 @@ +{{define "title"}}Home{{end}} +{{define "content"}} +
+

T5DE Hub

+

Your T5DE account: link your IMVU accounts, see who's online in T5DE, and soon, community products.

+ {{if not .Person}} + + {{end}} +
+
+
+

Online in T5DE

+
{{.OnlineCount}}
+

people showing that they're online. See who

+
+ {{if .Person}} +
+

Your IMVU accounts

+
{{len .Links}}
+

linked. Manage

+
+ {{else}} +
+

One account, all your avatars

+

Link any of your IMVU accounts by putting a code in its tagline. Your accounts stay private: + nobody else can see they're yours.

+
+ {{end}} +
+{{end}} diff --git a/internal/web/templates/layout.html b/internal/web/templates/layout.html new file mode 100644 index 0000000..be524f2 --- /dev/null +++ b/internal/web/templates/layout.html @@ -0,0 +1,40 @@ + + + + + +{{template "title" .}} · T5DE Hub + + + + + + +
+
+ Hub + +
+
+
+ {{if .Flash}}
{{.Flash}}
{{end}} + {{template "content" .}} +
+
T5DE is unofficial: not made by or affiliated with IMVU.
+ + diff --git a/internal/web/templates/online.html b/internal/web/templates/online.html new file mode 100644 index 0000000..10f7364 --- /dev/null +++ b/internal/web/templates/online.html @@ -0,0 +1,27 @@ +{{define "title"}}Online{{end}} +{{define "content"}} +

Online in T5DE

+

IMVU accounts in a T5DE client right now, that chose to show it.

+
+ {{template "online-list" .}} +
+{{end}} + +{{define "online-list"}} +{{range .Online}} + +{{else}} +

Nobody is showing that they're online right now.

+{{end}} +{{end}} diff --git a/internal/web/templates/signin.html b/internal/web/templates/signin.html new file mode 100644 index 0000000..71090cc --- /dev/null +++ b/internal/web/templates/signin.html @@ -0,0 +1,19 @@ +{{define "title"}}Sign in{{end}} +{{define "content"}} +
+

Sign in

+

to your T5DE account.

+ {{if .Error}}
{{.Error}}
{{end}} +
+ + + + + +
+ + No account? Create one +
+
+
+{{end}} diff --git a/internal/web/templates/signup.html b/internal/web/templates/signup.html new file mode 100644 index 0000000..d989af0 --- /dev/null +++ b/internal/web/templates/signup.html @@ -0,0 +1,22 @@ +{{define "title"}}Create account{{end}} +{{define "content"}} +
+

Create account

+

One T5DE account for all your IMVU accounts.

+ {{if .Error}}
{{.Error}}
{{end}} +
+ + + + + + +

Not your IMVU password: use a new one.

+
+ + Have one? Sign in +
+
+
+{{end}} diff --git a/internal/web/web_test.go b/internal/web/web_test.go new file mode 100644 index 0000000..2a861b1 --- /dev/null +++ b/internal/web/web_test.go @@ -0,0 +1,409 @@ +package web + +import ( + "context" + "encoding/json" + "fmt" + "io" + "log" + "net/http" + "net/http/cookiejar" + "net/http/httptest" + "net/url" + "os" + "path/filepath" + "regexp" + "strings" + "sync" + "testing" + "time" + + "github.com/coder/websocket" + embeddedpostgres "github.com/fergusstrange/embedded-postgres" + "github.com/jackc/pgx/v5" + + "git.t5de.dev/t5de/t5de-hub/internal/imvu" + "git.t5de.dev/t5de/t5de-hub/internal/realtime" + "git.t5de.dev/t5de/t5de-hub/internal/store" + "git.t5de.dev/t5de/t5de-hub/internal/token" +) + +// The tests run the whole hub against a real Postgres (embedded, downloaded once) and a fake IMVU API. + +var dbURL string + +func TestMain(m *testing.M) { + dir, _ := os.MkdirTemp("", "t5de-hub-test") + cache := os.Getenv("T5DE_PG_CACHE") + if cache == "" { + home, _ := os.UserCacheDir() + cache = filepath.Join(home, "t5de-hub-test-postgres") + } + const port = 54339 + pg := embeddedpostgres.NewDatabase(embeddedpostgres.DefaultConfig().Port(port). + DataPath(filepath.Join(dir, "data")).RuntimePath(filepath.Join(dir, "rt")).CachePath(cache). + Logger(io.Discard).StartTimeout(2 * time.Minute)) + if err := pg.Start(); err != nil { + fmt.Println("starting Postgres:", err) + os.Exit(1) + } + dbURL = fmt.Sprintf("postgres://postgres:postgres@localhost:%d/postgres?sslmode=disable", port) + code := m.Run() + pg.Stop() + os.RemoveAll(dir) + os.Exit(code) +} + +// fakeIMVU is IMVU's public user API, with accounts the test controls. +type fakeIMVU struct { + mu sync.Mutex + users map[int64]*imvu.User +} + +func (f *fakeIMVU) set(u imvu.User) { + f.mu.Lock() + defer f.mu.Unlock() + f.users[u.ID] = &u +} + +func (f *fakeIMVU) ServeHTTP(w http.ResponseWriter, r *http.Request) { + f.mu.Lock() + defer f.mu.Unlock() + var found *imvu.User + for _, u := range f.users { + if r.URL.Path == fmt.Sprintf("/user/user-%d", u.ID) || r.URL.Path == "/user" && strings.EqualFold(r.URL.Query().Get("username"), u.Username) { + found = u + } + } + den := map[string]any{} + if found != nil { + den[fmt.Sprintf("https://api.imvu.com/user/user-%d", found.ID)] = map[string]any{"data": map[string]any{ + "legacy_cid": found.ID, "username": found.Username, "tagline": found.Tagline, + "is_ageverified": found.AgeVerified, "is_ap": found.AccessPass}} + } + json.NewEncoder(w).Encode(map[string]any{"status": "success", "id": r.URL.String(), "denormalized": den}) +} + +type hub struct { + t *testing.T + server *Server + http *httptest.Server + imvu *fakeIMVU +} + +// newHub runs a hub on a fresh database. +func newHub(t *testing.T) *hub { + ctx := context.Background() + admin, err := pgx.Connect(ctx, dbURL) + if err != nil { + t.Fatal(err) + } + db := "t" + strings.ToLower(token.Random(6)) + db = regexp.MustCompile(`[^a-z0-9]`).ReplaceAllString(db, "x") + if _, err := admin.Exec(ctx, "create database "+db); err != nil { + t.Fatal(err) + } + admin.Close(ctx) + st, err := store.Open(ctx, strings.Replace(dbURL, "/postgres?", "/"+db+"?", 1)) + if err != nil { + t.Fatal(err) + } + t.Cleanup(st.Close) + fake := &fakeIMVU{users: map[int64]*imvu.User{}} + imvuServer := httptest.NewServer(fake) + t.Cleanup(imvuServer.Close) + keys, err := token.LoadOrCreate(filepath.Join(t.TempDir(), "key"), "http://hub.test") + if err != nil { + t.Fatal(err) + } + s := &Server{Store: st, IMVU: &imvu.Client{Base: imvuServer.URL, HTTP: imvuServer.Client()}, Keys: keys, + Log: log.New(io.Discard, "", 0)} + s.Realtime = realtime.New(Owners{Store: st}, s.Log) + handler, err := s.Handler() + if err != nil { + t.Fatal(err) + } + srv := httptest.NewServer(handler) + t.Cleanup(srv.Close) + s.PublicURL = srv.URL + return &hub{t: t, server: s, http: srv, imvu: fake} +} + +// browser is a person on the website. +type browser struct { + h *hub + client *http.Client + csrf string +} + +func (h *hub) browser() *browser { + jar, _ := cookiejar.New(nil) + return &browser{h: h, client: &http.Client{Jar: jar}} +} + +var csrfPattern = regexp.MustCompile(`name="csrf" value="([^"]+)"`) + +func (b *browser) get(path string) (int, string) { + b.h.t.Helper() + resp, err := b.client.Get(b.h.http.URL + path) + if err != nil { + b.h.t.Fatal(err) + } + defer resp.Body.Close() + body, _ := io.ReadAll(resp.Body) + if m := csrfPattern.FindSubmatch(body); m != nil { + b.csrf = string(m[1]) + } + return resp.StatusCode, string(body) +} + +func (b *browser) post(path string, form url.Values) (int, string) { + b.h.t.Helper() + if form == nil { + form = url.Values{} + } + if b.csrf != "" && form.Get("csrf") == "" { + form.Set("csrf", b.csrf) + } + req, _ := http.NewRequest("POST", b.h.http.URL+path, strings.NewReader(form.Encode())) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req.Header.Set("Sec-Fetch-Site", "same-origin") + resp, err := b.client.Do(req) + if err != nil { + b.h.t.Fatal(err) + } + defer resp.Body.Close() + body, _ := io.ReadAll(resp.Body) + if m := csrfPattern.FindSubmatch(body); m != nil { + b.csrf = string(m[1]) + } + return resp.StatusCode, string(body) +} + +func (b *browser) signup(username string) { + b.h.t.Helper() + status, body := b.post("/signup", url.Values{"username": {username}, "password": {"a long password"}, "password2": {"a long password"}}) + if status != 200 || !strings.Contains(body, "Signed in as "+username) { + b.h.t.Fatalf("signup %s: %d %s", username, status, body) + } +} + +var codePattern = regexp.MustCompile(`t5de-[A-Z0-9]{4}-[A-Z0-9]{4}`) + +// link links an IMVU account through the website, putting the code in its fake tagline. +func (b *browser) link(u imvu.User) string { + b.h.t.Helper() + b.h.imvu.set(u) + _, body := b.post("/account/link", url.Values{"username": {u.Username}}) + code := codePattern.FindString(body) + if code == "" { + b.h.t.Fatalf("no code: %s", body) + } + u.Tagline = "hello " + strings.ToLower(code) + b.h.imvu.set(u) + _, body = b.post(fmt.Sprintf("/account/link/%d/check", u.ID), nil) + return body +} + +func TestSignupSigninAndCSRF(t *testing.T) { + h := newHub(t) + a := h.browser() + a.signup("alice") + // The first account is the admin. + if _, body := a.get("/account"); !strings.Contains(body, ">admin<") { + t.Fatal("first account isn't admin") + } + b := h.browser() + b.signup("bob") + if status, _ := b.get("/console"); status != http.StatusForbidden { + t.Fatalf("bob sees the console: %d", status) + } + // Taken usernames (any case), bad passwords. + c := h.browser() + if status, body := c.post("/signup", url.Values{"username": {"ALICE"}, "password": {"a long password"}, "password2": {"a long password"}}); status != 400 || !strings.Contains(body, "taken") { + t.Fatalf("%d %s", status, body) + } + if status, _ := c.post("/signup", url.Values{"username": {"carol"}, "password": {"short"}, "password2": {"short"}}); status != 400 { + t.Fatalf("short password: %d", status) + } + // Signing in, with the right and wrong password. + if status, body := c.post("/signin", url.Values{"username": {"bob"}, "password": {"wrong"}}); status != 400 || !strings.Contains(body, "Wrong") { + t.Fatalf("%d %s", status, body) + } + if _, body := c.post("/signin", url.Values{"username": {"Bob"}, "password": {"a long password"}}); !strings.Contains(body, "Signed in as bob") { + t.Fatal("sign-in failed") + } + // A form without the session's CSRF token is refused. + if status, _ := c.post("/account/link", url.Values{"username": {"x"}, "csrf": {"forged"}}); status != http.StatusForbidden { + t.Fatalf("forged CSRF: %d", status) + } +} + +func TestLinkingByTagline(t *testing.T) { + h := newHub(t) + a := h.browser() + a.signup("alice") + // Without the code in the tagline, nothing is linked. + h.imvu.set(imvu.User{ID: 42, Username: "AliceAv"}) + a.post("/account/link", url.Values{"username": {"aliceav"}}) + if _, body := a.post("/account/link/42/check", nil); !strings.Contains(body, "doesn't have the code") { + t.Fatalf("linked without the code: %s", body) + } + body := a.link(imvu.User{ID: 42, Username: "AliceAv", AgeVerified: true}) + if !strings.Contains(body, "Linked AliceAv") || !strings.Contains(body, "age verified") { + t.Fatalf("%s", body) + } + if _, body := a.post("/account/link", url.Values{"username": {"nobody"}}); !strings.Contains(body, "IMVU has no account") { + t.Fatalf("%s", body) + } + + // Bob proves the same account is his (it was sold, say): it moves to him, with a new pairwise ID. + before, _ := h.server.Store.LinkByIMVUID(context.Background(), 42) + b := h.browser() + b.signup("bob") + b.link(imvu.User{ID: 42, Username: "AliceAv"}) + after, _ := h.server.Store.LinkByIMVUID(context.Background(), 42) + if after.PersonID == before.PersonID || after.PairwiseID == before.PairwiseID { + t.Fatalf("not moved: %+v -> %+v", before, after) + } + if _, body := a.get("/account"); strings.Contains(body, "AliceAv") { + t.Fatal("alice still has the account") + } + + // The link cap: bob may link only one more after a moderator lowers it. + bob, _ := h.server.Store.PersonByUsername(context.Background(), "bob") + a.get("/console/people/" + fmt.Sprint(bob.ID)) + if status, _ := a.post(fmt.Sprintf("/console/people/%d/cap", bob.ID), url.Values{"cap": {"1"}}); status != 200 { + t.Fatalf("setting the cap: %d", status) + } + if body := b.link(imvu.User{ID: 43, Username: "BobAlt"}); !strings.Contains(body, "link up to 1") { + t.Fatalf("over the cap: %s", body) + } +} + +func TestProxySignInAndPresence(t *testing.T) { + h := newHub(t) + a := h.browser() + a.signup("alice") + a.link(imvu.User{ID: 42, Username: "AliceAv"}) + + // The proxy asks for a code, alice enters it, the proxy gets tokens. + resp, err := http.Post(h.http.URL+"/api/v1/device/code", "", nil) + if err != nil { + t.Fatal(err) + } + var device struct{ DeviceCode, UserCode string } + var raw map[string]any + json.NewDecoder(resp.Body).Decode(&raw) + resp.Body.Close() + device.DeviceCode, device.UserCode = raw["device_code"].(string), raw["user_code"].(string) + poll := func() map[string]any { + resp, err := http.PostForm(h.http.URL+"/api/v1/device/token", url.Values{"device_code": {device.DeviceCode}}) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + var out map[string]any + json.NewDecoder(resp.Body).Decode(&out) + return out + } + if out := poll(); out["error"] != "authorization_pending" { + t.Fatalf("%v", out) + } + a.get("/device") + if _, body := a.post("/device", url.Values{"code": {strings.ToLower(strings.ReplaceAll(device.UserCode, "-", ""))}}); !strings.Contains(body, "T5DE is signed in") { + t.Fatalf("%s", body) + } + tokens := poll() + access, _ := tokens["access_token"].(string) + if access == "" || tokens["refresh_token"] == "" { + t.Fatalf("%v", tokens) + } + if out := poll(); out["error"] != "expired_token" { + t.Fatalf("a code worked twice: %v", out) + } + + // The refresh token rotates: the old one stops working. + refresh := func(tok string) map[string]any { + resp, err := http.PostForm(h.http.URL+"/api/v1/token/refresh", url.Values{"refresh_token": {tok}}) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + var out map[string]any + json.NewDecoder(resp.Body).Decode(&out) + return out + } + next := refresh(tokens["refresh_token"].(string)) + if next["access_token"] == nil || refresh(tokens["refresh_token"].(string))["error"] != "invalid_grant" { + t.Fatalf("rotation: %v", next) + } + + // The live connection: an account that isn't alice's is refused; hers shows as online (to moderators, and to + // everyone once she opts in) with her community outfit. + ctx := context.Background() + ws, _, err := websocket.Dial(ctx, strings.Replace(h.http.URL, "http", "ws", 1)+"/api/v1/realtime", + &websocket.DialOptions{HTTPHeader: http.Header{"Authorization": {"Bearer " + access}}}) + if err != nil { + t.Fatal(err) + } + defer ws.CloseNow() + read := func() map[string]any { + _, data, err := ws.Read(ctx) + if err != nil { + t.Fatal(err) + } + var m map[string]any + json.Unmarshal(data, &m) + return m + } + if m := read(); m["type"] != "welcome" { + t.Fatalf("%v", m) + } + ws.Write(ctx, websocket.MessageText, []byte(`{"type":"session","imvu_id":99}`)) + if m := read(); m["message"] != "not_linked" { + t.Fatalf("%v", m) + } + ws.Write(ctx, websocket.MessageText, []byte(`{"type":"session","imvu_id":42}`)) + ws.Write(ctx, websocket.MessageText, []byte(`{"type":"outfit","items":[{"id":"blue-coat","name":"Blue Coat"}]}`)) + waitFor(t, func() bool { o := h.server.Realtime.Online(); return len(o) == 1 && len(o[0].Outfit) == 1 }) + + visitor := h.browser() + if _, body := visitor.get("/online"); strings.Contains(body, "AliceAv") { + t.Fatal("online shown without opting in") + } + if _, body := a.get("/console"); !strings.Contains(body, "AliceAv") || !strings.Contains(body, "Blue Coat") { + t.Fatal("the console doesn't show her") + } + a.post("/account/links/42/settings", url.Values{"presence": {"on"}, "badge": {"on"}}) + if _, body := visitor.get("/online"); !strings.Contains(body, "AliceAv") || !strings.Contains(body, "Blue Coat") { + t.Fatal("online not shown after opting in") + } + + // A ban ends the connection and the sign-in. + bob := h.browser() + bob.signup("bob") + alice, _ := h.server.Store.PersonByUsername(ctx, "alice") + h.server.Store.SetRole(ctx, alice.ID, "user") + bobP, _ := h.server.Store.PersonByUsername(ctx, "bob") + h.server.Store.SetRole(ctx, bobP.ID, "admin") + bob.get("/account") + if status, _ := bob.post(fmt.Sprintf("/console/people/%d/ban", alice.ID), url.Values{"reason": {"testing"}}); status != 200 { + t.Fatalf("ban: %d", status) + } + waitFor(t, func() bool { return len(h.server.Realtime.Online()) == 0 }) + if out := refresh(next["refresh_token"].(string)); out["access_token"] != nil { + t.Fatal("a banned person's proxy can still sign in") + } +} + +func waitFor(t *testing.T, ok func() bool) { + t.Helper() + for i := 0; i < 200; i++ { + if ok() { + return + } + time.Sleep(10 * time.Millisecond) + } + t.Fatal("timed out") +}