| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/hex" |
| 6 | "encoding/json" |
| 7 | "strings" |
| 8 | |
| 9 | access "amadan.net/oficina/agent-access.git" |
| 10 | "github.com/google/uuid" |
| 11 | "gorm.io/gorm" |
| 12 | ) |
| 13 | |
| 14 | // Only a keyed fingerprint is retained. Each destination's atomic create |
| 15 | // receipt owns progress; the connection service retains no workflow data. |
| 16 | func (a *app) delegatedReserveSchedule(ctx context.Context, p access.Principal, person Person, raw json.RawMessage) (any, error) { |
| 17 | var in struct { |
| 18 | Team string `json:"team"` |
| 19 | ClientID string `json:"client_id"` |
| 20 | Fingerprint string `json:"fingerprint"` |
| 21 | } |
| 22 | if json.Unmarshal(raw, &in) != nil { |
| 23 | return nil, access.Fail("invalid", "Invalid schedule reservation.") |
| 24 | } |
| 25 | id, err := uuid.Parse(in.ClientID) |
| 26 | digest, e := hex.DecodeString(in.Fingerprint) |
| 27 | if err != nil || id == uuid.Nil || id.String() != in.ClientID || e != nil || len(digest) != 32 || strings.ToLower(in.Fingerprint) != in.Fingerprint { |
| 28 | return nil, access.Fail("invalid", "Supply a canonical UUID and SHA-256 fingerprint.") |
| 29 | } |
| 30 | coordinates, _ := json.Marshal([]string{a.cfg.Home.Issuer, a.agentIssuer, p.Subject, p.Client, in.Team, "calls_reserve_schedule", in.ClientID}) |
| 31 | key, err := a.sealer.Fingerprint("connection.schedule.coordinate.v1", string(coordinates)) |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | content, err := a.sealer.Fingerprint("connection.schedule.content.v1", in.Fingerprint) |
| 36 | if err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | err = a.store.g.WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
| 40 | if err := a.bindDelegated(tx); err != nil { |
| 41 | return err |
| 42 | } |
| 43 | if err := a.checkDelegatedWriter(ctx, tx, p, person); err != nil { |
| 44 | return err |
| 45 | } |
| 46 | current := *a.store |
| 47 | current.g = tx |
| 48 | return current.guard(ctx, func() error { |
| 49 | if err := tx.Exec("INSERT INTO calls_agent_schedules(coordinate,content) VALUES(?,?) ON CONFLICT(coordinate) DO NOTHING", key, content).Error; err != nil { |
| 50 | return err |
| 51 | } |
| 52 | var saved string |
| 53 | if err := tx.Raw("SELECT content FROM calls_agent_schedules WHERE coordinate=?", key).Row().Scan(&saved); err != nil { |
| 54 | return err |
| 55 | } |
| 56 | if saved != content { |
| 57 | return access.Fail("conflict", "This schedule client_id was used with different arguments.") |
| 58 | } |
| 59 | return a.checkDelegatedWriter(ctx, tx, p, person) |
| 60 | }) |
| 61 | }) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | return map[string]any{"reserved": true}, nil |
| 66 | } |
| 67 | |
| 68 | func (a *app) delegatedScheduleOperation(team string) access.Operation { |
| 69 | return access.Operation{Name: "calls_reserve_schedule", Description: "Reserve a fingerprint for a resumable schedule. Creates no meeting or event and sends no invitations. Reuse the UUID and fingerprint only for the same workflow.", Scope: "calls:write", Idempotent: true, |
| 70 | Input: delegatedInput(map[string]any{"client_id": delegatedString(36), "fingerprint": map[string]any{"type": "string", "pattern": "^[0-9a-f]{64}$"}}, "client_id", "fingerprint"), |
| 71 | Output: delegatedObject(map[string]any{"reserved": map[string]any{"const": true}}, "reserved"), Invoke: a.delegatedGuard(team, "calls:write", a.delegatedReserveSchedule)} |
| 72 | } |
| 73 | |