Files
superbrain/internal/notify/unsubscribe.go
T
Andrew S Erwin 5684757d24 Rename Go module to gitea.federated.computer/Federated_Computer/brain
The module path claimed a nonexistent GitHub repo. Point it (and the
agent User-Agent contact URL, README/RUNNING clone URLs) at the real
Gitea repository. Pure identifier rename: no runtime behavior change.

Verified: build, vet, unit + full DB integration (-race), db-converge,
web lint/SSR/build, browser regressions, e2e against rebuilt stack.
2026-11-22 02:46:40 -06:00

77 lines
3.0 KiB
Go

package notify
// Email footer links and the unsubscribe token. The token is a stable,
// HMAC-signed user id (no stored row, no expiry): every notification mail
// carries the same link, and rotating AUTH_SECRET invalidates outstanding
// links exactly like sessions. Unsubscribe is a NOTIFICATION preference —
// transactional mail (password reset) is never gated by it.
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"strings"
"gitea.federated.computer/Federated_Computer/brain/internal/config"
"gitea.federated.computer/Federated_Computer/brain/internal/db"
"gitea.federated.computer/Federated_Computer/brain/internal/mailer"
)
// unsubscribeDomain separates these MACs from every other use of the
// signing secret (assertions, forwarded-IP attestation, ...).
const unsubscribeDomain = "superbrain-unsubscribe:"
// UnsubscribeToken returns the signed unsubscribe token for a user. IDs
// are 32-char hex (db.NewID), so the single "." separator is unambiguous.
func UnsubscribeToken(userID string) string {
mac := hmac.New(sha256.New, []byte(config.Load().Secret()))
mac.Write([]byte(unsubscribeDomain + userID))
return userID + "." + hex.EncodeToString(mac.Sum(nil))
}
// ParseUnsubscribeToken validates a token and returns the user id it
// carries. The comparison is constant-time; a tampered or foreign-secret
// token never resolves an identity.
func ParseUnsubscribeToken(token string) (string, bool) {
userID, sig, ok := strings.Cut(token, ".")
if !ok || userID == "" || sig == "" {
return "", false
}
mac := hmac.New(sha256.New, []byte(config.Load().Secret()))
mac.Write([]byte(unsubscribeDomain + userID))
want := hex.EncodeToString(mac.Sum(nil))
if !hmac.Equal([]byte(want), []byte(sig)) {
return "", false
}
return userID, true
}
// FooterLinks builds the branded email footer for a message to recipient.
// Preferences and Help are offered whenever the deployment has a public
// origin (NEXTAUTH_URL). Unsubscribe is offered ONLY when recipient is the
// owner's own account email: a third-party recipient (a channel address or
// an agent task's To) must never receive a link that changes the owner's
// settings. With no origin configured every link stays empty and the
// renderer omits the footer sections.
func FooterLinks(ctx context.Context, d *db.DB, ownerID, recipient string) mailer.Content {
var c mailer.Content
base := strings.TrimRight(strings.TrimSpace(config.Load().NextAuthURL), "/")
if base == "" {
return c
}
c.PreferencesURL = base + "/settings?tab=notifications"
c.HelpURL = base + "/help"
var email *string
if err := d.QueryRowContext(ctx, `SELECT email FROM "User" WHERE id=$1`, ownerID).Scan(&email); err != nil {
return c
}
if email == nil || *email == "" || !strings.EqualFold(strings.TrimSpace(recipient), *email) {
return c
}
token := UnsubscribeToken(ownerID)
c.UnsubscribeURL = base + "/unsubscribe?token=" + token
c.UnsubscribePostURL = base + "/api/notifications/unsubscribe?token=" + token
return c
}