| 1 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | |
| 3 | package web |
| 4 | |
| 5 | import ( |
| 6 | "crypto/sha256" |
| 7 | "encoding/base64" |
| 8 | "io/fs" |
| 9 | "log/slog" |
| 10 | |
| 11 | "github.com/jackc/pgx/v5/pgxpool" |
| 12 | |
| 13 | "github.com/tenseleyFlow/shithub/internal/infra/config" |
| 14 | notifhandlers "github.com/tenseleyFlow/shithub/internal/web/handlers/notifications" |
| 15 | "github.com/tenseleyFlow/shithub/internal/web/render" |
| 16 | ) |
| 17 | |
| 18 | // buildNotifHandlers wires the S29 notification handlers. Owns its |
| 19 | // own renderer (same pattern as the other handler builders). |
| 20 | func buildNotifHandlers( |
| 21 | cfg config.Config, |
| 22 | pool *pgxpool.Pool, |
| 23 | tmplFS fs.FS, |
| 24 | logger *slog.Logger, |
| 25 | ) (*notifhandlers.Handlers, error) { |
| 26 | rr, err := render.New(tmplFS, render.Options{Octicons: render.BuiltinOcticons()}) |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | return notifhandlers.New(notifhandlers.Deps{ |
| 31 | Logger: logger, |
| 32 | Render: rr, |
| 33 | Pool: pool, |
| 34 | UnsubscribeKey: notifUnsubscribeKey(cfg, logger), |
| 35 | }) |
| 36 | } |
| 37 | |
| 38 | // notifUnsubscribeKey mirrors the worker's resolver. Kept here so |
| 39 | // the web binary doesn't have to import the worker bundle just for |
| 40 | // this helper. The dev-fallback derivation is intentional and |
| 41 | // logged loudly — see worker.go for the matching prod hint. |
| 42 | func notifUnsubscribeKey(cfg config.Config, logger *slog.Logger) []byte { |
| 43 | if cfg.Notif.UnsubscribeKeyB64 != "" { |
| 44 | k, err := base64.StdEncoding.DecodeString(cfg.Notif.UnsubscribeKeyB64) |
| 45 | if err == nil && len(k) >= 16 { |
| 46 | return k |
| 47 | } |
| 48 | } |
| 49 | if cfg.Session.KeyB64 != "" { |
| 50 | seed, err := base64.StdEncoding.DecodeString(cfg.Session.KeyB64) |
| 51 | if err == nil && len(seed) > 0 { |
| 52 | sum := sha256.Sum256(append([]byte("notif-unsub:"), seed...)) |
| 53 | if logger != nil { |
| 54 | logger.Warn("notif (web): deriving unsubscribe key from session secret (dev fallback)", |
| 55 | "hint", "set Notif.UnsubscribeKeyB64 in prod") |
| 56 | } |
| 57 | return sum[:] |
| 58 | } |
| 59 | } |
| 60 | if logger != nil { |
| 61 | logger.Warn("notif (web): no key material — using static dev key") |
| 62 | } |
| 63 | return []byte("shithub-dev-unsub-static-key-32B") |
| 64 | } |
| 65 |