Repository navigation
Expand file tree
/
Copy pathhandler.go
More file actions
273 lines (242 loc) · 8.72 KB
/
Copy pathhandler.go
File metadata and controls
273 lines (242 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package handler
import (
"database/sql"
"encoding/hex"
"log/slog"
"net/http"
"sync"
"time"
"golang.org/x/oauth2"
"github.com/calnode/calnode/internal/booking"
"github.com/calnode/calnode/internal/calendar"
"github.com/calnode/calnode/internal/livekit"
"github.com/calnode/calnode/internal/llm"
"github.com/calnode/calnode/internal/mailer"
"github.com/calnode/calnode/internal/stripe"
"github.com/calnode/calnode/internal/webhook"
"github.com/calnode/calnode/internal/zoom"
)
type Handler struct {
db *sql.DB
logger *slog.Logger
bookingSvc *booking.Service
mailer mailer.Mailer
smtpConnectHost string
smtpConnectPort string
live *mailer.Live // non-nil in production; nil in tests using a direct stub
encKey [32]byte // AES-256 key for encrypting secrets stored in the DB
calMu sync.RWMutex
cal *calendar.Service
calNudge chan struct{} // buffered(1): wakes the calendar reconciler after a failed inline op
webhookSvc *webhook.Service
baseURL string
publicBaseURL string
dataDir string
authMu sync.RWMutex
googleAuth *oauth2.Config
microsoftAuth *oauth2.Config
secureCookie bool
llmMu sync.RWMutex
llm *llm.Client // nil when the optional LLM layer is off/unconfigured
zoomMu sync.RWMutex
zoom *zoom.Client // nil when no Zoom app is configured
stripeMu sync.RWMutex
stripe *stripe.Client // nil when payments are unconfigured
livekitMu sync.RWMutex
livekit *livekit.Client // nil when LiveKit video is unconfigured
demoMode bool // true on the public demo instance: disables calendar/Zoom connect
demoResetInterval time.Duration
demoMu sync.RWMutex
demoNextResetAt time.Time
}
// SetLiveKit swaps the active LiveKit client (nil disables built-in video rooms).
// Hot-reloadable from the LiveKit settings page.
func (h *Handler) SetLiveKit(c *livekit.Client) {
h.livekitMu.Lock()
h.livekit = c
h.livekitMu.Unlock()
// Self-heal: any recording still 'active' is an orphan from before this restart (its egress
// is no longer tracked), and would otherwise block the idempotent guard on its room forever.
if c != nil {
if _, err := h.db.Exec(
`UPDATE recordings SET status = 'complete', updated_at = datetime('now') WHERE status = 'active'`); err != nil {
h.logger.Warn("livekit: sweep stale recordings", "error", err)
}
}
}
// getLiveKit returns the active LiveKit client, or nil when video is unconfigured.
func (h *Handler) getLiveKit() *livekit.Client {
h.livekitMu.RLock()
defer h.livekitMu.RUnlock()
return h.livekit
}
// SetStripe swaps the active Stripe client (nil disables paid bookings). Hot-reloadable
// from the Payments settings page.
func (h *Handler) SetStripe(c *stripe.Client) {
h.stripeMu.Lock()
h.stripe = c
h.stripeMu.Unlock()
}
// getStripe returns the active Stripe client, or nil when payments are unconfigured.
func (h *Handler) getStripe() *stripe.Client {
h.stripeMu.RLock()
defer h.stripeMu.RUnlock()
return h.stripe
}
// SetZoom swaps the active Zoom client (nil disables Zoom auto-minting). Hot-reloadable
// from the Zoom settings page.
func (h *Handler) SetZoom(c *zoom.Client) {
h.zoomMu.Lock()
h.zoom = c
h.zoomMu.Unlock()
}
// getZoom returns the active Zoom client, or nil when no Zoom app is configured.
func (h *Handler) getZoom() *zoom.Client {
h.zoomMu.RLock()
defer h.zoomMu.RUnlock()
return h.zoom
}
// SetLLM swaps the active LLM client (nil disables AI features). Hot-reloadable from
// the settings page.
func (h *Handler) SetLLM(c *llm.Client) {
h.llmMu.Lock()
h.llm = c
h.llmMu.Unlock()
}
// getLLM returns the active LLM client, or nil when AI is off — callers MUST nil-check
// and fall back to the deterministic path.
func (h *Handler) getLLM() *llm.Client {
h.llmMu.RLock()
defer h.llmMu.RUnlock()
return h.llm
}
func New(db *sql.DB, logger *slog.Logger) *Handler {
whs, _ := webhook.New(db, "") // ephemeral key when no encryption key configured
return &Handler{
db: db,
logger: logger,
bookingSvc: booking.New(db),
mailer: &mailer.Noop{},
webhookSvc: whs,
calNudge: make(chan struct{}, 1),
}
}
// SetMailer configures the email sender and the base URL used in email links.
// If m is a *mailer.Live, it is also stored as h.live for hot-swap support.
func (h *Handler) SetMailer(m mailer.Mailer, baseURL string) {
h.mailer = m
h.baseURL = baseURL
if l, ok := m.(*mailer.Live); ok {
h.live = l
}
}
// SetEncKey stores the AES-256 encryption key used for secrets in the DB.
func (h *Handler) SetEncKey(hexKey string) {
if b, err := hex.DecodeString(hexKey); err == nil && len(b) == 32 {
copy(h.encKey[:], b)
}
// If empty or invalid, encKey stays zero — suitable for dev/test.
}
// SetBaseURL sets the identity host used for OAuth redirects, admin UI links,
// and team invites.
func (h *Handler) SetBaseURL(url string) {
h.baseURL = url
}
// SetPublicBaseURL sets the booker-facing host used for booking-page links and
// outbound email links. When empty, publicURL falls back to baseURL.
func (h *Handler) SetPublicBaseURL(url string) {
h.publicBaseURL = url
}
// publicURL returns the booker-facing base URL, defaulting to the identity host
// (baseURL) when no public host has been configured.
func (h *Handler) publicURL() string {
if h.publicBaseURL != "" {
return h.publicBaseURL
}
return h.baseURL
}
// SetDataDir sets the directory used for file uploads (avatars, etc.).
func (h *Handler) SetDataDir(dir string) {
h.dataDir = dir
}
// SetDemoMode marks this instance as the public, self-resetting demo, which
// disables calendar/Zoom connect and is surfaced to the frontend via
// GET /v1/auth/status. Never set this on a real deployment.
func (h *Handler) SetDemoMode(v bool) {
h.demoMode = v
}
// SetDemoResetInterval records how often the demo wipes and re-seeds, purely
// so DemoReset (an on-demand reset) can recompute the same "next reset at"
// estimate the periodic ticker uses.
func (h *Handler) SetDemoResetInterval(d time.Duration) {
h.demoResetInterval = d
}
// SetDemoNextResetAt records when the next scheduled demo reset will fire,
// surfaced to the frontend via GET /v1/auth/status for a countdown.
func (h *Handler) SetDemoNextResetAt(t time.Time) {
h.demoMu.Lock()
h.demoNextResetAt = t
h.demoMu.Unlock()
}
// getDemoNextResetAt returns the next scheduled demo reset time (zero value
// when not in demo mode).
func (h *Handler) getDemoNextResetAt() time.Time {
h.demoMu.RLock()
defer h.demoMu.RUnlock()
return h.demoNextResetAt
}
// SetCalendar configures the multi-provider calendar service.
func (h *Handler) SetCalendar(c *calendar.Service) {
h.calMu.Lock()
h.cal = c
h.calMu.Unlock()
}
// getCal returns the current calendar service under a read lock (nil if unconfigured).
func (h *Handler) getCal() *calendar.Service {
h.calMu.RLock()
defer h.calMu.RUnlock()
return h.cal
}
// getGoogleAuth returns the current Google OAuth config under a read lock.
func (h *Handler) getGoogleAuth() *oauth2.Config {
h.authMu.RLock()
defer h.authMu.RUnlock()
return h.googleAuth
}
// getMicrosoftAuth returns the current Microsoft OAuth config under a read lock.
func (h *Handler) getMicrosoftAuth() *oauth2.Config {
h.authMu.RLock()
defer h.authMu.RUnlock()
return h.microsoftAuth
}
// SetWebhookSvc replaces the default ephemeral-key webhook service with one
// backed by the configured encryption key.
func (h *Handler) SetWebhookSvc(svc *webhook.Service) {
h.webhookSvc = svc
}
// isEmailEnabled reports whether a real SMTP sender is configured.
func (h *Handler) isEmailEnabled() bool {
if h.live != nil {
return h.live.IsEnabled()
}
// Fallback for tests that inject a direct stub mailer (not wrapped in Live).
_, isNoop := h.mailer.(*mailer.Noop)
return !isNoop
}
func (h *Handler) writeError(w http.ResponseWriter, status int, msg string) {
h.writeJSON(w, status, map[string]string{"error": msg})
}
// requireAdmin resolves the authenticated caller and writes a 403 (returning
// ok=false, which the caller must check and return on) unless they're an admin.
// Every settings handler (branding/tracking/google/email/llm/storage/stripe/zoom/
// livekit/notetaker) must call this first, not repeat the check inline — copy-paste
// was exactly how GetEmailSettings shipped without it (a real, live gap, not a
// hypothetical one) while its sibling PATCH/test-connection handlers had it.
func (h *Handler) requireAdmin(w http.ResponseWriter, r *http.Request) (AuthUser, bool) {
user, ok := userFromContext(r.Context())
if !ok || !user.IsAdmin {
h.writeError(w, http.StatusForbidden, "admin access required")
return AuthUser{}, false
}
return user, true
}