Files
supermarket/internal/secrets/secrets.go
T
davidandClaude Opus 5.5 0e34f574b7 Phase 1, part 3: Reddit
- Sign in with a Reddit web app; access tokens refresh automatically,
  and an app-only token reads rules before sign-in.
- Reddit drafts: a title and text for one subreddit, chosen when
  writing. The subreddit's guidelines and rules go into the prompt, which
  asks for a community member's post that discloses the affiliation and
  avoids marketing language.
- The Inbox shows the subreddit's rules (cached for a day, with Refresh)
  and warns, without blocking, about promotion rules, restricted
  subreddits, link-only subreddits, posting in the same subreddit within
  a week, more than 1 in 10 recent posts linking to the company's sites,
  and new accounts.
- Publishing makes a text post with the source link at the end, or a
  link post where the subreddit takes only links.
- Migration 4 adds the draft's title and subreddit, and the rules cache.
  Publishers now take a Post, which carries the title and subreddit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 13:51:16 -07:00

120 lines
3.0 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"
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
}