| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/hmac" |
| 6 | "crypto/sha256" |
| 7 | "embed" |
| 8 | "encoding/hex" |
| 9 | "encoding/json" |
| 10 | "errors" |
| 11 | "io/fs" |
| 12 | |
| 13 | access "amadan.net/oficina/agent-access.git" |
| 14 | "amadan.net/oficina/home/connect/linkstore" |
| 15 | "amadan.net/oficina/home/roster" |
| 16 | "amadan.net/rastrillo/rastrillo/migrate" |
| 17 | "gorm.io/gorm" |
| 18 | ) |
| 19 | |
| 20 | //go:embed agent_migrations/migrations/*.sql |
| 21 | var agentMigrationFS embed.FS |
| 22 | var agentSchema = func() *migrate.Set { |
| 23 | sub, _ := fs.Sub(agentMigrationFS, "agent_migrations") |
| 24 | return migrate.MustFromFS(sub, "slides_agent") |
| 25 | }() |
| 26 | |
| 27 | const slideAgentLimit = 1 << 20 |
| 28 | |
| 29 | type slideAgent struct { |
| 30 | app *app |
| 31 | issuer, identityIssuer, team string |
| 32 | key []byte |
| 33 | } |
| 34 | type slideAgentContext struct { |
| 35 | list bool |
| 36 | member User |
| 37 | verify func(context.Context) error |
| 38 | slides map[int64]bool |
| 39 | } |
| 40 | type slideAgentKey struct{} |
| 41 | |
| 42 | func slideAgentFailure(err error) error { |
| 43 | if err == nil { |
| 44 | return nil |
| 45 | } |
| 46 | var f access.Failure |
| 47 | if errors.As(err, &f) { |
| 48 | return f |
| 49 | } |
| 50 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 51 | return access.Fail("not_found", "This deck is unavailable.") |
| 52 | } |
| 53 | if errors.Is(err, linkstore.ErrRefused) { |
| 54 | return access.Fail("forbidden", "This Home identity is not admitted.") |
| 55 | } |
| 56 | return access.Fail("unavailable", "Deck authority or storage is unavailable.") |
| 57 | } |
| 58 | func (a *slideAgent) digest(domain string, value any) string { |
| 59 | raw, _ := json.Marshal(value) |
| 60 | mac := hmac.New(sha256.New, a.key) |
| 61 | mac.Write([]byte("oficina/slides/agent/v1/" + domain + "\x00")) |
| 62 | mac.Write(raw) |
| 63 | return hex.EncodeToString(mac.Sum(nil)) |
| 64 | } |
| 65 | func (a *slideAgent) bind(tx *gorm.DB) error { |
| 66 | want := a.digest("keycheck", []string{"immutable-authority", a.identityIssuer, a.issuer, a.team}) |
| 67 | if e := tx.Exec("INSERT INTO slides_agent_state(id,keycheck) SELECT 1,? WHERE NOT EXISTS(SELECT 1 FROM slides_agent_receipts) AND NOT EXISTS(SELECT 1 FROM slides_connections) AND NOT EXISTS(SELECT 1 FROM slides_agent_edits) ON CONFLICT(id) DO NOTHING", want).Error; e != nil { |
| 68 | return e |
| 69 | } |
| 70 | var got string |
| 71 | if e := tx.Raw("SELECT keycheck FROM slides_agent_state WHERE id=1").Scan(&got).Error; e != nil { |
| 72 | return e |
| 73 | } |
| 74 | if !hmac.Equal([]byte(got), []byte(want)) { |
| 75 | return access.Fail("unavailable", "Agent receipt authority requires an explicit migration.") |
| 76 | } |
| 77 | return nil |
| 78 | } |
| 79 | func (a *slideAgent) member(ctx context.Context, p access.Principal, write bool) (User, error) { |
| 80 | scope := "slides:read" |
| 81 | if write { |
| 82 | scope = "slides:write" |
| 83 | } |
| 84 | if p.Subject == "" || p.Client == "" || len(p.Client) > 256 || p.Grant == "" || !p.Allows(a.team, scope) { |
| 85 | return User{}, access.Fail("forbidden", "This connection does not allow that team and operation.") |
| 86 | } |
| 87 | if a.app.home == nil { |
| 88 | return User{}, access.Fail("unavailable", "Home identity resolution is unavailable.") |
| 89 | } |
| 90 | person, e := a.app.home.ResolveOn(ctx, a.app.db.G.Statement.ConnPool, p.Subject) |
| 91 | if e != nil { |
| 92 | return User{}, slideAgentFailure(e) |
| 93 | } |
| 94 | var u User |
| 95 | if e = a.app.db.G.WithContext(ctx).First(&u, person.ID).Error; e != nil { |
| 96 | return User{}, slideAgentFailure(e) |
| 97 | } |
| 98 | if u.ID <= 0 || person.Label == "" || u.Email != person.Label || a.app.team == nil && !a.app.allowed.allows(u.Email) { |
| 99 | return User{}, slideAgentFailure(linkstore.ErrRefused) |
| 100 | } |
| 101 | if a.app.team != nil { |
| 102 | if state, ok := ctx.Value(slideAgentKey{}).(*slideAgentContext); ok && state != nil { |
| 103 | u.team = state.member.team |
| 104 | if u.team == nil || u.team.Subject() != p.Subject { |
| 105 | return User{}, slideAgentFailure(linkstore.ErrRefused) |
| 106 | } |
| 107 | } else { |
| 108 | u.team = &teamAccess{Access: a.app.team.Access(ctx, p.Subject)} |
| 109 | } |
| 110 | } |
| 111 | return u, nil |
| 112 | } |
| 113 | func (a *slideAgent) creator(u User) error { |
| 114 | if a.app.team == nil { |
| 115 | if a.app.allowed.allows(u.Email) { |
| 116 | return nil |
| 117 | } |
| 118 | return linkstore.ErrRefused |
| 119 | } |
| 120 | switch u.team.state() { |
| 121 | case roster.Member: |
| 122 | return nil |
| 123 | case roster.NotMember: |
| 124 | return access.Fail("forbidden", "Creating a deck requires current team membership.") |
| 125 | default: |
| 126 | return access.Fail("unavailable", "Team membership cannot be verified now.") |
| 127 | } |
| 128 | } |
| 129 | func slideAgentMember(ctx context.Context) User { |
| 130 | if state, ok := ctx.Value(slideAgentKey{}).(*slideAgentContext); ok && state != nil { |
| 131 | return state.member |
| 132 | } |
| 133 | return User{} |
| 134 | } |
| 135 | func (a *slideAgent) remember(ctx context.Context, id int64) { |
| 136 | if state, ok := ctx.Value(slideAgentKey{}).(*slideAgentContext); ok && state != nil { |
| 137 | state.slides[id] = true |
| 138 | } |
| 139 | } |
| 140 | func (a *slideAgent) writer(ctx context.Context, tx *gorm.DB, p access.Principal, u User) error { |
| 141 | state, ok := ctx.Value(slideAgentKey{}).(*slideAgentContext) |
| 142 | if !ok || state == nil || state.verify == nil { |
| 143 | return linkstore.ErrRefused |
| 144 | } |
| 145 | if e := state.verify(ctx); e != nil { |
| 146 | return e |
| 147 | } |
| 148 | person, e := a.app.home.ResolveOn(ctx, tx.Statement.ConnPool, p.Subject) |
| 149 | if e != nil { |
| 150 | return e |
| 151 | } |
| 152 | if person.ID != u.ID || person.Label != u.Email { |
| 153 | return linkstore.ErrRefused |
| 154 | } |
| 155 | id := person.ID |
| 156 | |
| 157 | var current User |
| 158 | if e := tx.First(¤t, id).Error; e != nil { |
| 159 | return e |
| 160 | } |
| 161 | if current.Email != u.Email { |
| 162 | return linkstore.ErrRefused |
| 163 | } |
| 164 | current.team = u.team |
| 165 | return a.creator(current) |
| 166 | } |
| 167 | func slideAgentBounded(value any) (any, error) { |
| 168 | body, e := json.Marshal(value) |
| 169 | if e != nil { |
| 170 | return nil, slideAgentFailure(e) |
| 171 | } |
| 172 | if len(body) > slideAgentLimit { |
| 173 | return nil, access.Fail("too_large", "The deck result exceeds 1 MiB.") |
| 174 | } |
| 175 | return value, nil |
| 176 | } |
| 177 | |