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 "embed"
6 "encoding/json"
7 "errors"
8 strictjson "github.com/go-json-experiment/json"
9 "gorm.io/gorm"
10 "io/fs"
11
12 access "amadan.net/oficina/agent-access.git"
13 "amadan.net/oficina/home/connect/linkstore"
14 "amadan.net/oficina/home/teammode"
15 "amadan.net/rastrillo/rastrillo/migrate"
16)
17
18//go:embed delegated_migrations/migrations/*.sql
19var delegatedFiles embed.FS
20var delegatedSchema = func() *migrate.Set {
21 sub, _ := fs.Sub(delegatedFiles, "delegated_migrations")
22 return migrate.MustFromFS(sub, "calls_agent_access")
23}()
24
25func validateDelegatedConfig(cfg Config) error {
26 if cfg.AgentIssuer == "" && cfg.AgentTeam == "" {
27 return nil
28 }
29 if cfg.Home == nil || cfg.AgentIssuer == "" || cfg.AgentTeam == "" || len(cfg.AgentTeam) > 64 {
30 return errors.New("calls: incomplete agent configuration")
31 }
32 if cfg.Team != nil && cfg.AgentTeam != cfg.Team.TeamID {
33 return errors.New("calls: agent and roster teams differ")
34 }
35 _, err := access.NewRemoteVerifier(cfg.AgentIssuer, cfg.Origin+"/api/v1", cfg.Home.Issuer)
36 return err
37}
38func (a *app) wireDelegated() error {
39 if a.cfg.AgentIssuer == "" {
40 return nil
41 }
42 v, err := access.NewRemoteVerifier(a.cfg.AgentIssuer, a.cfg.Origin+"/api/v1", a.cfg.Home.Issuer)
43 if err != nil {
44 return err
45 }
46 return a.mountDelegated(v, a.cfg.AgentIssuer, a.cfg.AgentTeam)
47}
48func (a *app) mountDelegated(v access.Verifier, issuer, team string) error {
49 if a.cfg.Team != nil && (a.team == nil || team != a.cfg.Team.TeamID) {
50 return errors.New("calls: agent and roster teams differ")
51 }
52 if _, err := migrate.Apply(context.Background(), a.db, delegatedSchema); err != nil {
53 return err
54 }
55 a.agentIssuer, a.agentTeam = issuer, team
56 if a.cfg.Home == nil || issuer == "" {
57 return errors.New("calls: agent authority unavailable")
58 }
59 if err := a.db.G.Transaction(func(tx *gorm.DB) error { return a.bindDelegated(tx) }); err != nil {
60 return err
61 }
62 reg, err := access.NewRegistry(a.delegatedOperations(team)...)
63 if err != nil {
64 return err
65 }
66 a.delegated = a.delegatedBoundary(v, reg.HTTPGuarded(access.VerifyFunc(func(ctx context.Context, token string) (access.Principal, error) {
67 p, err := v.Verify(ctx, token)
68 if err != nil {
69 return access.Principal{}, err
70 }
71 if _, err = a.delegatedPerson(ctx, p, team); err != nil {
72 return access.Principal{}, err
73 }
74 coordinate, _ := json.Marshal([]string{p.Subject, p.Client})
75 key, err := a.sealer.Fingerprint("connection.rate.v1", string(coordinate))
76 if err != nil {
77 return access.Principal{}, err
78 }
79 if ok, _ := a.apiLimiter.allow("connection:"+key, a.store.now()); !ok {
80 return access.Principal{}, access.Fail("rate_limited", "Calls request limit reached. Try again later.")
81 }
82 return p, nil
83 }), v, a.delegatedResponse))
84 return nil
85}
86func (a *app) delegatedPerson(ctx context.Context, p access.Principal, team string) (Person, error) {
87 allowed := false
88 for _, t := range p.Teams {
89 allowed = allowed || t == team
90 }
91 if !allowed || p.Subject == "" || p.Client == "" || len(p.Client) > 256 || p.Grant == "" || a.home == nil {
92 return Person{}, access.Fail("forbidden", "This connection cannot access Calls.")
93 }
94 linked, err := a.home.bridge.ResolveOn(ctx, a.db.G.Statement.ConnPool, p.Subject)
95 if errors.Is(err, linkstore.ErrStorage) {
96 return Person{}, access.Fail("unavailable", "Account verification is unavailable.")
97 }
98 if err != nil {
99 return Person{}, access.Fail("forbidden", "Link an admitted Calls account to Home first.")
100 }
101 person, ok, err := a.store.personByID(ctx, linked.ID)
102 if err != nil {
103 return Person{}, access.Fail("unavailable", "Account verification is unavailable.")
104 }
105 if !ok || linked.ID <= 0 || linked.Label == "" || person.Email != linked.Label {
106 return Person{}, access.Fail("forbidden", "This account cannot access Calls.")
107 }
108 if err := a.delegatedStanding(ctx, p.Subject, person, true); err != nil {
109 return Person{}, err
110 }
111 if state, ok := ctx.Value(delegatedResponseKey{}).(*delegatedResponseState); ok {
112 if state.person.ID != 0 && (state.person.ID != person.ID || state.person.Email != person.Email) {
113 return Person{}, access.Fail("forbidden", "The linked identity changed.")
114 }
115 state.person = person
116 }
117 return person, nil
118}
119func delegatedFailure(err error) error {
120 if err == nil {
121 return nil
122 }
123 var f access.Failure
124 if errors.As(err, &f) {
125 return f
126 }
127 return access.Fail("unavailable", "Calls is unavailable. Try again.")
128}
129func (a *app) delegatedGuard(team, scope string, fn func(context.Context, access.Principal, Person, json.RawMessage) (any, error)) func(context.Context, access.Principal, json.RawMessage) (any, error) {
130 return func(ctx context.Context, p access.Principal, raw json.RawMessage) (any, error) {
131 var in struct {
132 Team string `json:"team"`
133 }
134 if strictjson.Unmarshal(raw, &in) != nil {
135 return nil, access.Fail("invalid", "Send valid Unicode JSON.")
136 }
137 if in.Team != team || !p.Allows(team, scope) {
138 return nil, access.Fail("forbidden", "This connection cannot access that team.")
139 }
140 person, err := a.delegatedPerson(ctx, p, team)
141 if err != nil {
142 return nil, err
143 }
144 if a.cfg.Team != nil {
145 state, _ := ctx.Value(delegatedResponseKey{}).(*delegatedResponseState)
146 if state == nil {
147 return nil, access.Fail("unavailable", "Team authority is unavailable.")
148 }
149 ctx = teammode.WithAccess(ctx, state.standing)
150 }
151 out, err := fn(ctx, p, person, raw)
152 if err == nil {
153 if state, ok := ctx.Value(delegatedResponseKey{}).(*delegatedResponseState); ok {
154 state.selected = true
155 }
156 }
157 return out, delegatedFailure(err)
158 }
159}
160