Repository navigation
Expand file tree
/
Copy pathserver.go
More file actions
613 lines (575 loc) · 21.2 KB
/
Copy pathserver.go
File metadata and controls
613 lines (575 loc) · 21.2 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// Package httpapi serves the JMAP HTTP surface (PLAN §3): the session
// resource and the API endpoint, both under the /{account}/ path prefix
// (D-13), both on the configured base_url origin (FR-D.2, FR-J.10), and
// both behind the same Basic authentication on every endpoint (FR-A.3,
// FR-J.6).
package httpapi
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"io"
"log/slog"
"net/http"
"strings"
"sync"
"github.com/CaffeinatedTech/jmap-bridge/internal/auth"
"github.com/CaffeinatedTech/jmap-bridge/internal/config"
"github.com/CaffeinatedTech/jmap-bridge/internal/jmapapi"
"github.com/CaffeinatedTech/jmap-bridge/internal/metrics"
"github.com/CaffeinatedTech/jmap-bridge/internal/oauth"
"github.com/CaffeinatedTech/jmap-bridge/internal/push"
"github.com/CaffeinatedTech/jmap-bridge/internal/ratelimit"
)
// Request size caps (NFR-5, locked as D-16). maxSizeRequest mirrors the
// value advertised in the core capability object.
const (
maxSizeRequest = 32 << 20 // 32 MiB
maxSizeUpload = 64 << 20 // 64 MiB: the session cap and the upload endpoint's limit (NFR-5, D-16)
)
// Server is the bridge's HTTP handler.
type Server struct {
cfg *config.Config
tokens *auth.Tokens
store jmapapi.Store
backends map[string]jmapapi.Backend
hub *push.Hub
jmap *jmapapi.Handler
log *slog.Logger
mux *http.ServeMux
// oauth maps an account id to its OAuth2 client; accounts without
// one have no /oauth/ endpoints at all (FR-A.5).
oauth map[string]*oauth.Manager
// kick nudges an account's sync engine after credentials land.
kick func(account string)
// contactsReady reports whether an account's first CardDAV sync has
// succeeded — the second half of the contacts capability gate
// (FR-P.3: configured *and* working, never advertised on hope).
contactsReady func(account string) bool
// limit is the in-process failed-auth lockout (NFR-5, A2); nil when
// rate limiting is disabled.
limit *ratelimit.Limiter
// reqSem/uploadSem bound concurrent work; nil means unbounded.
reqSem chan struct{}
uploadSem chan struct{}
// maxReq/maxUpload are what the session advertises; enforcement uses
// the same numbers so capability and behaviour cannot drift.
maxReq, maxUpload int
// SSE connection caps (A4). sseMu guards the live counters.
sseMu sync.Mutex
ssePerAcct map[string]int
sseTotal int
maxSSEPerAcct int
maxSSETotal int
// metrics, when non-nil, is handed to the JMAP dispatcher (FR-D.6).
metrics *metrics.Registry
// gmailPush maps an account id to its Pub/Sub push authenticator
// (FR-S.14); accounts absent from the map have no push endpoint
// (404). nil disables the endpoint entirely.
gmailPush map[string]PushVerifier
}
// PushVerifier authenticates one Gmail Pub/Sub push request (FR-S.14). It is
// the seam between the HTTP layer and the Gmail-specific OIDC/shared-secret
// verification; tests substitute a fake.
type PushVerifier interface {
Verify(ctx context.Context, r *http.Request) error
}
// Option configures optional Server collaborators.
type Option func(*Server)
// WithMetrics attaches the FR-D.6 registry: the JMAP method counters are
// wired into the dispatcher and the registry is kept so main can register
// the SSE gauge. A nil registry leaves metrics off.
func WithMetrics(reg *metrics.Registry) Option {
return func(s *Server) {
if reg != nil {
s.metrics = reg
}
}
}
// WithGmailPush mounts the Gmail Pub/Sub push endpoint (FR-S.14): the map
// names, per gmail_api account, how a push is authenticated. Accounts absent
// from the map have no push endpoint, and an empty map leaves it unregistered
// behind a 404.
func WithGmailPush(verifiers map[string]PushVerifier) Option {
return func(s *Server) {
if len(verifiers) > 0 {
s.gmailPush = verifiers
}
}
}
// New wires a Server: routing, auth, dispatch and push. store serves
// every configured account; backends maps an account id to the object
// that mutates it (nil for a cache-only account, which then refuses
// every write); hub is the change signal source for the eventsource
// endpoint (FR-J.8); oauth carries the per-account OAuth2 clients
// (FR-A.5) and kick is called after a successful consent.
func New(cfg *config.Config, tokens *auth.Tokens, store jmapapi.Store,
backends map[string]jmapapi.Backend, hub *push.Hub, log *slog.Logger,
oauth map[string]*oauth.Manager, kick func(account string),
contactsReady func(account string) bool, opts ...Option,
) *Server {
s := &Server{
cfg: cfg,
tokens: tokens,
store: store,
backends: backends,
hub: hub,
log: log,
mux: http.NewServeMux(),
oauth: oauth,
kick: kick,
contactsReady: contactsReady,
// Allocated regardless of rate limiting: acquireSSE writes into
// it on every stream, and a nil map would panic when rate
// limiting is disabled (SSE caps are then simply unbounded).
ssePerAcct: map[string]int{},
}
for _, opt := range opts {
opt(s)
}
s.jmap = jmapapi.NewHandler(store, jmapapi.WithMetrics(s.metrics))
if s.log == nil {
// Tests build Servers without logging; the error paths must not
// segfault for it.
s.log = slog.New(slog.NewTextHandler(io.Discard, nil))
}
s.applyRateLimits(cfg.Rate)
s.mux.HandleFunc("GET /{account}/.well-known/jmap", s.handleSession)
s.mux.HandleFunc("POST /{account}/jmap", s.handleAPI)
s.mux.HandleFunc("GET /{account}/eventsource/", s.handleEventSource)
// Subtree pattern: the session's {accountId} expands into the
// path prefix, and anything after it is the client's business
// (RFC 8620 §6.1 only requires the template to carry accountId).
s.mux.HandleFunc("POST /{account}/upload/", s.handleUpload)
s.mux.HandleFunc("GET /{account}/download/{blobId}/{name}", s.handleDownload)
// FR-D.13: the OAuth consent screen's public home page and privacy
// policy. Both are on this origin and neither carries account data.
s.mux.HandleFunc("GET /{$}", s.handleHome)
s.mux.HandleFunc("GET /privacy", s.handlePrivacy)
// FR-S.14: the Gmail Pub/Sub push endpoint. It is deliberately not
// behind the client token (Pub/Sub cannot carry it) — its own verifier
// authenticates the request; the handler 404s for accounts without one.
s.mux.HandleFunc("POST /gmail/push/{account}", s.handleGmailPush)
return s
}
// ServeHTTP implements http.Handler. The OAuth2 bootstrap subtree is
// dispatched before the mux (FR-A.5): its "/oauth/…" paths collide
// structurally with the "/{account}/…" wildcards above — ServeMux would
// refuse to register both — and it is deliberately NOT behind the client
// token: the operator reaches it from a browser before any client is
// configured, and the flow's own state is the credential (FR-A.6).
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Defence-in-depth response headers (NFR-5). The pages carry no
// scripts or styles, so a deny-all CSP costs nothing and stops a
// download or content-sniffing quirk from turning into script on
// this origin.
h := w.Header()
h.Set("X-Content-Type-Options", "nosniff")
h.Set("X-Frame-Options", "DENY")
h.Set("Referrer-Policy", "no-referrer")
h.Set("Content-Security-Policy",
"default-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'")
if r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/oauth/") {
s.handleOAuth(w, r)
return
}
// EventSource streams are long-lived and governed by their own caps;
// every other request takes a slot so the advertised
// maxConcurrentRequests is real (golden rule 4, A4).
if !isEventSource(r) {
if !s.acquire(s.reqSem) {
s.tooManyRequests(w, "maxConcurrentRequests")
return
}
defer s.release(s.reqSem)
}
s.mux.ServeHTTP(w, r)
}
// applyRateLimits builds the failed-auth limiter and concurrency gates
// from configuration (NFR-5). Disabled leaves them nil (unbounded),
// which is an explicit operator choice.
func (s *Server) applyRateLimits(rate config.Rate) {
s.maxReq, s.maxUpload = 8, 4
if rate.MaxConcurrentRequests > 0 {
s.maxReq = rate.MaxConcurrentRequests
}
if rate.MaxConcurrentUploads > 0 {
s.maxUpload = rate.MaxConcurrentUploads
}
if !rate.Enabled {
return
}
s.limit = ratelimit.New(ratelimit.Config{
AuthFailures: rate.AuthFailures,
AuthWindow: rate.AuthWindow.Std(),
AuthBlock: rate.AuthBlock.Std(),
ClientIPHeader: rate.ClientIPHeader,
TrustedProxies: rate.TrustedProxies,
})
s.reqSem = make(chan struct{}, s.maxReq)
s.uploadSem = make(chan struct{}, s.maxUpload)
s.maxSSEPerAcct = rate.MaxEventsourcePerAccount
s.maxSSETotal = rate.MaxEventsourceTotal
s.ssePerAcct = map[string]int{}
}
// isEventSource reports whether r targets the SSE endpoint, which is
// exempt from the request concurrency gate.
func isEventSource(r *http.Request) bool {
return r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/eventsource/")
}
// acquire takes a slot from sem without blocking; nil is unbounded.
func (s *Server) acquire(sem chan struct{}) bool {
if sem == nil {
return true
}
select {
case sem <- struct{}{}:
return true
default:
return false
}
}
// release returns a slot taken by acquire.
func (s *Server) release(sem chan struct{}) {
if sem != nil {
<-sem
}
}
// acquireSSE reserves an EventSource slot for account, refusing when the
// per-account or total cap is reached (A4).
func (s *Server) acquireSSE(account string) bool {
s.sseMu.Lock()
defer s.sseMu.Unlock()
if s.maxSSETotal > 0 && s.sseTotal >= s.maxSSETotal {
return false
}
if s.maxSSEPerAcct > 0 && s.ssePerAcct[account] >= s.maxSSEPerAcct {
return false
}
s.ssePerAcct[account]++
s.sseTotal++
return true
}
// releaseSSE frees an EventSource slot.
func (s *Server) releaseSSE(account string) {
s.sseMu.Lock()
defer s.sseMu.Unlock()
if s.ssePerAcct[account] > 0 {
s.ssePerAcct[account]--
if s.ssePerAcct[account] == 0 {
delete(s.ssePerAcct, account)
}
}
if s.sseTotal > 0 {
s.sseTotal--
}
}
// SSECounts reports the live EventSource connections (FR-D.6): the total
// and a copy of the per-account counts, so the caller can read them
// without holding sseMu.
func (s *Server) SSECounts() (total int, perAccount map[string]int) {
s.sseMu.Lock()
defer s.sseMu.Unlock()
perAccount = make(map[string]int, len(s.ssePerAcct))
for account, n := range s.ssePerAcct {
perAccount[account] = n
}
return s.sseTotal, perAccount
}
// tooManyRequests answers a refused request with Retry-After and the
// registered JMAP limit problem type (RFC 8620 §3.6.1).
func (s *Server) tooManyRequests(w http.ResponseWriter, limit string) {
w.Header().Set("Retry-After", "60")
writeJSON(w, http.StatusTooManyRequests, map[string]any{
"type": "urn:ietf:params:jmap:error:limit",
"status": http.StatusTooManyRequests,
"limit": limit,
"detail": "too many requests",
}, nil)
}
// authorize resolves the path account and checks the request
// credentials. Every failure in token mode collapses to the same 401
// with WWW-Authenticate — unknown account, missing header, empty
// username, or wrong token are indistinguishable (FR-A.11, FR-A.12,
// FR-J.6). auth.mode = "none" serves without credentials and is only
// reachable on loopback (FR-A.3), enforced when the config loads.
func (s *Server) authorize(w http.ResponseWriter, r *http.Request, accountID string) *config.Account {
acct := s.cfg.Account(accountID)
if s.cfg.Auth.Mode == "none" {
return acct // nil → caller 404s: no credentials are in play to hide behind
}
user, pass, ok := r.BasicAuth()
// Verify runs unconditionally: an unknown account must cost the same
// constant-time compare as a known one, or the response time leaks
// which account ids exist (FR-A.11, FR-A.12).
verified := s.tokens.Verify(accountID, pass)
key := ""
if s.limit != nil {
key = "auth:" + s.limit.ClientIP(r) + ":" + accountID
if s.limit.Blocked(key) {
s.tooManyRequests(w, "authFailures")
return nil
}
}
if acct == nil || !ok || user == "" || !verified {
if s.limit != nil {
s.limit.Fail(key)
}
w.Header().Set("WWW-Authenticate", `Basic realm="jmap-bridge"`)
http.Error(w, "unauthorized", http.StatusUnauthorized)
return nil
}
if s.limit != nil {
s.limit.Succeed(key)
}
return acct
}
// username extracts the Basic username for session display; in none mode
// there is none, so the account address stands in.
func username(r *http.Request, acct *config.Account) string {
if user, _, ok := r.BasicAuth(); ok && user != "" {
return user
}
return acct.Address
}
// sessionState derives the session's state string (RFC 8620 §2): a
// short hash over every property of the session object, so any change to
// them changes it. Deterministic across restarts, so clients do not
// refetch the session needlessly.
func (s *Server) sessionState(acct *config.Account, user string) string {
h := sha256.New()
for _, part := range []string{
s.cfg.BaseURL,
acct.ID,
acct.Name,
acct.Address,
user,
strings.Join(s.accountCapabilities(acct), ","),
} {
_, _ = io.WriteString(h, part)
_, _ = h.Write([]byte{0})
}
return hex.EncodeToString(h.Sum(nil))[:16]
}
// handleSession serves GET /{account}/.well-known/jmap (FR-J.1). URLs
// are built from base_url, never the request Host header (FR-D.2), and
// every endpoint advertised here exists — uploadUrl and downloadUrl
// since M3, the submission capability only for an account with SMTP
// behind it (FR-J.5, FR-M.14).
func (s *Server) handleSession(w http.ResponseWriter, r *http.Request) {
acct := s.authorize(w, r, r.PathValue("account"))
if acct == nil {
if s.cfg.Auth.Mode == "none" {
http.NotFound(w, r)
}
return
}
user := username(r, acct)
name := acct.Name
if name == "" {
name = acct.Address
}
if name == "" {
name = acct.ID
}
base := strings.TrimRight(s.cfg.BaseURL, "/")
// RFC 8621 §1.3.2: the session-level capability is an empty object,
// the account-level one carries maxDelayedSend (0 — v0.1 does not
// do delayed send). It appears only when this account has an SMTP
// server to submit through (FR-J.5, FR-M.14).
capabilities := map[string]any{
// RFC 8621 §1.3.1: the session-level mail capability is an
// empty object; the limits live on the account.
"urn:ietf:params:jmap:mail": map[string]any{},
}
accountCapabilities := map[string]any{
"urn:ietf:params:jmap:mail": map[string]any{
"maxMailboxesPerEmail": 100,
"maxMailboxDepth": 20,
"maxSizeMailboxName": 255,
"maxSizeAttachmentsPerEmail": maxSizeUpload,
"emailQuerySortOptions": []string{
"receivedAt", "subject", "from", "size", "hasAttachment",
},
// Mailbox/set exists as of M2 (FR-M.12), so top-level
// creation may be advertised honestly (FR-J.5).
"mayCreateTopLevelMailbox": true,
},
}
if canSend(acct) {
capabilities[jmapapi.SubmissionURN] = map[string]any{}
accountCapabilities[jmapapi.SubmissionURN] = map[string]any{"maxDelayedSend": 0}
}
// FR-P.3: the contacts capability appears only when CardDAV is
// configured *and* the first sync succeeded; until then the
// primaryAccounts entry and every contacts method are absent, and
// clients degrade exactly as they do for an unknown server.
if s.canContacts(acct) {
capabilities[jmapapi.ContactURN] = map[string]any{}
accountCapabilities[jmapapi.ContactURN] = map[string]any{
"maxAddressBooks": 100,
"maxContactsPerBook": 0, // 0 = no server-imposed limit (RFC 9610 §1.5)
"maxCardsInSet": 512,
"mayCreateAddressBook": false, // AddressBook/set is out of v0.1 scope (FR-P.10)
"mayDeleteAddressBook": false,
}
}
capabilities["urn:ietf:params:jmap:core"] = map[string]any{
"maxSizeUpload": maxSizeUpload,
"maxConcurrentUpload": s.maxUpload,
"maxSizeRequest": maxSizeRequest,
"maxConcurrentRequests": s.maxReq,
"maxCallsInRequest": 256,
"maxObjectsInGet": 512,
"maxObjectsInSet": 512,
// RFC 8620 §2 requires the property; the bridge imposes no
// collation of its own, so an empty array is the honest answer.
"collationAlgorithms": []string{},
}
session := map[string]any{
"capabilities": capabilities,
"accounts": map[string]any{
acct.ID: map[string]any{
"name": name,
"isPersonal": true,
"isReadOnly": false,
"accountCapabilities": accountCapabilities,
},
},
"primaryAccounts": primaryAccounts(s, acct),
"username": user,
"apiUrl": base + "/" + acct.ID + "/jmap",
// The eventsource exists as of M1 (FR-J.8), upload/download as
// of M3 (FR-M.16, FR-M.17); all three are RFC 8620 §2 URI
// templates this server expands exactly as written.
"eventSourceUrl": base + "/" + acct.ID +
"/eventsource/?types={types}&closeafter={closeafter}&ping={ping}",
// {accountId} leads both URLs: the account path prefix *is* the
// variable RFC 8620 §6.1/§6.2 require, so expanding the
// template produces this server's own route with nothing left
// over (D-13: one origin, account-prefixed paths).
"uploadUrl": base + "/{accountId}/upload/",
"downloadUrl": base + "/{accountId}/download/{blobId}/{name}?type={type}",
"state": s.sessionState(acct, user),
}
writeJSON(w, http.StatusOK, session, map[string]string{
"Cache-Control": "no-cache, no-store, must-revalidate",
})
}
// handleAPI serves POST /{account}/jmap (FR-J.2…FR-J.6).
func (s *Server) handleAPI(w http.ResponseWriter, r *http.Request) {
acct := s.authorize(w, r, r.PathValue("account"))
if acct == nil {
if s.cfg.Auth.Mode == "none" {
http.NotFound(w, r)
}
return
}
mediaType := r.Header.Get("Content-Type")
if i := strings.IndexByte(mediaType, ';'); i >= 0 {
mediaType = mediaType[:i]
}
if strings.TrimSpace(mediaType) != "application/json" {
writeJSON(w, http.StatusBadRequest, map[string]any{
"type": "urn:ietf:params:jmap:error:notJSON",
"status": 400,
"detail": "Content-Type must be application/json",
}, nil)
return
}
body, err := io.ReadAll(io.LimitReader(r.Body, maxSizeRequest+1))
if err != nil {
http.Error(w, "could not read request", http.StatusBadRequest)
return
}
if len(body) > maxSizeRequest {
writeJSON(w, http.StatusRequestEntityTooLarge, map[string]any{
"type": "urn:ietf:params:jmap:error:limit",
"status": http.StatusRequestEntityTooLarge,
"limit": "maxSizeRequest",
"detail": "request body exceeds maxSizeRequest",
}, nil)
return
}
user := username(r, acct)
jacct := &jmapapi.Account{
ID: acct.ID,
Store: s.store,
Backend: s.backends[acct.ID],
Capabilities: s.accountCapabilities(acct),
Identity: identityFor(acct),
SessionState: s.sessionState(acct, user),
}
status, resp := s.jmap.Dispatch(r.Context(), jacct, body)
writeJSON(w, status, resp, nil)
}
// canSend reports whether this account may be offered submission
// (FR-J.5). IMAP mode needs an SMTP server to send through *and* an IMAP
// backend, because the engine that submits is the same one that files the
// sent message and applies the caller's patches — an account with SMTP but
// no IMAP has no engine behind it (main skips those). Gmail API mode
// submits through the provider API and needs no SMTP block (D-API-6).
func canSend(acct *config.Account) bool {
if acct.Backend == "gmail_api" {
return true
}
return acct.SMTP != nil && acct.IMAP != nil
}
// canContacts applies the FR-P.3 gate: CardDAV configured and the first
// sync succeeded. nil readiness (a server built without engines) means
// "never" — the honest default.
func (s *Server) canContacts(acct *config.Account) bool {
return acct != nil && acct.CardDAV != nil && acct.CardDAV.URL != "" &&
s.contactsReady != nil && s.contactsReady(acct.ID)
}
// primaryAccounts names the account for every capability it offers
// (FR-J.5): mail always, submission with SMTP, contacts once the gate
// passes.
func primaryAccounts(s *Server, acct *config.Account) map[string]any {
out := map[string]any{"urn:ietf:params:jmap:mail": acct.ID}
if canSend(acct) {
out[jmapapi.SubmissionURN] = acct.ID
}
if s.canContacts(acct) {
out[jmapapi.ContactURN] = acct.ID
}
return out
}
// accountCapabilities is what this account offers (FR-J.5): core and
// mail always, submission only once it can actually send, contacts only
// once the FR-P.3 gate passes. The session advertises this set and
// dispatch accepts `using` entries from it, so the two can never drift
// apart.
func (s *Server) accountCapabilities(acct *config.Account) []string {
caps := []string{"urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"}
if canSend(acct) {
caps = append(caps, jmapapi.SubmissionURN)
}
if s.canContacts(acct) {
caps = append(caps, jmapapi.ContactURN)
}
return caps
}
// identityFor builds the account's sendable identity (FR-M.14): the
// configured address, signed with the account's display name, under an
// id that is stable across restarts. An account that cannot send has
// none — and then neither the capability nor Identity/get exist.
func identityFor(acct *config.Account) *jmapapi.Identity {
if !canSend(acct) || acct.Address == "" {
return nil
}
return &jmapapi.Identity{
ID: jmapapi.IdentityID(acct.ID),
Name: acct.Name,
Email: acct.Address,
}
}
func writeJSON(w http.ResponseWriter, status int, body any, extra map[string]string) {
for k, v := range extra {
w.Header().Set(k, v)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(body)
}