Social accounts: X's 127.0.0.1 callback, "Get it" links, save in place; cart logo

- X refuses "localhost" in callback URLs, so its sign-in now uses
  http://127.0.0.1:8470/auth/x/callback (shown on the page and sent to X);
  LinkedIn and Reddit keep localhost.
- Every key, on Social accounts and the AI tab, has a "Get it" link to the
  service's page for it (opens a new tab) and says where on that page to
  find it. Addresses checked on 2026-11-21.
- Each network's card saves its own keys in place (htmx swaps the card),
  saying "Saved." and enabling Connect when ready; no reload. Without
  JavaScript it still saves and reloads.
- The shopping-cart image is the logo in the sidebar, the tab icon (PNG,
  and a multi-size favicon.ico at /favicon.ico) and the touch icon.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 11:01:01 -07:00
co-authored by Claude Opus 5.5
parent d9b1467365
commit 1f181ed554
10 changed files with 154 additions and 47 deletions
+4
View File
@@ -69,6 +69,10 @@ func (s *Server) Handler() (http.Handler, error) {
mux := http.NewServeMux()
static, _ := fs.Sub(staticFS, "static")
mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServerFS(static)))
// Browsers ask for /favicon.ico whatever the page says.
mux.HandleFunc("GET /favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.ServeFileFS(w, r, static, "favicon.ico")
})
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "ok") })
if s.MCP != nil {
mux.Handle("/mcp", s.MCP)
+36 -1
View File
@@ -91,7 +91,7 @@ func TestEveryPageRenders(t *testing.T) {
for _, path := range []string{"/", "/?msg=saved", "/settings/profile", "/inventory", "/inventory?relationship=partner",
"/inventory/new", "/inventory/1", "/inventory/import", "/inventory/export",
"/settings/ai", "/settings/accounts", "/settings/core", "/static/app.css", "/search", "/search?q=rival", "/search?q=nothing+here", "/static/htmx.min.js", "/healthz",
"/settings/ai", "/settings/accounts", "/settings/core", "/static/app.css", "/favicon.ico", "/static/favicon.png", "/static/logo.png", "/search", "/search?q=rival", "/search?q=nothing+here", "/static/htmx.min.js", "/healthz",
"/content", "/content/1", "/content/1?dismissed=1", "/inbox", "/inbox?msg=drafted",
"/content/scheduled", "/content/published"} {
rec := do(h, http.MethodGet, path, nil, nil)
@@ -215,6 +215,18 @@ func TestAccountsKeepAndRemove(t *testing.T) {
if s.Secrets.Has(secrets.AnthropicAPIKey) {
t.Error("the accounts form saved an AI key")
}
// From the page, a save sends back just that card, updated, not a redirect.
rec := do(h, http.MethodPost, "/settings/accounts", strings.NewReader(url.Values{
"network": {"LinkedIn"}, secrets.LinkedInClientID: {"li"}, secrets.LinkedInClientSecret: {"ls"}}.Encode()),
map[string]string{"Content-Type": "application/x-www-form-urlencoded", "Origin": "http://localhost:8470", "HX-Request": "true"})
card := rec.Body.String()
if rec.Code != http.StatusOK || !strings.Contains(card, `id="network-LinkedIn"`) || strings.Contains(card, "<html") ||
!strings.Contains(card, "Saved.") || strings.Contains(card, "Save the client ID") || strings.Contains(card, `"li"`) {
t.Errorf("htmx save: %d\n%s", rec.Code, card)
}
if s.Secrets.Get(secrets.XClientID) != "id-1" {
t.Error("saving one card cleared another's keys")
}
page := do(h, http.MethodGet, "/settings/accounts", nil, nil).Body.String()
if strings.Contains(page, "id-1") {
t.Error("the accounts page must never show a saved value")
@@ -284,6 +296,29 @@ func TestSearch(t *testing.T) {
}
}
func TestCallbackURLs(t *testing.T) {
s, h := newTestServer(t, nil)
s.PublicURL = "http://localhost:8470"
for provider, want := range map[string]string{
"x": "http://127.0.0.1:8470/auth/x/callback", // X refuses localhost
"linkedin": "http://localhost:8470/auth/linkedin/callback",
"reddit": "http://localhost:8470/auth/reddit/callback",
} {
if got := s.callbackURL(provider); got != want {
t.Errorf("%s: %s", provider, got)
}
}
if body := do(h, http.MethodGet, "/settings/accounts", nil, nil).Body.String(); !strings.Contains(body, "http://127.0.0.1:8470/auth/x/callback") {
t.Error("the accounts page should show X's 127.0.0.1 callback")
}
// The sign-in sends the same address X has registered.
_ = s.Secrets.Set(map[string]string{secrets.XClientID: "cid"})
rec := postForm(h, "/auth/x/start", nil)
if loc := rec.Header().Get("Location"); !strings.Contains(loc, url.QueryEscape("http://127.0.0.1:8470/auth/x/callback")) {
t.Errorf("x start: %d %s", rec.Code, loc)
}
}
func TestOAuthCallbackRejectsUnknownState(t *testing.T) {
_, h := newTestServer(t, nil)
rec := do(h, http.MethodGet, "/auth/x/callback?state=nope&code=c", nil, nil)
+79 -22
View File
@@ -3,6 +3,7 @@ package web
import (
"context"
"net/http"
"net/url"
"strings"
"time"
@@ -142,27 +143,48 @@ func (s *Server) aiModels(w http.ResponseWriter, r *http.Request) {
// --- Secrets forms ---
// secretField is one secret a settings form edits. Saved values are never
// shown: an empty field keeps what's saved.
// shown: an empty field keeps what's saved. Link is where to get it; Help
// says where on that page.
type secretField struct {
Group, Name, Label, Help string
Group, Name, Label, Help, Link string
}
// Where each key is found. Developer portals move; these were checked on
// 2026-11-21.
const (
xConsole = "https://console.x.com"
linkedInApps = "https://www.linkedin.com/developers/apps"
redditApps = "https://www.reddit.com/prefs/apps"
googleCreds = "https://console.cloud.google.com/apis/credentials"
anthropicKeys = "https://platform.claude.com/settings/keys"
)
// aiKeys are edited on the AI tab.
var aiKeys = []secretField{
{"anthropic", secrets.AnthropicAPIKey, "Anthropic API key", "From console.anthropic.com."},
{"openai", secrets.OpenAIAPIKey, "API key for the server", "Only if your server asks for one (Ollama and LM Studio don't)."},
{"anthropic", secrets.AnthropicAPIKey, "Anthropic API key",
"In the Claude Console, Settings → API keys → Create key. It's shown once: copy it straight here.", anthropicKeys},
{"openai", secrets.OpenAIAPIKey, "API key for the server",
"Only if your server asks for one (Ollama and LM Studio don't). It's in the server's or service's settings.", ""},
}
// accountFields are edited on the Social accounts tab, by network.
var accountFields = []secretField{
{"X", secrets.XBearerToken, "Bearer token", "For reading posts. Needs an X API plan with read access."},
{"X", secrets.XClientID, "OAuth 2.0 client ID", "For posting. From your app in the X developer portal."},
{"X", secrets.XClientSecret, "OAuth 2.0 client secret", "Only for confidential clients; leave empty for a public client."},
{"LinkedIn", secrets.LinkedInClientID, "Client ID", "From your app at linkedin.com/developers."},
{"LinkedIn", secrets.LinkedInClientSecret, "Client secret", ""},
{"Reddit", secrets.RedditClientID, "Client ID", "From a \"web app\" at reddit.com/prefs/apps: the string under the app's name."},
{"Reddit", secrets.RedditClientSecret, "Client secret", ""},
{"YouTube", secrets.YouTubeAPIKey, "API key", "Free, from console.cloud.google.com (YouTube Data API v3). For reading channels' videos."},
{"X", secrets.XBearerToken, "Bearer token",
"For reading posts; needs an X API plan with read access. In the X Developer Console, open your app → Keys and tokens → Bearer token.", xConsole},
{"X", secrets.XClientID, "OAuth 2.0 client ID",
"For posting. In your app, set up User authentication (read and write; type Web App; the callback URL below), then Keys and tokens → OAuth 2.0 Client ID.", xConsole},
{"X", secrets.XClientSecret, "OAuth 2.0 client secret",
"Next to the client ID, under Keys and tokens. Only a confidential client (Web App) has one; leave empty for a public client.", xConsole},
{"LinkedIn", secrets.LinkedInClientID, "Client ID",
"Create an app (it needs a LinkedIn company page), add the products Share on LinkedIn and Sign In with LinkedIn using OpenID Connect, then open the Auth tab.", linkedInApps},
{"LinkedIn", secrets.LinkedInClientSecret, "Client secret",
"On the same Auth tab, under the client ID.", linkedInApps},
{"Reddit", secrets.RedditClientID, "Client ID",
"Create app → choose \"web app\" and enter the redirect URI below. The client ID is the string under the app's name.", redditApps},
{"Reddit", secrets.RedditClientSecret, "Client secret",
"On the same app, next to \"secret\".", redditApps},
{"YouTube", secrets.YouTubeAPIKey, "API key",
"Free. In Google Cloud, enable the YouTube Data API v3 for a project, then Credentials → Create credentials → API key. For reading channels' videos.", googleCreds},
}
// saveSecrets stores what was typed into these fields. An empty field keeps
@@ -191,7 +213,9 @@ func (s *Server) saveSecrets(r *http.Request, fields []secretField) error {
type network struct {
Name string
Fields []secretField
Connect *connectInfo // nil: read-only, no sign-in
Connect *connectInfo // nil: read-only, no sign-in
Set map[string]bool // which secrets are saved
Saved bool // just saved: say so on the card
}
type connectInfo struct {
@@ -206,6 +230,13 @@ type connectInfo struct {
}
func (s *Server) accountsPage(w http.ResponseWriter, r *http.Request) {
s.render(w, r, "accounts", page{Title: "Social accounts", Nav: "settings/accounts", Data: map[string]any{
"Networks": s.networks(),
}})
}
// networks are the cards on the Social accounts tab, as things stand.
func (s *Server) networks() []network {
names := []string{secrets.XAccessToken, secrets.LinkedInAccessToken, secrets.RedditAccessToken}
for _, f := range accountFields {
names = append(names, f.Name)
@@ -214,7 +245,7 @@ func (s *Server) accountsPage(w http.ResponseWriter, r *http.Request) {
conns := s.connections()
connect := func(provider, token, setup, note string, ready bool, account func(string) string) *connectInfo {
c := conns[provider]
return &connectInfo{Provider: provider, Redirect: s.PublicURL + "/auth/" + provider + "/callback",
return &connectInfo{Provider: provider, Redirect: s.callbackURL(provider),
Setup: setup, Note: note, Ready: ready, Signed: set[token], Account: account(c.Account), At: c.At}
}
at := func(prefix string) func(string) string {
@@ -227,7 +258,7 @@ func (s *Server) accountsPage(w http.ResponseWriter, r *http.Request) {
}
var nets []network
for _, name := range []string{"X", "LinkedIn", "Reddit", "YouTube"} {
n := network{Name: name}
n := network{Name: name, Set: set}
for _, f := range accountFields {
if f.Group == name {
n.Fields = append(n.Fields, f)
@@ -247,10 +278,7 @@ func (s *Server) accountsPage(w http.ResponseWriter, r *http.Request) {
}
nets = append(nets, n)
}
s.render(w, r, "accounts", page{Title: "Social accounts", Nav: "settings/accounts", Data: map[string]any{
"Networks": nets,
"Set": set,
}})
return nets
}
func (s *Server) accountsSave(w http.ResponseWriter, r *http.Request) {
@@ -262,11 +290,40 @@ func (s *Server) accountsSave(w http.ResponseWriter, r *http.Request) {
s.serverError(w, err)
return
}
// From the page (htmx): send back just that network's card, so the page
// doesn't reload and lose its place. Without JavaScript: reload.
if r.Header.Get("HX-Request") == "true" {
for _, n := range s.networks() {
if n.Name == r.PostForm.Get("network") {
n.Saved = true
s.fragment(w, "accounts", "network", n)
return
}
}
}
redirect(w, r, "/settings/accounts", "saved")
}
// --- OAuth ---
// callbackURL is where a network sends the browser back after sign-in, as
// registered in the network's developer settings. X refuses "localhost" in
// callback URLs and asks for 127.0.0.1 instead, which reaches the same app.
func (s *Server) callbackURL(provider string) string {
base := s.PublicURL
if provider == "x" {
if u, err := url.Parse(base); err == nil && strings.EqualFold(u.Hostname(), "localhost") {
port := u.Port()
u.Host = "127.0.0.1"
if port != "" {
u.Host += ":" + port
}
base = u.String()
}
}
return strings.TrimSuffix(base, "/") + "/auth/" + provider + "/callback"
}
const oauthTTL = 15 * time.Minute
func (s *Server) oauthStart(w http.ResponseWriter, r *http.Request) {
@@ -283,21 +340,21 @@ func (s *Server) oauthStart(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Save X's OAuth client ID first.", http.StatusBadRequest)
return
}
authURL, p, err = oauth.XStart(id, s.PublicURL+"/auth/x/callback")
authURL, p, err = oauth.XStart(id, s.callbackURL("x"))
case "linkedin":
id := s.Secrets.Get(secrets.LinkedInClientID)
if id == "" || !s.Secrets.Has(secrets.LinkedInClientSecret) {
http.Error(w, "Save LinkedIn's client ID and secret first.", http.StatusBadRequest)
return
}
authURL, p, err = oauth.LinkedInStart(id, s.PublicURL+"/auth/linkedin/callback")
authURL, p, err = oauth.LinkedInStart(id, s.callbackURL("linkedin"))
case "reddit":
id := s.Secrets.Get(secrets.RedditClientID)
if id == "" || !s.Secrets.Has(secrets.RedditClientSecret) {
http.Error(w, "Save Reddit's client ID and secret first.", http.StatusBadRequest)
return
}
authURL, p, err = oauth.RedditStart(id, s.PublicURL+"/auth/reddit/callback")
authURL, p, err = oauth.RedditStart(id, s.callbackURL("reddit"))
default:
http.NotFound(w, r)
return
+5 -2
View File
@@ -118,8 +118,7 @@ h3 { font-size: 1rem; font-weight: 650; margin: 0 0 0.5rem; letter-spacing: -0.0
}
.brand { display: flex; align-items: center; gap: 0.55rem; padding: 0.85rem 0.9rem 0.7rem; color: inherit; text-decoration: none;
font-family: var(--font-display); font-weight: 700; font-size: 0.95rem; letter-spacing: -0.02em; }
.brand .logo { display: grid; place-items: center; width: 1.5rem; height: 1.5rem; border-radius: var(--radius-sm); background: hsl(var(--primary)); color: hsl(var(--primary-foreground)); }
.brand .logo .icon { width: 0.9rem; height: 0.9rem; }
.brand .logo { width: 1.75rem; height: 1.75rem; object-fit: contain; flex: none; }
.search { position: relative; margin: 0 0.6rem 0.4rem; display: flex; align-items: center; }
.search .icon { position: absolute; left: 0.65rem; color: hsl(var(--muted-foreground)); pointer-events: none; }
@@ -358,3 +357,7 @@ form.inline { display: inline-block; margin: 0 0 0.75rem; }
.suggestions li:last-child { border-bottom: 0; }
.suggestions li .help { flex-basis: 100%; margin: 0; }
.suggestions li.dropped a { text-decoration: line-through; color: hsl(var(--muted-foreground)); }
/* A key's label, with where to get it */
.label-row { display: flex; align-items: baseline; gap: 0.75rem; }
.get-it { margin-left: auto; font-size: 0.82rem; font-weight: 600; white-space: nowrap; }
Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

+23 -17
View File
@@ -1,26 +1,33 @@
{{define "content"}}{{template "settings-start" .}}{{with .Data}}{{$set := .Set}}
{{define "content"}}{{template "settings-start" .}}{{with .Data}}
<h2>Social accounts</h2>
<p class="lede">The keys Supermarket reads posts with, and the sign-ins it publishes with. Keys are kept in <code>secrets.json</code> in Supermarket's data folder, readable only by you; saved values are never shown, and an empty field keeps what's saved.</p>
{{/* The key fields of every card belong to this one form (form="accounts"),
so each card's Save saves them all, and the sign-in forms aren't nested. */}}
<form method="post" action="/settings/accounts" id="accounts"></form>
{{range .Networks}}{{template "network" .}}{{end}}
{{end}}{{template "settings-end"}}{{end}}
{{range .Networks}}{{$name := .Name}}
<section class="card network">
{{/* One network's card. Its keys form saves in place (htmx swaps the card
for the server's new copy); the sign-in form is a sibling, not nested. */}}
{{define "network"}}{{$set := .Set}}{{$name := .Name}}
<section class="card network" id="network-{{.Name}}">
<h2>{{.Name}}
{{with .Connect}}{{if .Signed}}<span class="badge published">connected{{with .Account}} as {{.}}{{end}}</span>{{else}}<span class="badge">not connected</span>{{end}}{{end}}
</h2>
{{range .Fields}}
<div class="secret">
<label>{{.Label}}
<input type="password" name="{{.Name}}" form="accounts" autocomplete="off" placeholder="{{if index $set .Name}}saved — type to replace{{else}}not set{{end}}">
{{if .Help}}<span class="help">{{.Help}}</span>{{end}}
</label>
{{if index $set .Name}}<label class="check small"><input type="checkbox" name="remove:{{.Name}}" form="accounts"> remove</label>{{end}}
</div>
{{end}}
<button type="submit" form="accounts" class="secondary">Save keys</button>
<form method="post" action="/settings/accounts" hx-post="/settings/accounts" hx-target="closest section" hx-swap="outerHTML" autocomplete="off">
<input type="hidden" name="network" value="{{.Name}}">
{{range .Fields}}
<div class="secret">
<label><span class="label-row">{{.Label}}{{with .Link}} <a class="get-it" href="{{.}}" target="_blank" rel="noopener noreferrer">Get it ↗</a>{{end}}</span>
<input type="password" name="{{.Name}}" autocomplete="off" placeholder="{{if index $set .Name}}saved — type to replace{{else}}not set{{end}}">
{{if .Help}}<span class="help">{{.Help}}</span>{{end}}
</label>
{{if index $set .Name}}<label class="check small"><input type="checkbox" name="remove:{{.Name}}"> remove</label>{{end}}
</div>
{{end}}
<div class="row">
<button type="submit" class="secondary">Save {{.Name}} keys</button>
{{if .Saved}}<span class="ok small" role="status">Saved.</span>{{end}}
</div>
</form>
{{with .Connect}}
<div class="signin">
@@ -34,4 +41,3 @@
{{end}}
</section>
{{end}}
{{end}}{{template "settings-end"}}{{end}}
+5 -3
View File
@@ -5,6 +5,9 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="htmx-config" content='{"includeIndicatorStyles": false}'>
<title>{{.Title}} · Supermarket</title>
<link rel="icon" href="/favicon.ico" sizes="any">
<link rel="icon" type="image/png" href="/static/favicon.png">
<link rel="apple-touch-icon" href="/static/logo.png">
<link rel="stylesheet" href="/static/app.css">
<script src="/static/htmx.min.js" defer></script>
<script src="/static/app.js" defer></script>
@@ -12,7 +15,7 @@
<body>
<div class="shell">
<aside class="sidebar" id="sidebar">
<a class="brand" href="/"><span class="logo">{{template "icon" "cart"}}</span>Supermarket</a>
<a class="brand" href="/"><img class="logo" src="/static/logo.png" alt="" width="28" height="28">Supermarket</a>
<form class="search" action="/search" method="get" role="search">
{{template "icon" "search"}}
<input type="search" name="q" value="{{.Query}}" placeholder="Search" aria-label="Search" id="search-box">
@@ -95,7 +98,7 @@
Saved values are never shown; an empty field keeps what's saved. */}}
{{define "key-field"}}
<div class="secret">
<label>{{.F.Label}}
<label><span class="label-row">{{.F.Label}}{{with .F.Link}} <a class="get-it" href="{{.}}" target="_blank" rel="noopener noreferrer">Get it ↗</a>{{end}}</span>
<input type="password" name="{{.F.Name}}" autocomplete="off" placeholder="{{if .Set}}saved — type to replace{{else}}not set{{end}}">
{{if .F.Help}}<span class="help">{{.F.Help}} Kept in <code>secrets.json</code>, readable only by you, and never shown again.</span>{{end}}
</label>
@@ -119,6 +122,5 @@
{{- else if eq . "accounts"}}<circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><path d="m8.59 13.51 6.83 3.98"/><path d="m15.41 6.51-6.82 3.98"/>
{{- else if eq . "ai"}}<path d="M12 8V4H8"/><rect width="16" height="12" x="4" y="8" rx="2"/><path d="M2 14h2"/><path d="M20 14h2"/><path d="M15 13v2"/><path d="M9 13v2"/>
{{- else if eq . "core"}}<path d="M6.3 20.3a2.4 2.4 0 0 0 3.4 0L12 18l-6-6-2.3 2.3a2.4 2.4 0 0 0 0 3.4Z"/><path d="m2 22 3-3"/><path d="M7.5 13.5 10 11"/><path d="M10.5 16.5 13 14"/><path d="m18 3-4 4h6l-4 4"/>
{{- else if eq . "cart"}}<circle cx="8" cy="21" r="1"/><circle cx="19" cy="21" r="1"/><path d="M2.05 2.05h2l2.66 12.42a2 2 0 0 0 2 1.58h9.78a2 2 0 0 0 1.95-1.57l1.65-7.43H5.12"/>
{{- else if eq . "menu"}}<path d="M4 12h16"/><path d="M4 6h16"/><path d="M4 18h16"/>
{{- end}}</svg>{{end}}