| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 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/rastrillo/rastrillo/migrate" |
| 15 | ) |
| 16 | |
| 17 | //go:embed agent_migrations/migrations/*.sql |
| 18 | var agentMigrations embed.FS |
| 19 | var agentSchema = func() *migrate.Set { |
| 20 | sub, err := fs.Sub(agentMigrations, "agent_migrations") |
| 21 | if err != nil { |
| 22 | panic(err) |
| 23 | } |
| 24 | return migrate.MustFromFS(sub, "calendar_agent_access") |
| 25 | }() |
| 26 | |
| 27 | func validateAgentConfig(cfg Config, home bool) error { |
| 28 | if cfg.AgentIssuer == "" && cfg.AgentTeam == "" { |
| 29 | return nil |
| 30 | } |
| 31 | if !home || cfg.AgentTeam == "" { |
| 32 | return errors.New("calendar: agent access requires Home and a team") |
| 33 | } |
| 34 | if _, err := access.NewRemoteVerifier(cfg.AgentIssuer, cfg.Origin+"/api/v1", cfg.Home.Issuer); err != nil { |
| 35 | return errors.New("calendar: invalid agent access configuration") |
| 36 | } |
| 37 | return nil |
| 38 | } |
| 39 | func (a *App) wireAgent() error { |
| 40 | if a.cfg.AgentIssuer == "" { |
| 41 | return nil |
| 42 | } |
| 43 | verifier, err := access.NewRemoteVerifier(a.cfg.AgentIssuer, a.cfg.Origin+"/api/v1", a.cfg.Home.Issuer) |
| 44 | if err != nil { |
| 45 | return err |
| 46 | } |
| 47 | return a.mountAgent(verifier, a.cfg.AgentIssuer, a.cfg.AgentTeam) |
| 48 | } |
| 49 | func (a *App) mountAgent(verifier access.Verifier, issuer, team string) error { |
| 50 | if a.cfg.Team != nil && (a.team == nil || a.team.Bindings().Team != team) { |
| 51 | return errors.New("calendar: agent team must match roster authority") |
| 52 | } |
| 53 | if _, err := migrate.Apply(context.Background(), a.db, agentSchema); err != nil { |
| 54 | return err |
| 55 | } |
| 56 | a.agentIssuer, a.agentTeam = issuer, team |
| 57 | if a.cfg.Home == nil || issuer == "" { |
| 58 | return errors.New("calendar: agent authority unavailable") |
| 59 | } |
| 60 | if err := a.db.G.Transaction(func(tx *gorm.DB) error { return a.bindAgent(tx) }); err != nil { |
| 61 | return err |
| 62 | } |
| 63 | registry, err := access.NewRegistry(a.agentOperations(team)...) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | a.agent = a.agentBoundary(verifier, registry.HTTPGuarded(access.VerifyFunc(func(ctx context.Context, token string) (access.Principal, error) { |
| 68 | p, err := verifier.Verify(ctx, token) |
| 69 | if err != nil { |
| 70 | return access.Principal{}, err |
| 71 | } |
| 72 | if _, err = a.agentPerson(ctx, p, team); err != nil { |
| 73 | return access.Principal{}, err |
| 74 | } |
| 75 | return p, nil |
| 76 | }), verifier, a.agentResponse)) |
| 77 | return nil |
| 78 | } |
| 79 | func (a *App) agentPerson(ctx context.Context, p access.Principal, team string) (Person, error) { |
| 80 | allowed := false |
| 81 | for _, t := range p.Teams { |
| 82 | allowed = allowed || t == team |
| 83 | } |
| 84 | if !allowed || p.Subject == "" || p.Client == "" || p.Grant == "" || a.home == nil { |
| 85 | return Person{}, access.Fail("forbidden", "This connection cannot access Calendar.") |
| 86 | } |
| 87 | linked, err := a.home.bridge.ResolveOn(ctx, a.db.G.Statement.ConnPool, p.Subject) |
| 88 | if errors.Is(err, linkstore.ErrStorage) { |
| 89 | return Person{}, access.Fail("unavailable", "Account verification is unavailable.") |
| 90 | } |
| 91 | if err != nil { |
| 92 | return Person{}, access.Fail("forbidden", "Link an admitted Calendar account to Home first.") |
| 93 | } |
| 94 | var person Person |
| 95 | if err = a.db.G.WithContext(ctx).First(&person, linked.ID).Error; err != nil { |
| 96 | return Person{}, access.Fail("unavailable", "Account verification is unavailable.") |
| 97 | } |
| 98 | if person.ID != linked.ID || person.Address != linked.Label || a.cfg.Team == nil && !a.home.admitted.allows(person.Address) { |
| 99 | return Person{}, access.Fail("forbidden", "This account cannot access Calendar.") |
| 100 | } |
| 101 | if state, ok := ctx.Value(agentResponseKey{}).(*agentResponseState); ok { |
| 102 | if state.person.ID != 0 && (state.person.ID != person.ID || state.person.Address != person.Address) { |
| 103 | return Person{}, access.Fail("forbidden", "The linked identity changed.") |
| 104 | } |
| 105 | state.person = person |
| 106 | } |
| 107 | if err := a.agentStanding(ctx, p.Subject, true); err != nil { |
| 108 | return Person{}, err |
| 109 | } |
| 110 | return person, nil |
| 111 | } |
| 112 | func agentFailure(err error) error { |
| 113 | if err == nil { |
| 114 | return nil |
| 115 | } |
| 116 | var failure access.Failure |
| 117 | if errors.As(err, &failure) { |
| 118 | return failure |
| 119 | } |
| 120 | if errors.Is(err, ErrNotFound) { |
| 121 | return access.Fail("not_found", "This calendar or event is unavailable.") |
| 122 | } |
| 123 | if errors.Is(err, ErrReadOnly) { |
| 124 | return access.Fail("forbidden", "This calendar is read-only.") |
| 125 | } |
| 126 | if _, ok := Invalid(err); ok { |
| 127 | return access.Fail("invalid", "Check the event fields and explicit time zone.") |
| 128 | } |
| 129 | return access.Fail("unavailable", "Calendar is unavailable. Try again.") |
| 130 | } |
| 131 | func (a *App) agentGuard(team, scope string, fn func(context.Context, access.Principal, Person, json.RawMessage) (any, error)) func(context.Context, access.Principal, json.RawMessage) (any, error) { |
| 132 | return func(ctx context.Context, p access.Principal, raw json.RawMessage) (any, error) { |
| 133 | var in struct { |
| 134 | Team string `json:"team"` |
| 135 | } |
| 136 | if strictjson.Unmarshal(raw, &in) != nil { |
| 137 | return nil, access.Fail("invalid", "Send valid Unicode JSON.") |
| 138 | } |
| 139 | if in.Team != team || !p.Allows(team, scope) { |
| 140 | return nil, access.Fail("forbidden", "This connection cannot access that team.") |
| 141 | } |
| 142 | person, err := a.agentPerson(ctx, p, team) |
| 143 | if err != nil { |
| 144 | return nil, err |
| 145 | } |
| 146 | ctx = a.agentContext(ctx) |
| 147 | out, err := fn(ctx, p, person, raw) |
| 148 | if err == nil { |
| 149 | if state, ok := ctx.Value(agentResponseKey{}).(*agentResponseState); ok { |
| 150 | state.selected = true |
| 151 | } |
| 152 | } |
| 153 | return out, agentFailure(err) |
| 154 | } |
| 155 | } |
| 156 | |