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:
+79
-22
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user