Files
supermarket/internal/secrets/secrets.go
T
davidandClaude Opus 5.5 5451fe6d63 Reddit: posted by hand, with the rules pasted
Reddit's API isn't open to apps like this one, so Supermarket no longer
signs in to Reddit, publishes or schedules there, or reads subreddits and
the account's posts. The internal/api/reddit client, Reddit's OAuth, its
keys and its Social accounts card are gone.

Supermarket still writes the post. Each Reddit draft in Posts has a box
for its subreddit's rules, with a link to them on Reddit: pasted once,
they're kept (subreddits.data, as pasted_rules) and given to the model
for later drafts. Open in Reddit saves the edits and opens the submit
page filled in; Copy title and Copy text (with the tagged source link)
cover a box that comes up empty; Mark as posted records it. Warnings come
from the pasted rules and the posts marked as posted. Fill's subreddit
suggestions are kept unchecked, and say so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 11:48:54 -07:00

157 lines
4.1 KiB
Go

// Package secrets keeps API keys and tokens. The local edition keeps them
// in one file, readable only by the user, outside the database so a copy
// of the database holds no credentials. The server and multi-tenant
// editions keep them in the database, encrypted (database.go).
package secrets
import (
"encoding/json"
"errors"
"fmt"
"os"
"sort"
"sync"
"supermarket/internal/db"
"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"
// The company page's own LinkedIn app (Community Management API)
LinkedInPageClientID = "linkedin_page.client_id"
LinkedInPageClientSecret = "linkedin_page.client_secret"
LinkedInPageAccessToken = "linkedin_page.access_token"
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
ListmonkToken = "listmonk.token" // an API user's token, to add people to mailing lists
GoogleClientID = "google.client_id"
GoogleClientSecret = "google.client_secret"
GoogleRefreshToken = "google.refresh_token" // Search Console, read-only
PageSpeedAPIKey = "pagespeed.api_key"
GiteaToken = "gitea.token" // opens pull requests on the sites' repositories
BingAPIKey = "bing.api_key"
PlausibleAPIKey = "plausible.api_key"
StripeAPIKey = "stripe.api_key" // restricted, read-only
)
// Store is the secrets: a file, or the database. It is safe for
// concurrent use.
type Store struct {
mu sync.Mutex
// The file (local edition)
path string
vals map[string]string
// The database (server and multi-tenant editions)
d *db.DB
key *sealer
cache map[string]string // the values last read, for when the database can't be
}
// 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 {
if s.d != nil {
return s.dbGet(name)
}
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 {
if s.d != nil {
return s.dbSet(updates)
}
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 {
if s.d != nil {
return s.dbNames()
}
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
}