Files
supermarket/internal/secrets/secrets.go
T
davidandClaude Opus 5.5 43a7b42126 Phase 3 part 3: the audit, ranked by traffic, with AI visibility
A polite crawl of each website (Search Console's busiest pages, the
sitemap, then links; up to 300 pages; robots.txt honoured) checks titles
and descriptions (characters and pixels), headings, canonical, noindex,
status and redirects, broken links (outside ones twice), alt text, Open
Graph, JSON-LD, text without JavaScript, and whether the title says what
the page's main search asks. Issues are ranked by severity, then the
page's clicks and impressions. AI visibility: AI crawlers in robots.txt,
llms.txt, structured data, Organization data. Optional PageSpeed checks.
Weekly in the background, or Audit now. Schema 14.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 07:53:10 -07:00

135 lines
3.4 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"
OrigamiAPIKey = "origami.api_key"
ExpleeAPIKey = "explee.api_key"
HunterAPIKey = "hunter.api_key"
MailPassword = "mail.password" // the outreach mailbox on the Core
GoogleClientID = "google.client_id"
GoogleClientSecret = "google.client_secret"
GoogleRefreshToken = "google.refresh_token" // Search Console, read-only
PageSpeedAPIKey = "pagespeed.api_key"
PlausibleAPIKey = "plausible.api_key"
StripeAPIKey = "stripe.api_key" // restricted, read-only
RedditClientID = "reddit.client_id"
RedditClientSecret = "reddit.client_secret"
RedditAccessToken = "reddit.access_token"
RedditRefreshToken = "reddit.refresh_token"
RedditUsername = "reddit.username"
)
// 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
}