Replace the old Go TUI with a web app (port 8470) that runs in a container as a CoreDesktop sibling: - cmd/supermarket server with htmx web UI: dashboard, company profile, inventory (partners/competitors), AI and credential settings, and a Core & Hermes page - SQLite storage and a secrets store under ~/.local/share/supermarket - AI providers (Anthropic, OpenAI-compatible) behind one interface - MCP server and a Hermes bridge to the Federated Core MCP hub - Import of the old Coremarketing TUI's state - install.sh / update.sh / lib.sh, Dockerfile, compose.yaml, the bin/supermarket launcher and hermes/setup.sh Posting is not wired back up yet; that comes in Phase 1. See PLAN.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
2.8 KiB
Go
114 lines
2.8 KiB
Go
// Package secrets keeps API keys and tokens in one file, readable only by the
|
|
// user, outside the database so a copy of the database holds no credentials.
|
|
package secrets
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"sort"
|
|
"sync"
|
|
|
|
"supermarket/internal/fileutil"
|
|
)
|
|
|
|
// Names of the secrets the app knows. Keep them stable: they are the keys in
|
|
// the file.
|
|
const (
|
|
AnthropicAPIKey = "anthropic.api_key"
|
|
OpenAIAPIKey = "openai.api_key" // for OpenAI-compatible servers that want one
|
|
|
|
XBearerToken = "x.bearer_token"
|
|
XClientID = "x.client_id"
|
|
XClientSecret = "x.client_secret"
|
|
XAccessToken = "x.access_token"
|
|
XRefreshToken = "x.refresh_token"
|
|
|
|
LinkedInClientID = "linkedin.client_id"
|
|
LinkedInClientSecret = "linkedin.client_secret"
|
|
LinkedInAccessToken = "linkedin.access_token"
|
|
LinkedInAuthorURN = "linkedin.author_urn"
|
|
|
|
YouTubeAPIKey = "youtube.api_key"
|
|
)
|
|
|
|
// Store is the secrets file. It is safe for concurrent use.
|
|
type Store struct {
|
|
path string
|
|
mu sync.Mutex
|
|
vals map[string]string
|
|
}
|
|
|
|
// Open loads the secrets file at path, or starts empty if there is none. An
|
|
// unreadable file is an error rather than an empty store, so a later save
|
|
// can't overwrite the user's credentials.
|
|
func Open(path string) (*Store, error) {
|
|
s := &Store{path: path, vals: map[string]string{}}
|
|
data, err := os.ReadFile(path)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return s, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(data) > 0 {
|
|
if err := json.Unmarshal(data, &s.vals); err != nil {
|
|
return nil, fmt.Errorf("%s isn't valid JSON (fix or move it aside): %w", path, err)
|
|
}
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
// Get returns the secret name, or "" if it isn't set.
|
|
func (s *Store) Get(name string) string {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
return s.vals[name]
|
|
}
|
|
|
|
// Has reports whether the secret name is set.
|
|
func (s *Store) Has(name string) bool {
|
|
return s.Get(name) != ""
|
|
}
|
|
|
|
// Set stores the given secrets (an empty value removes one) and saves the
|
|
// file. On a failed save nothing changes in memory either.
|
|
func (s *Store) Set(updates map[string]string) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
next := make(map[string]string, len(s.vals)+len(updates))
|
|
for k, v := range s.vals {
|
|
next[k] = v
|
|
}
|
|
for k, v := range updates {
|
|
if v == "" {
|
|
delete(next, k)
|
|
} else {
|
|
next[k] = v
|
|
}
|
|
}
|
|
data, err := json.MarshalIndent(next, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := fileutil.WriteFileAtomic(s.path, data, 0600); err != nil {
|
|
return err
|
|
}
|
|
s.vals = next
|
|
return nil
|
|
}
|
|
|
|
// Names lists the secrets that are set, for status pages. It never returns
|
|
// values.
|
|
func (s *Store) Names() []string {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
names := make([]string, 0, len(s.vals))
|
|
for k := range s.vals {
|
|
names = append(names, k)
|
|
}
|
|
sort.Strings(names)
|
|
return names
|
|
}
|