oficina / meet Public

Clone
git clone https://amadan.net/oficina/meet

Plain git — no account needed to clone.

Download

Download this file

1package calls
2
3import (
4 "amadan.net/oficina/calls/internal/calls/apns"
5 "encoding/base64"
6 "fmt"
7 "log/slog"
8 "net/http"
9 "strings"
10 "time"
11
12 "amadan.net/oficina/home/connect"
13 "amadan.net/oficina/home/roster"
14 "amadan.net/oficina/home/teammode"
15 "amadan.net/rastrillo/rastrillo/mail"
16
17 "amadan.net/oficina/calls/internal/calls/relay"
18)
19
20// EnvDevelopment is the one Env value that relaxes startup refusals.
21// Only the -env flag can select it (cmd/calls/main.go); an environment
22// variable cannot, so a misconfigured production box fails loudly
23// rather than quietly running with a zero key.
24const EnvDevelopment = "development"
25
26// Config is everything the app needs to start. Every field that is a
27// secret or a boundary is a startup refusal when it is missing outside
28// development, per spec §7.1.
29type Config struct {
30 Home *connect.Credentials
31 // Team is roster team mode's configuration (teammode.ConfigFromEnv
32 // with prefix CALLS: the five CALLS_TEAM_* variables, all or none),
33 // nil with team mode off. It exists only beside Home, and in team
34 // mode AllowedAddresses is optional: if set it only admits local
35 // sign-in, and gives nobody team standing (team_config.go).
36 Team *teammode.Config
37 // teamTransport and teamClock are for tests: a directory that
38 // answers in memory and a clock a test can step (teammodetest).
39 // Production leaves both nil.
40 teamTransport http.RoundTripper
41 teamClock roster.Clock
42 AgentIssuer string
43 AgentTeam string
44 // Env is "development" or anything else (treated as production).
45 Env string
46 // Origin is the app's external origin, scheme included
47 // ("https://calls.oficina.works"). CALLS_ORIGIN.
48 Origin string
49 // InstanceKey seals rastrillo/auth's pending blob and signs the
50 // pending-join cookie. CALLS_INSTANCE_KEY.
51 InstanceKey string
52 // DEK is the base64 data encryption key, exactly 32 bytes decoded.
53 // CALLS_DEK, set with `carlos secrets set`.
54 DEK string
55 // AllowedAddresses is the team: the addresses that are members.
56 // CALLS_ALLOWED_ADDRESSES, comma-separated. In team mode it is
57 // optional and only admits local sign-in: empty admits nobody
58 // locally, in every Env, and the roster decides standing.
59 AllowedAddresses []string
60
61 // The Apple push key, for telling a member who is not in the room
62 // that somebody is waiting. All four are required together or none
63 // of them: a half-configured push is a boot-time mistake, not a
64 // runtime surprise on the first knock.
65 //
66 // APNsKey is the .p8 Apple issued, PEM as downloaded, set with
67 // `carlos secrets set` and never committed. CALLS_APNS_KEY.
68 APNsKey string
69 // CALLS_APNS_KEY_ID, the ten characters Apple names the key by.
70 APNsKeyID string
71 // CALLS_APNS_TEAM_ID, the Apple developer team.
72 APNsTeamID string
73 // CALLS_APNS_TOPIC_IOS and CALLS_APNS_TOPIC_MACOS: the bundle
74 // identifier of the iPhone app and of the Mac app. Both are
75 // works.oficina.calls since 2026-11-14 (they were
76 // net.amadan.oficina.calls.ios and net.amadan.oficina.calls). Kept
77 // separate because a token is issued per app, and one sent under a
78 // topic that is not its app's is refused as DeviceTokenNotForTopic,
79 // which apns treats as Gone: the token is deleted.
80 APNsTopicIOS string
81 APNsTopicMacOS string
82 // CALLS_APNS_SANDBOX sends to Apple's sandbox host, which is where a
83 // development build's tokens live. A production token refused by the
84 // sandbox says nothing useful about why, so this is configuration
85 // rather than something to detect.
86 APNsSandbox bool
87 // Mailer delivers magic links. Nil outside development is refused;
88 // nil in development logs the link instead of sending it.
89 Mailer mail.Sender
90 // Relay is the raw $CARLOS_RELAY: the platform's base64-wrapped JSON
91 // list of regions, keys and username suffixes (platform relay design
92 // §2). Absent means the floor (§5.5). It is parsed once here and the
93 // decoded value is held in memory and never logged (§5.2).
94 Relay string
95 // Zone is the IANA zone start times are entered and shown in.
96 // CALLS_ZONE, default UTC. Per-person zones are deliberately not in
97 // version one.
98 Zone string
99 Logger *slog.Logger
100 // QualityReports is CALLS_QUALITY_REPORTS: "off" turns the automatic
101 // quality reports off (spec §15.26), and the endpoint answers 204 so
102 // clients stop sending. Anything else, unset included, is on.
103 QualityReports string
104 // ReportLogger is where quality and problem reports are written. The
105 // deployed main hands it a JSON handler, because the platform's
106 // `carlos logs --field` reads fields only from JSON lines. Nil is
107 // Logger.
108 ReportLogger *slog.Logger
109 // KeymailServers is rastrillo/auth's Config.KeymailServers. Nil, as
110 // cmd/calls leaves it, is any server an address's own _keymail
111 // delegation names, which rastrillo (v0.45) dials at public addresses
112 // only, on a transport of its own. Tests name their in-process fake's
113 // host here (keymailtest), the one way a probe still goes through the
114 // test's transport; an empty list is no Keymail at all.
115 KeymailServers []string
116}
117
118func (cfg Config) development() bool { return cfg.Env == EnvDevelopment }
119
120const devInstanceKey = "calls-development-instance-key-not-a-secret"
121
122var devDEK = make([]byte, 32)
123
124// resolved is Config after every refusal has been checked and every
125// default applied.
126type resolved struct {
127 home *connect.DecodedCredentials
128 // team is the verified team configuration, nil with team mode off.
129 team *teammode.Settings
130 Config
131 instanceKey string
132 dek []byte
133 mailer mail.Sender
134 apns *apns.Config
135 allow allowlist
136 relay []relay.Region
137 relayConfigured bool
138 zone *time.Location
139 qualityReports bool
140}
141
142func resolve(cfg Config) (resolved, error) {
143 if err := validateDelegatedConfig(cfg); err != nil {
144 return resolved{}, err
145 }
146 if cfg.Logger == nil {
147 cfg.Logger = slog.Default()
148 }
149 out := resolved{Config: cfg, qualityReports: qualityReportsOn(cfg.QualityReports)}
150 if out.ReportLogger == nil {
151 out.ReportLogger = cfg.Logger
152 }
153
154 if strings.TrimSpace(cfg.Origin) == "" {
155 if !cfg.development() {
156 return out, fmt.Errorf("calls: Config.Origin is empty and Env is %q, not %q; set $CALLS_ORIGIN", cfg.Env, EnvDevelopment)
157 }
158 out.Origin = "http://localhost:8080"
159 }
160
161 key := strings.TrimSpace(cfg.InstanceKey)
162 switch {
163 case key != "":
164 out.instanceKey = key
165 case cfg.development():
166 out.instanceKey = devInstanceKey
167 default:
168 return out, fmt.Errorf("calls: Config.InstanceKey is empty and Env is %q, not %q; refusing to start with no seal for the sign-in link and the join cookie; set $CALLS_INSTANCE_KEY", cfg.Env, EnvDevelopment)
169 }
170
171 raw := strings.TrimSpace(cfg.DEK)
172 switch {
173 case raw == "" && cfg.development():
174 out.dek = devDEK
175 case raw == "":
176 return out, fmt.Errorf("calls: Config.DEK is empty and Env is %q, not %q; a meeting's title is sealed at rest and a boot without a key would write plaintext into a replicated file; set $CALLS_DEK to 32 base64 bytes with `carlos secrets set`", cfg.Env, EnvDevelopment)
177 default:
178 b, err := base64.StdEncoding.DecodeString(raw)
179 if err != nil {
180 return out, fmt.Errorf("calls: $CALLS_DEK is not valid base64: %w", err)
181 }
182 if len(b) != 32 {
183 return out, fmt.Errorf("calls: $CALLS_DEK decodes to %d bytes, want exactly 32", len(b))
184 }
185 out.dek = b
186 }
187
188 // Wrapped here rather than at the call site: a delivery error names
189 // the recipient, and rastrillo/auth logs it (review C1-2). Doing it
190 // in resolve makes that unbypassable — every mailer the app ever
191 // holds has been through RedactMailErrors, including the one a test
192 // or another main passes in. The nil check above the wrap, not
193 // below: it is what refuses to start without a relay.
194 switch {
195 case cfg.Mailer != nil:
196 out.mailer = RedactMailErrors(cfg.Mailer, cfg.Logger)
197 case cfg.development():
198 out.mailer = RedactMailErrors(mail.Logged(cfg.Logger), cfg.Logger)
199 default:
200 return out, fmt.Errorf("calls: mail is not configured (CARLOS mail submission sets $CARLOS_MAIL=submit; otherwise set $CALLS_SMTP_HOST and $CALLS_SMTP_FROM, or $CARLOS_SMTP_*) and Env is %q, not %q; a sign-in that cannot send its link is not sign-in", cfg.Env, EnvDevelopment)
201 }
202
203 list, err := resolveAllowlist(cfg)
204 if err != nil {
205 return out, err
206 }
207 out.allow = list
208
209 push, err := resolveAPNs(cfg)
210 if err != nil {
211 return out, err
212 }
213 out.apns = push
214
215 regions, err := relay.Parse(cfg.Relay)
216 switch {
217 case err == nil:
218 out.relay = regions
219 out.relayConfigured = len(regions) > 0
220 case cfg.development():
221 // One line, the variable and not the value: the value carries a
222 // key. Development runs without a relay rather than refusing.
223 cfg.Logger.Warn("calls: CARLOS_RELAY is malformed and is ignored in development", "err", err.Error())
224 default:
225 return out, fmt.Errorf("calls: CARLOS_RELAY is present but malformed (%v); a relay the app cannot read is a relay it must not pretend to have", err)
226 }
227
228 zone := strings.TrimSpace(cfg.Zone)
229 if zone == "" {
230 zone = "UTC"
231 }
232 loc, err := time.LoadLocation(zone)
233 if err != nil {
234 return out, fmt.Errorf("calls: $CALLS_ZONE %q is not an IANA zone: %w", zone, err)
235 }
236 out.zone = loc
237 out.Zone = zone
238 out.home, err = connect.DecodeCredentials(cfg.Home, out.Origin)
239 if err != nil {
240 return out, err
241 }
242 // Team mode (team_config.go): verified against the Home credentials
243 // just decoded, before anything is migrated. Off, this is nil.
244 out.team, err = teamSettings(cfg.Team, out.home, out.Origin)
245 if err != nil {
246 return out, fmt.Errorf("calls: team configuration: %w", err)
247 }
248 return out, nil
249}
250
251// resolveAPNs reads the push key, or reads that there isn't one.
252//
253// All five settings are required together. A key with no topic, or a
254// topic with no key, is somebody halfway through configuring push, and
255// the moment to say so is boot — not the first knock, in a goroutine,
256// into a log nobody is reading.
257//
258// No key at all is not an error and not a degraded mode. Knocks still
259// reach every member who is in the room, over the streams they always
260// used. It is only the member who is elsewhere who goes untold, which
261// is exactly where this app was before any of this existed.
262func resolveAPNs(cfg Config) (*apns.Config, error) {
263 present := 0
264 for _, v := range []string{cfg.APNsKey, cfg.APNsKeyID, cfg.APNsTeamID, cfg.APNsTopicIOS, cfg.APNsTopicMacOS} {
265 if strings.TrimSpace(v) != "" {
266 present++
267 }
268 }
269 if present == 0 {
270 return nil, nil
271 }
272 if present < 5 {
273 return nil, fmt.Errorf("calls: Apple push is half-configured (%d of 5 settings present); set all of $CALLS_APNS_KEY, $CALLS_APNS_KEY_ID, $CALLS_APNS_TEAM_ID, $CALLS_APNS_TOPIC_IOS and $CALLS_APNS_TOPIC_MACOS, or none", present)
274 }
275 key, err := apns.ParseKey([]byte(cfg.APNsKey))
276 if err != nil {
277 // The error names the shape and never the bytes.
278 return nil, fmt.Errorf("calls: $CALLS_APNS_KEY is not usable: %w", err)
279 }
280 host := apns.HostProduction
281 if cfg.APNsSandbox {
282 host = apns.HostSandbox
283 }
284 return &apns.Config{
285 KeyID: strings.TrimSpace(cfg.APNsKeyID), TeamID: strings.TrimSpace(cfg.APNsTeamID), Key: key,
286 Topics: map[string]string{
287 "ios": strings.TrimSpace(cfg.APNsTopicIOS),
288 "macos": strings.TrimSpace(cfg.APNsTopicMacOS),
289 },
290 Host: host,
291 }, nil
292}
293