| 1 | package hostactors |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "github.com/carlosframework/platform/internal/release" |
| 7 | ) |
| 8 | |
| 9 | type ConsoleInvitation struct { |
| 10 | V int `json:"v"` |
| 11 | Bucket string `json:"bucket"` |
| 12 | Console Console `json:"console"` |
| 13 | } |
| 14 | |
| 15 | func (d Directory) InviteConsole(ctx context.Context, signer release.Signer, c Console) error { |
| 16 | o, err := d.Read(ctx) |
| 17 | if err != nil { |
| 18 | return err |
| 19 | } |
| 20 | if err := d.validateInvitedConsole(o, c.InstanceID, c); err != nil { |
| 21 | return err |
| 22 | } |
| 23 | wire, err := release.SignRootEnvelope(signer, ConsoleInvitation{V: 1, Bucket: d.Bucket, Console: c}) |
| 24 | if err != nil { |
| 25 | return err |
| 26 | } |
| 27 | return release.CreateExact(ctx, d.Store, "control/host-console-invitations/"+c.InstanceID+".json", wire) |
| 28 | } |
| 29 | |
| 30 | func (d Directory) InvitedConsole(ctx context.Context, o Observed, id string) (Console, error) { |
| 31 | if !segment(id) { |
| 32 | return Console{}, ErrRoster |
| 33 | } |
| 34 | wire, _, err := d.Store.Get(ctx, "control/host-console-invitations/"+id+".json") |
| 35 | if err != nil { |
| 36 | return Console{}, err |
| 37 | } |
| 38 | var invitation ConsoleInvitation |
| 39 | if release.VerifyRootEnvelope(d.PublicKey, wire, &invitation) != nil || invitation.V != 1 || invitation.Bucket != d.Bucket { |
| 40 | return Console{}, ErrRoster |
| 41 | } |
| 42 | if err := d.validateInvitedConsole(o, id, invitation.Console); err != nil { |
| 43 | return Console{}, err |
| 44 | } |
| 45 | return invitation.Console, nil |
| 46 | } |
| 47 | |
| 48 | func (d Directory) validateInvitedConsole(o Observed, id string, c Console) error { |
| 49 | if !segment(id) || c.InstanceID != id { |
| 50 | return ErrRoster |
| 51 | } |
| 52 | if _, exists := o.Roster.Consoles[id]; exists { |
| 53 | return ErrRoster |
| 54 | } |
| 55 | next := Clone(o.Roster) |
| 56 | next.Consoles[id] = c |
| 57 | return next.Validate(d.Bucket) |
| 58 | } |
| 59 | |