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 "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 delegatedBoundaryKey struct{}
17type delegatedRecheck func(context.Context, access.Principal) error
18
19func (a *app) delegatedBoundary(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 := delegatedRecheck(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, "calls:write") {
32 return access.Fail("forbidden", "This connection is no longer authorized.")
33 }
34 return nil
35 })
36 state := &delegatedResponseState{}
37 ctx := context.WithValue(r.Context(), delegatedResponseKey{}, state)
38 next.ServeHTTP(&delegatedLeaseWriter{ResponseWriter: w, state: state, team: a.cfg.Team != nil}, r.WithContext(context.WithValue(ctx, delegatedBoundaryKey{}, check)))
39 })
40}
41func (a *app) bindDelegated(tx *gorm.DB) error {
42 raw, _ := json.Marshal([]string{a.cfg.Home.Issuer, a.agentIssuer, a.agentTeam})
43 want, e := a.sealer.Fingerprint("connection.authority.v1", string(raw))
44 if e != nil {
45 return e
46 }
47 if e = tx.Exec("INSERT INTO calls_agent_state(id,keycheck) SELECT 1,? WHERE NOT EXISTS(SELECT 1 FROM calls_agent_meetings) AND NOT EXISTS(SELECT 1 FROM calls_connections) AND NOT EXISTS(SELECT 1 FROM calls_agent_schedules) ON CONFLICT(id) DO NOTHING", want).Error; e != nil {
48 return e
49 }
50 var got string
51 if e = tx.Raw("SELECT keycheck FROM calls_agent_state WHERE id=1").Scan(&got).Error; e != nil {
52 return e
53 }
54 if !hmac.Equal([]byte(got), []byte(want)) {
55 return access.Fail("unavailable", "Agent receipt authority requires an explicit migration.")
56 }
57 return nil
58}
59func (a *app) checkDelegatedWriter(ctx context.Context, tx *gorm.DB, p access.Principal, expected Person) error {
60 check, ok := ctx.Value(delegatedBoundaryKey{}).(delegatedRecheck)
61 if !ok || check == nil {
62 return access.Fail("forbidden", "Agent authority is unavailable.")
63 }
64 if e := check(ctx, p); e != nil {
65 return e
66 }
67 person, e := a.home.bridge.ResolveOn(ctx, tx.Statement.ConnPool, p.Subject)
68 if errors.Is(e, linkstore.ErrRefused) {
69 return access.Fail("forbidden", "The linked identity is unavailable.")
70 }
71 if e != nil {
72 return e
73 }
74 if person.ID != expected.ID || person.Label != expected.Email {
75 return access.Fail("forbidden", "The linked identity changed.")
76 }
77 id := person.ID
78
79 currentStore := *a.store
80 currentStore.g = tx
81 current, exists, e := currentStore.personByID(ctx, id)
82 if e != nil {
83 return e
84 }
85 if !exists || current.Email != expected.Email {
86 return access.Fail("forbidden", "This account is no longer admitted.")
87 }
88 return a.delegatedStanding(ctx, p.Subject, current, false)
89}
90