| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | |
| 8 | access "amadan.net/oficina/agent-access.git" |
| 9 | "amadan.net/oficina/home/connect" |
| 10 | "amadan.net/oficina/home/handoff/seal" |
| 11 | "gorm.io/gorm" |
| 12 | ) |
| 13 | |
| 14 | func (a *slideAgent) actor(tx *gorm.DB, p access.Principal, u User) (string, error) { |
| 15 | coordinate := a.digest("actor", []string{a.identityIssuer, a.issuer, p.Subject, p.Client, a.team, strconv.FormatInt(u.ID, 10)}) |
| 16 | sealer, e := seal.New(a.key, "oficina/slides/agent/v1") |
| 17 | if e != nil { |
| 18 | return "", e |
| 19 | } |
| 20 | ciphertext, e := sealer.Seal("client/"+coordinate, []byte(p.Client)) |
| 21 | if e != nil { |
| 22 | return "", e |
| 23 | } |
| 24 | e = tx.Exec("INSERT INTO slides_connections(coordinate,operator_id,client_sealed) VALUES(?,?,?) ON CONFLICT(coordinate) DO NOTHING", coordinate, u.ID, ciphertext).Error |
| 25 | return "connection:" + coordinate, e |
| 26 | } |
| 27 | func (a *app) connectionLabels(ctx context.Context, actors []string) map[string]string { |
| 28 | out := map[string]string{} |
| 29 | if a.cfg.Home == nil { |
| 30 | return out |
| 31 | } |
| 32 | keys := []string{} |
| 33 | for _, actor := range actors { |
| 34 | if key, ok := strings.CutPrefix(actor, "connection:"); ok { |
| 35 | keys = append(keys, key) |
| 36 | } |
| 37 | } |
| 38 | if len(keys) == 0 { |
| 39 | return out |
| 40 | } |
| 41 | var rows []struct { |
| 42 | Coordinate string |
| 43 | ClientSealed []byte |
| 44 | Email string |
| 45 | } |
| 46 | if e := a.db.G.WithContext(ctx).Raw("SELECT c.coordinate,c.client_sealed,u.email FROM slides_connections c JOIN users u ON u.id=c.operator_id WHERE c.coordinate IN ?", keys).Scan(&rows).Error; e != nil { |
| 47 | return out |
| 48 | } |
| 49 | settings, e := connect.DecodeCredentials(a.cfg.Home, a.cfg.Origin) |
| 50 | if e != nil || settings == nil { |
| 51 | return out |
| 52 | } |
| 53 | sealer, e := seal.New(settings.DEK, "oficina/slides/agent/v1") |
| 54 | if e != nil { |
| 55 | return out |
| 56 | } |
| 57 | for _, r := range rows { |
| 58 | client, e := sealer.Open("client/"+r.Coordinate, r.ClientSealed) |
| 59 | if e == nil { |
| 60 | out["connection:"+r.Coordinate] = agentMark + agentPrincipal(string(client), r.Email) |
| 61 | } |
| 62 | } |
| 63 | return out |
| 64 | } |
| 65 | |