| 1 | package runnerproto |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | type LoginDisplay struct { |
| 10 | VerificationURI, UserCode string |
| 11 | ExpiresAt time.Time |
| 12 | } |
| 13 | type AccountSetup struct { |
| 14 | ID, RunnerID, Provider, Label, State, Mode string |
| 15 | Challenge *LoginDisplay |
| 16 | ConnectionID string |
| 17 | Revision int64 |
| 18 | } |
| 19 | |
| 20 | // AccountSetupResult deliberately has no provider challenge ID, home or return code. |
| 21 | type AccountSetupResult struct { |
| 22 | ID, State, AccountID, ProviderVersion string |
| 23 | Revision int64 |
| 24 | Challenge *LoginDisplay |
| 25 | } |
| 26 | type AccountSetupRequest struct { |
| 27 | Version int `json:"version"` |
| 28 | ID string `json:"id"` |
| 29 | Result *AccountSetupResult `json:"result,omitempty"` |
| 30 | } |
| 31 | |
| 32 | // Terminal passthrough has no instance display contract. Reject all supplied |
| 33 | // display data until a separately verified projection is introduced. |
| 34 | func ValidLoginDisplay(_, _ string, d *LoginDisplay, _ time.Time) bool { return d == nil } |
| 35 | |
| 36 | type AccountSetupAPI interface { |
| 37 | RunnerAccountSetup(context.Context, RunnerPrincipal, string) (AccountSetup, error) |
| 38 | ReportAccountSetup(context.Context, RunnerPrincipal, AccountSetupResult) (AccountSetup, error) |
| 39 | } |
| 40 | |
| 41 | func (s *Handler) runnerAccountSetup(w http.ResponseWriter, r *http.Request) { |
| 42 | p, _, ok := s.Bearer(w, r) |
| 43 | if !ok { |
| 44 | return |
| 45 | } |
| 46 | var q AccountSetupRequest |
| 47 | if !s.Body(w, r, &q, 4096) { |
| 48 | return |
| 49 | } |
| 50 | if q.Version != 2 || q.ID == "" || len(q.ID) > 128 || q.Result != nil && q.Result.ID != q.ID { |
| 51 | s.Failure(w, s.Invalid) |
| 52 | return |
| 53 | } |
| 54 | if s.AccountSetups == nil { |
| 55 | s.Failure(w, s.Unavailable) |
| 56 | return |
| 57 | } |
| 58 | var out AccountSetup |
| 59 | var err error |
| 60 | if q.Result == nil { |
| 61 | out, err = s.AccountSetups.RunnerAccountSetup(r.Context(), p, q.ID) |
| 62 | } else { |
| 63 | out, err = s.AccountSetups.ReportAccountSetup(r.Context(), p, *q.Result) |
| 64 | } |
| 65 | if err != nil { |
| 66 | s.Failure(w, err) |
| 67 | return |
| 68 | } |
| 69 | s.JSON(w, 200, WorkResponse{Version: 2, Result: out}) |
| 70 | } |
| 71 | |