Bind event retry authority and recheck delegation before commit
11 files changed,
+395
−25
docs/superpowers/specs/2026-10-25-agent-authority-hardening.md+29 −0go.mod+1 −1go.sum+0 −2internal/calendar/agent_access.go+17 −5internal/calendar/agent_access_test.go+2 −2internal/calendar/agent_authority.go+94 −0internal/calendar/agent_authority_test.go+138 −0internal/calendar/agent_continuity_test.go+85 −0internal/calendar/agent_create.go+16 −7internal/calendar/agent_migrations/migrations/0002_authority.sql+4 −0internal/calendar/app.go+9 −8
CI passed — run details
diff --git a/docs/superpowers/specs/2026-10-25-agent-authority-hardening.md b/docs/superpowers/specs/2026-10-25-agent-authority-hardening.md| new file mode 100644 |
| index 0000000..51e3cae |
| --- /dev/null |
| +++ b/docs/superpowers/specs/2026-10-25-agent-authority-hardening.md |
| @@ -0,0 +1,29 @@ |
| +# Delegated event authority and retry hardening |
| + |
| +Status: bounded design approved by GPT-6 Astra on 2026-10-25 in the joint |
| +Memoria/Calendar/Calls mutation review. Source, CI and canary remain separate. |
| + |
| +Persist a keyed marker binding the content fingerprint key, exact Home issuer, |
| +delegation issuer and instance team. Adopt only under the SQLite writer |
| +reservation when all agent receipts are empty. Validate on enablement and before |
| +every receipt lookup. Changed or missing binding with existing receipts requires |
| +explicit migration; do not reset it. Include issuers in receipt coordinates. |
| + |
| +Each request retains a private callback to the original credential's underlying |
| +verifier. Compare subject, client, grant, required team and scope. The callback |
| +must not call local database or rate-limiter wrappers while a transaction holds |
| +the writer. An absent callback refuses agent mutation. |
| + |
| +After reserving the writer, check delegation, the transaction-local Home link, |
| +original local person/address, current admission and calendar write permission. |
| +Repeat immediately before every successful commit, including an existing receipt |
| +retry. Use the refreshed person for default reminders. Effect/reminder/receipt |
| +remain one transaction. Reject malformed Unicode before lossy JSON decoding. |
| + |
| +Discriminating tests cover final revocation, transaction-triggered unlink and |
| +calendar grant removal, rollback of all effects, changed key/issuer/team and |
| +restart binding, unchanged retries, and invalid Unicode. Mutation checks and |
| +full CI precede source approval and deployed canary review. |
| + |
| +This bounded write-hardening change does not establish Docs-equivalent guarded |
| +response emission for the existing Registry.HTTP read routes. |
diff --git a/go.mod b/go.mod| index c231446..0b10dfd 100644 |
| --- a/go.mod |
| +++ b/go.mod |
| @@ -21,7 +21,7 @@ require ( |
| github.com/chromedp/cdproto v0.0.0-20250724212937-08a3db8b4327 |
| github.com/chromedp/sysutil v1.1.0 // indirect |
| github.com/dustin/go-humanize v1.0.1 // indirect |
| - github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 // indirect |
| + github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 |
| github.com/gobwas/httphead v0.1.0 // indirect |
| github.com/gobwas/pool v0.2.1 // indirect |
| github.com/gobwas/ws v1.4.0 // indirect |
diff --git a/go.sum b/go.sum| index bf681e9..cda714f 100644 |
| --- a/go.sum |
| +++ b/go.sum |
| @@ -1,5 +1,3 @@ |
| -amadan.net/oficina/agent-access.git v0.0.0-20261024224328-3966c68144d1 h1:GknXgICfFlRNJTwXrZRdeTRS6DtQ37b3e7COQX/IxAs= |
| -amadan.net/oficina/agent-access.git v0.0.0-20261024224328-3966c68144d1/go.mod h1:Ph9PIa3VE1IEaUzZiQH6D+IQ6HThWWGfVr0ecMMgrFA= |
| amadan.net/oficina/agent-access.git v0.0.0-20261024232549-8ed8ba826000 h1:L07nc+dmLI7c1PBkSkbnrjOK5f8p6OBbA/yTxznxCAw= |
| amadan.net/oficina/agent-access.git v0.0.0-20261024232549-8ed8ba826000/go.mod h1:Ph9PIa3VE1IEaUzZiQH6D+IQ6HThWWGfVr0ecMMgrFA= |
| amadan.net/oficina/estilo.git v0.0.0-20261001093750-530cb932e768 h1:bVscoa9fH5LdLDjlyecZhrX7JzWPT0blb57MH0FIm2U= |
diff --git a/internal/calendar/agent_access.go b/internal/calendar/agent_access.go| index dc51386..5d92e82 100644 |
| --- a/internal/calendar/agent_access.go |
| +++ b/internal/calendar/agent_access.go |
| @@ -5,6 +5,8 @@ import ( |
| "embed" |
| "encoding/json" |
| "errors" |
| + strictjson "github.com/go-json-experiment/json" |
| + "gorm.io/gorm" |
| "io/fs" |
| access "amadan.net/oficina/agent-access.git" |
| @@ -42,17 +44,24 @@ func (a *App) wireAgent() error { |
| if err != nil { |
| return err |
| } |
| - return a.mountAgent(verifier, a.cfg.AgentTeam) |
| + return a.mountAgent(verifier, a.cfg.AgentIssuer, a.cfg.AgentTeam) |
| } |
| -func (a *App) mountAgent(verifier access.Verifier, team string) error { |
| +func (a *App) mountAgent(verifier access.Verifier, issuer, team string) error { |
| if _, err := migrate.Apply(context.Background(), a.db, agentSchema); err != nil { |
| return err |
| } |
| + a.agentIssuer, a.agentTeam = issuer, team |
| + if a.cfg.Home == nil || issuer == "" { |
| + return errors.New("calendar: agent authority unavailable") |
| + } |
| + if err := a.db.G.Transaction(func(tx *gorm.DB) error { return a.bindAgent(tx) }); err != nil { |
| + return err |
| + } |
| registry, err := access.NewRegistry(a.agentOperations(team)...) |
| if err != nil { |
| return err |
| } |
| - a.agent = registry.HTTP(access.VerifyFunc(func(ctx context.Context, token string) (access.Principal, error) { |
| + a.agent = a.agentBoundary(verifier, registry.HTTP(access.VerifyFunc(func(ctx context.Context, token string) (access.Principal, error) { |
| p, err := verifier.Verify(ctx, token) |
| if err != nil { |
| return access.Principal{}, err |
| @@ -61,7 +70,7 @@ func (a *App) mountAgent(verifier access.Verifier, team string) error { |
| return access.Principal{}, err |
| } |
| return p, nil |
| - })) |
| + }))) |
| return nil |
| } |
| func (a *App) agentPerson(ctx context.Context, p access.Principal, team string) (Person, error) { |
| @@ -112,7 +121,10 @@ func (a *App) agentGuard(team, scope string, fn func(context.Context, access.Pri |
| var in struct { |
| Team string `json:"team"` |
| } |
| - if json.Unmarshal(raw, &in) != nil || in.Team != team || !p.Allows(team, scope) { |
| + if strictjson.Unmarshal(raw, &in) != nil { |
| + return nil, access.Fail("invalid", "Send valid Unicode JSON.") |
| + } |
| + if in.Team != team || !p.Allows(team, scope) { |
| return nil, access.Fail("forbidden", "This connection cannot access that team.") |
| } |
| person, err := a.agentPerson(ctx, p, team) |
diff --git a/internal/calendar/agent_access_test.go b/internal/calendar/agent_access_test.go| index c59a5b7..09ef887 100644 |
| --- a/internal/calendar/agent_access_test.go |
| +++ b/internal/calendar/agent_access_test.go |
| @@ -32,7 +32,7 @@ func linkedCalendarAgent(t *testing.T) (*homeRouterFixture, *access.Principal, P |
| } |
| homeRedirect(t, f.do("POST", "/account/home/confirm", "")) |
| p := &access.Principal{Subject: "opaque-agent-alice", Client: "agent-one", Grant: "grant-one", Teams: []string{"acme"}, Scopes: []string{"calendar:read", "calendar:write"}} |
| - if err := f.app.mountAgent(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return *p, nil }), "acme"); err != nil { |
| + if err := f.app.mountAgent(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return *p, nil }), "https://connect.example.test", "acme"); err != nil { |
| t.Fatal(err) |
| } |
| person, err := f.app.agentPerson(t.Context(), *p, "acme") |
| @@ -115,7 +115,7 @@ func TestAgentEventRetryPermissionsAndSealing(t *testing.T) { |
| t.Fatal("plaintext checkpointed content", column) |
| } |
| } |
| - coordinate, _ := json.Marshal([]string{p.Subject, p.Client, "acme", input["client_id"].(string)}) |
| + coordinate, _ := json.Marshal([]string{f.app.cfg.Home.Issuer, f.app.agentIssuer, p.Subject, p.Client, "acme", input["client_id"].(string)}) |
| expected, err := f.app.store.seal.Fingerprint("agent.event.coordinate.v1", string(coordinate)) |
| if err != nil { |
| t.Fatal(err) |
diff --git a/internal/calendar/agent_authority.go b/internal/calendar/agent_authority.go| new file mode 100644 |
| index 0000000..bec2cdb |
| --- /dev/null |
| +++ b/internal/calendar/agent_authority.go |
| @@ -0,0 +1,94 @@ |
| +package calendar |
| + |
| +import ( |
| + "context" |
| + "crypto/hmac" |
| + "database/sql" |
| + "encoding/json" |
| + "errors" |
| + "net/http" |
| + "strings" |
| + |
| + access "amadan.net/oficina/agent-access.git" |
| + "gorm.io/gorm" |
| +) |
| + |
| +type agentBoundaryKey struct{} |
| +type agentRecheck func(context.Context, access.Principal) error |
| + |
| +func (a *App) agentBoundary(verifier access.Verifier, next http.Handler) http.Handler { |
| + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + parts := strings.Fields(r.Header.Get("Authorization")) |
| + token := "" |
| + if len(parts) == 2 { |
| + token = parts[1] |
| + } |
| + check := agentRecheck(func(ctx context.Context, p access.Principal) error { |
| + current, e := verifier.Verify(ctx, token) |
| + if e != nil { |
| + return e |
| + } |
| + if current.Subject != p.Subject || current.Client != p.Client || current.Grant != p.Grant || !current.Allows(a.agentTeam, "calendar:write") { |
| + return access.Fail("forbidden", "This connection is no longer authorized.") |
| + } |
| + return nil |
| + }) |
| + next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), agentBoundaryKey{}, check))) |
| + }) |
| +} |
| +func (a *App) bindAgent(tx *gorm.DB) error { |
| + raw, _ := json.Marshal([]string{a.cfg.Home.Issuer, a.agentIssuer, a.agentTeam}) |
| + want, e := a.store.seal.Fingerprint("agent.authority.v1", string(raw)) |
| + if e != nil { |
| + return e |
| + } |
| + if e = tx.Exec("INSERT INTO calendar_agent_state(id,keycheck) SELECT 1,? WHERE NOT EXISTS(SELECT 1 FROM calendar_agent_events) ON CONFLICT(id) DO NOTHING", want).Error; e != nil { |
| + return e |
| + } |
| + var got string |
| + if e = tx.Raw("SELECT keycheck FROM calendar_agent_state WHERE id=1").Scan(&got).Error; e != nil { |
| + return e |
| + } |
| + if !hmac.Equal([]byte(got), []byte(want)) { |
| + return access.Fail("unavailable", "Agent receipt authority requires an explicit migration.") |
| + } |
| + return nil |
| +} |
| +func (a *App) checkAgentWriter(ctx context.Context, tx *gorm.DB, p access.Principal, expected Person, calendarID int64) (Person, error) { |
| + check, ok := ctx.Value(agentBoundaryKey{}).(agentRecheck) |
| + if !ok || check == nil { |
| + return Person{}, access.Fail("forbidden", "Agent authority is unavailable.") |
| + } |
| + if e := check(ctx, p); e != nil { |
| + return Person{}, e |
| + } |
| + var id int64 |
| + if e := tx.Raw("SELECT person_id FROM home_identity_links WHERE issuer=? AND subject=?", a.cfg.Home.Issuer, p.Subject).Row().Scan(&id); e != nil { |
| + if errors.Is(e, sql.ErrNoRows) { |
| + return Person{}, access.Fail("forbidden", "The linked identity is unavailable.") |
| + } |
| + return Person{}, e |
| + } |
| + if id != expected.ID { |
| + return Person{}, access.Fail("forbidden", "The linked identity changed.") |
| + } |
| + var current Person |
| + if e := tx.First(¤t, id).Error; e != nil { |
| + return Person{}, e |
| + } |
| + if current.Address != expected.Address || !a.home.admitted.allows(current.Address) { |
| + return Person{}, access.Fail("forbidden", "This account is no longer admitted.") |
| + } |
| + store := NewStore(tx, a.store.seal, a.logger) |
| + cal, e := store.Calendar(ctx, id, calendarID) |
| + if e != nil { |
| + return Person{}, e |
| + } |
| + if cal.ArchivedAt != nil { |
| + return Person{}, ErrNotFound |
| + } |
| + if !CanWrite(cal.Role) { |
| + return Person{}, ErrReadOnly |
| + } |
| + return current, nil |
| +} |
diff --git a/internal/calendar/agent_authority_test.go b/internal/calendar/agent_authority_test.go| new file mode 100644 |
| index 0000000..ce4ae06 |
| --- /dev/null |
| +++ b/internal/calendar/agent_authority_test.go |
| @@ -0,0 +1,138 @@ |
| +package calendar |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "encoding/json" |
| + "net/http/httptest" |
| + "strings" |
| + "sync/atomic" |
| + "testing" |
| + |
| + access "amadan.net/oficina/agent-access.git" |
| + "amadan.net/oficina/calendar/internal/calendar/seal" |
| + "gorm.io/gorm" |
| +) |
| + |
| +func TestAgentEventPrecommitAuthority(t *testing.T) { |
| + for _, mode := range []string{"reserved-revoke", "final-revoke", "unlink", "grant"} { |
| + t.Run(mode, func(t *testing.T) { |
| + f, p, _, cal := linkedCalendarAgent(t) |
| + var checks atomic.Int64 |
| + if e := f.app.mountAgent(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| + n := checks.Add(1) |
| + if mode == "reserved-revoke" && n >= 2 || mode == "final-revoke" && n >= 3 { |
| + return access.Principal{}, access.Fail("unauthenticated", "Revoked.") |
| + } |
| + return *p, nil |
| + }), f.app.agentIssuer, "acme"); e != nil { |
| + t.Fatal(e) |
| + } |
| + if mode == "reserved-revoke" { |
| + if _, e := f.app.db.Writer().Exec("CREATE TRIGGER body_must_not_run BEFORE INSERT ON events BEGIN SELECT RAISE(ABORT,'body attempted'); END"); e != nil { |
| + t.Fatal(e) |
| + } |
| + } |
| + change := "" |
| + if mode == "unlink" { |
| + change = "DELETE FROM home_identity_links" |
| + } |
| + if mode == "grant" { |
| + change = "UPDATE grants SET role='viewer'" |
| + } |
| + if change != "" { |
| + if _, e := f.app.db.Writer().Exec("CREATE TRIGGER lose_agent_authority AFTER UPDATE OF event_id ON calendar_agent_events BEGIN " + change + "; END"); e != nil { |
| + t.Fatal(e) |
| + } |
| + } |
| + rec := calendarAgentCall(f.app, "calendar_create_event", eventAgentInput(cal)) |
| + if rec.Code != 401 && rec.Code != 403 { |
| + t.Fatal("lost authority committed", mode, rec.Code, rec.Body.String()) |
| + } |
| + for _, table := range []string{"events", "reminders", "reminder_queues", "calendar_agent_events"} { |
| + var n int |
| + if e := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM " + table).Scan(&n); e != nil || n != 0 { |
| + t.Fatal("partial event", table, n, e) |
| + } |
| + } |
| + }) |
| + } |
| +} |
| +func TestAgentEventAuthorityBindingRestart(t *testing.T) { |
| + f, p, _, cal := linkedCalendarAgent(t) |
| + input := eventAgentInput(cal) |
| + first := calendarAgentCall(f.app, "calendar_create_event", input) |
| + if first.Code != 200 { |
| + t.Fatal(first.Code) |
| + } |
| + cfg := f.app.cfg |
| + cfg.AgentIssuer = f.app.agentIssuer |
| + cfg.AgentTeam = "acme" |
| + if e := f.app.Close(); e != nil { |
| + t.Fatal(e) |
| + } |
| + restarted, e := New(cfg) |
| + if e != nil { |
| + t.Fatal(e) |
| + } |
| + defer restarted.Close() |
| + if e = restarted.mountAgent(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return *p, nil }), cfg.AgentIssuer, cfg.AgentTeam); e != nil { |
| + t.Fatal(e) |
| + } |
| + rec := calendarAgentCall(restarted, "calendar_create_event", input) |
| + if rec.Code != 200 || rec.Body.String() != first.Body.String() { |
| + t.Fatal("restart changed receipt", rec.Code, rec.Body.String()) |
| + } |
| + for _, kind := range []string{"issuer", "home", "team", "key"} { |
| + t.Run(kind, func(t *testing.T) { |
| + oldIssuer, oldHome, oldTeam, oldSeal := restarted.agentIssuer, restarted.cfg.Home.Issuer, restarted.agentTeam, restarted.store.seal |
| + defer func() { |
| + restarted.agentIssuer = oldIssuer |
| + restarted.cfg.Home.Issuer = oldHome |
| + restarted.agentTeam = oldTeam |
| + restarted.store.seal = oldSeal |
| + }() |
| + switch kind { |
| + case "issuer": |
| + restarted.agentIssuer = "https://other-connect.test" |
| + case "home": |
| + restarted.cfg.Home.Issuer = "https://other-home.test" |
| + case "team": |
| + restarted.agentTeam = "green" |
| + case "key": |
| + restarted.store.seal, _ = seal.New(bytes.Repeat([]byte{29}, 32)) |
| + } |
| + if e := restarted.db.G.Transaction(func(tx *gorm.DB) error { return restarted.bindAgent(tx) }); e == nil { |
| + t.Fatal("authority change accepted") |
| + } |
| + }) |
| + } |
| + if _, e = restarted.db.Writer().Exec("DELETE FROM calendar_agent_state"); e != nil { |
| + t.Fatal(e) |
| + } |
| + if e = restarted.db.G.Transaction(func(tx *gorm.DB) error { return restarted.bindAgent(tx) }); e == nil { |
| + t.Fatal("existing receipts adopted without marker") |
| + } |
| +} |
| +func TestAgentEventRejectsMalformedUnicode(t *testing.T) { |
| + f, _, _, cal := linkedCalendarAgent(t) |
| + input := eventAgentInput(cal) |
| + input["title"] = "REPLACEME" |
| + raw, _ := json.Marshal(input) |
| + for _, bad := range []string{`\ud800`, string([]byte{255})} { |
| + // Use the actual mounted handler; do not predecode with encoding/json. |
| + rec := calendarRawAgentCall(f.app, strings.Replace(string(raw), "REPLACEME", bad, 1)) |
| + if rec.Code != 422 { |
| + t.Fatal("malformed event Unicode", rec.Code, rec.Body.String()) |
| + } |
| + } |
| +} |
| + |
| +func calendarRawAgentCall(a *App, raw string) *httptest.ResponseRecorder { |
| + r := httptest.NewRequest("POST", homeTestOrigin+"/api/v1/operations/calendar_create_event", strings.NewReader(raw)) |
| + r.Header.Set("Content-Type", "application/json") |
| + r.Header.Set("Authorization", "Bearer fixture") |
| + w := httptest.NewRecorder() |
| + a.agent.ServeHTTP(w, r) |
| + return w |
| +} |
diff --git a/internal/calendar/agent_continuity_test.go b/internal/calendar/agent_continuity_test.go| new file mode 100644 |
| index 0000000..40d0567 |
| --- /dev/null |
| +++ b/internal/calendar/agent_continuity_test.go |
| @@ -0,0 +1,85 @@ |
| +package calendar |
| + |
| +import ( |
| + "context" |
| + "sync/atomic" |
| + "testing" |
| + |
| + access "amadan.net/oficina/agent-access.git" |
| +) |
| + |
| +func TestAgentEventRetryFinalCheckAndLiveMarker(t *testing.T) { |
| + for _, mode := range []string{"retry-final", "marker-create", "marker-retry"} { |
| + t.Run(mode, func(t *testing.T) { |
| + f, p, _, cal := linkedCalendarAgent(t) |
| + input := eventAgentInput(cal) |
| + if rec := calendarAgentCall(f.app, "calendar_create_event", input); rec.Code != 200 { |
| + t.Fatal(rec.Code) |
| + } |
| + want := 503 |
| + if mode == "retry-final" { |
| + want = 401 |
| + var checks atomic.Int64 |
| + if e := f.app.mountAgent(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| + if checks.Add(1) >= 3 { |
| + return access.Principal{}, access.Fail("unauthenticated", "Revoked.") |
| + } |
| + return *p, nil |
| + }), f.app.agentIssuer, "acme"); e != nil { |
| + t.Fatal(e) |
| + } |
| + } else { |
| + if _, e := f.app.db.Writer().Exec("UPDATE calendar_agent_state SET keycheck='damaged'"); e != nil { |
| + t.Fatal(e) |
| + } |
| + if mode == "marker-create" { |
| + input["client_id"] = "15bb7913-1316-4b96-bc3a-4b4cded1ded4" |
| + } |
| + } |
| + if rec := calendarAgentCall(f.app, "calendar_create_event", input); rec.Code != want { |
| + t.Fatal("boundary ignored", mode, rec.Code, rec.Body.String()) |
| + } |
| + for _, table := range []string{"events", "calendar_agent_events"} { |
| + var n int |
| + if e := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM " + table).Scan(&n); e != nil || n != 1 { |
| + t.Fatal("retry wrote extra effect", n, e) |
| + } |
| + } |
| + }) |
| + } |
| +} |
| +func TestAgentEventVerifierContinuity(t *testing.T) { |
| + for _, field := range []string{"subject", "client", "grant", "team", "scope"} { |
| + t.Run(field, func(t *testing.T) { |
| + f, p, _, cal := linkedCalendarAgent(t) |
| + var checks atomic.Int64 |
| + if e := f.app.mountAgent(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| + current := *p |
| + if checks.Add(1) > 1 { |
| + switch field { |
| + case "subject": |
| + current.Subject = "different" |
| + case "client": |
| + current.Client = "different" |
| + case "grant": |
| + current.Grant = "different" |
| + case "team": |
| + current.Teams = []string{"green"} |
| + case "scope": |
| + current.Scopes = []string{"calendar:read"} |
| + } |
| + } |
| + return current, nil |
| + }), f.app.agentIssuer, "acme"); e != nil { |
| + t.Fatal(e) |
| + } |
| + if rec := calendarAgentCall(f.app, "calendar_create_event", eventAgentInput(cal)); rec.Code != 403 { |
| + t.Fatal("changed principal accepted", field, rec.Code) |
| + } |
| + var n int |
| + if e := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM events").Scan(&n); e != nil || n != 0 { |
| + t.Fatal("changed principal wrote", n, e) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/calendar/agent_create.go b/internal/calendar/agent_create.go| index 9cccf88..a4f7548 100644 |
| --- a/internal/calendar/agent_create.go |
| +++ b/internal/calendar/agent_create.go |
| @@ -33,7 +33,7 @@ func (a *App) agentCreateEvent(ctx context.Context, principal access.Principal, |
| return nil, access.Fail("invalid", "Invalid event input.") |
| } |
| uid, err := uuid.Parse(in.ClientID) |
| - if err != nil || uid == uuid.Nil || len(in.ClientID) != 36 { |
| + if err != nil || uid == uuid.Nil || uid.String() != in.ClientID { |
| return nil, access.Fail("invalid", "Supply a nonzero UUID client_id.") |
| } |
| event := EventInput{CalendarID: in.CalendarID, Title: in.Title, Location: in.Location, Notes: in.Notes, AllDay: in.AllDay, StartLocal: in.StartLocal, EndLocal: in.EndLocal, TZ: in.TZ, Kind: in.Kind, Status: in.Status} |
| @@ -49,7 +49,7 @@ func (a *App) agentCreateEvent(ctx context.Context, principal access.Principal, |
| if !in.AllDay && (!agentUnambiguous(event.StartLocal, event.TZ) || !agentUnambiguous(event.EndLocal, event.TZ)) { |
| return nil, access.Fail("invalid", "Choose unambiguous local times outside a DST gap or fold.") |
| } |
| - coordinate, _ := json.Marshal([]string{principal.Subject, principal.Client, in.Team, uid.String()}) |
| + coordinate, _ := json.Marshal([]string{a.cfg.Home.Issuer, a.agentIssuer, principal.Subject, principal.Client, in.Team, uid.String()}) |
| key, err := a.store.seal.Fingerprint("agent.event.coordinate.v1", string(coordinate)) |
| if err != nil { |
| return nil, err |
| @@ -64,8 +64,15 @@ func (a *App) agentCreateEvent(ctx context.Context, principal access.Principal, |
| } |
| var eventID int64 |
| err = a.store.g.WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
| - // This first write reserves the SQLite writer before permissions and receipt |
| - // are read; the effect, reminder and receipt commit atomically. |
| + if e := a.bindAgent(tx); e != nil { |
| + return e |
| + } |
| + fresh, e := a.checkAgentWriter(ctx, tx, principal, person, in.CalendarID) |
| + if e != nil { |
| + return e |
| + } |
| + // bindAgent reserved the writer before current authority was checked. |
| + // The effect, reminder and receipt commit atomically. |
| if err := tx.Exec("INSERT INTO calendar_agent_events(coordinate,content) VALUES(?,?) ON CONFLICT(coordinate) DO NOTHING", key, fingerprint).Error; err != nil { |
| return err |
| } |
| @@ -93,20 +100,22 @@ func (a *App) agentCreateEvent(ctx context.Context, principal access.Principal, |
| return err |
| } |
| eventID = id.Int64 |
| - return nil |
| + _, e := a.checkAgentWriter(ctx, tx, principal, person, in.CalendarID) |
| + return e |
| } |
| created, err := current.CreateEvent(ctx, person.ID, event) |
| if err != nil { |
| return err |
| } |
| - if err = current.EnsureDefaultReminder(ctx, person, created); err != nil { |
| + if err = current.EnsureDefaultReminder(ctx, fresh, created); err != nil { |
| return err |
| } |
| if err = tx.Exec("UPDATE calendar_agent_events SET event_id=? WHERE coordinate=?", created.ID, key).Error; err != nil { |
| return err |
| } |
| eventID = created.ID |
| - return nil |
| + _, e = a.checkAgentWriter(ctx, tx, principal, person, in.CalendarID) |
| + return e |
| }) |
| if err != nil { |
| return nil, err |
diff --git a/internal/calendar/agent_migrations/migrations/0002_authority.sql b/internal/calendar/agent_migrations/migrations/0002_authority.sql| new file mode 100644 |
| index 0000000..c705aa1 |
| --- /dev/null |
| +++ b/internal/calendar/agent_migrations/migrations/0002_authority.sql |
| @@ -0,0 +1,4 @@ |
| +CREATE TABLE calendar_agent_state ( |
| + id INTEGER PRIMARY KEY CHECK(id=1), |
| + keycheck TEXT NOT NULL |
| +); |
diff --git a/internal/calendar/app.go b/internal/calendar/app.go| index 090147f..6d8fab4 100644 |
| --- a/internal/calendar/app.go |
| +++ b/internal/calendar/app.go |
| @@ -30,14 +30,15 @@ import ( |
| // development mail recorder both hang off this struct, and every later |
| // test depends on them. |
| type App struct { |
| - agent http.Handler |
| - native *nativeauth.Auth |
| - home *homeConnection |
| - cfg Config |
| - db *db.DB |
| - mux *http.ServeMux |
| - logger *slog.Logger |
| - store *Store |
| + agent http.Handler |
| + agentIssuer, agentTeam string |
| + native *nativeauth.Auth |
| + home *homeConnection |
| + cfg Config |
| + db *db.DB |
| + mux *http.ServeMux |
| + logger *slog.Logger |
| + store *Store |
| // mailRecorder is non-nil only in development — see buildMailer. |
| mailRecorder *recordingMailer |