oficina / calendar Public

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

Plain git — no account needed to clone.

Download

Download this file

1package calendar
2
3import (
4 "context"
5 "crypto/hmac"
6 "encoding/json"
7 "errors"
8 "net/http"
9 "strings"
10
11 access "amadan.net/oficina/agent-access.git"
12 "amadan.net/oficina/home/connect/linkstore"
13 "gorm.io/gorm"
14)
15
16type agentBoundaryKey struct{}
17type agentRecheck func(context.Context, access.Principal) error
18
19func (a *App) agentBoundary(verifier access.Verifier, next http.Handler) http.Handler {
20 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21 parts := strings.Fields(r.Header.Get("Authorization"))
22 token := ""
23 if len(parts) == 2 {
24 token = parts[1]
25 }
26 check := agentRecheck(func(ctx context.Context, p access.Principal) error {
27 current, e := verifier.Verify(ctx, token)
28 if e != nil {
29 return e
30 }
31 if current.Subject != p.Subject || current.Client != p.Client || current.Grant != p.Grant || !current.Allows(a.agentTeam, "calendar:write") {
32 return access.Fail("forbidden", "This connection is no longer authorized.")
33 }
34 return nil
35 })
36 ctx := context.WithValue(r.Context(), agentResponseKey{}, &agentResponseState{})
37 next.ServeHTTP(&agentLeaseWriter{ResponseWriter: w, state: ctx.Value(agentResponseKey{}).(*agentResponseState), team: a.cfg.Team != nil}, r.WithContext(context.WithValue(ctx, agentBoundaryKey{}, check)))
38 })
39}
40func (a *App) bindAgent(tx *gorm.DB) error {
41 raw, _ := json.Marshal([]string{a.cfg.Home.Issuer, a.agentIssuer, a.agentTeam})
42 want, e := a.store.seal.Fingerprint("agent.authority.v1", string(raw))
43 if e != nil {
44 return e
45 }
46 if e = tx.Exec("INSERT INTO calendar_agent_state(id,keycheck) SELECT 1,? WHERE NOT EXISTS(SELECT 1 FROM calendar_agent_events) AND NOT EXISTS(SELECT 1 FROM calendar_agent_updates) ON CONFLICT(id) DO NOTHING", want).Error; e != nil {
47 return e
48 }
49 var got string
50 if e = tx.Raw("SELECT keycheck FROM calendar_agent_state WHERE id=1").Scan(&got).Error; e != nil {
51 return e
52 }
53 if !hmac.Equal([]byte(got), []byte(want)) {
54 return access.Fail("unavailable", "Agent receipt authority requires an explicit migration.")
55 }
56 return nil
57}
58func (a *App) checkAgentWriter(ctx context.Context, tx *gorm.DB, p access.Principal, expected Person, calendarID int64) (Person, error) {
59 check, ok := ctx.Value(agentBoundaryKey{}).(agentRecheck)
60 if !ok || check == nil {
61 return Person{}, access.Fail("forbidden", "Agent authority is unavailable.")
62 }
63 if e := check(ctx, p); e != nil {
64 return Person{}, e
65 }
66 person, e := a.home.bridge.ResolveOn(ctx, tx.Statement.ConnPool, p.Subject)
67 if errors.Is(e, linkstore.ErrRefused) {
68 return Person{}, access.Fail("forbidden", "The linked identity is unavailable.")
69 }
70 if e != nil {
71 return Person{}, e
72 }
73 if person.ID != expected.ID || person.Label != expected.Address {
74 return Person{}, access.Fail("forbidden", "The linked identity changed.")
75 }
76 id := person.ID
77
78 var current Person
79 if e := tx.First(&current, id).Error; e != nil {
80 return Person{}, e
81 }
82 if current.Address != expected.Address || a.cfg.Team == nil && !a.home.admitted.allows(current.Address) {
83 return Person{}, access.Fail("forbidden", "This account is no longer admitted.")
84 }
85 if err := a.agentStanding(ctx, p.Subject, false); err != nil {
86 return Person{}, err
87 }
88 store := a.agentStore(tx)
89 cal, e := store.Calendar(ctx, id, calendarID)
90 if e != nil {
91 return Person{}, e
92 }
93 if cal.ArchivedAt != nil {
94 return Person{}, ErrNotFound
95 }
96 if !CanWrite(cal.Role) {
97 return Person{}, ErrReadOnly
98 }
99 if err := a.agentStanding(ctx, p.Subject, false); err != nil {
100 return Person{}, err
101 }
102 return current, nil
103}
104