| 1 | package github |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "gorm.io/gorm" |
| 6 | "net/url" |
| 7 | ) |
| 8 | |
| 9 | func (s *Store) claimCallback(ctx context.Context, p Principal, state, stage string) (Connection, error) { |
| 10 | var c Connection |
| 11 | var refused error |
| 12 | if state == "" { |
| 13 | return c, ErrAccess |
| 14 | } |
| 15 | err := s.write(ctx, p, func(tx *gorm.DB) error { |
| 16 | column := "manifest_hash" |
| 17 | if stage == "installation" { |
| 18 | column = "installation_hash" |
| 19 | } |
| 20 | if err := tx.Where(column+" = ?", digest(state)).First(&c).Error; err != nil { |
| 21 | return ErrAccess |
| 22 | } |
| 23 | if c.Owner != p.Address || c.Session != digest(p.Session) || c.Origin != s.Origin { |
| 24 | return ErrAccess |
| 25 | } |
| 26 | if !s.now().Before(c.RedirectUntil) { |
| 27 | if stage == "manifest" { |
| 28 | erase(&c, StateManifestExpired) |
| 29 | } else if !s.now().Before(c.PendingUntil) { |
| 30 | erase(&c, StateExpired) |
| 31 | } else { |
| 32 | c.InstallationHash = "" |
| 33 | c.State = StateInstallationExpired |
| 34 | } |
| 35 | if err := s.save(tx, p, &c, "forge.expire_redirect"); err != nil { |
| 36 | return err |
| 37 | } |
| 38 | refused = &Refusal{s.view(c), ErrAccess} |
| 39 | return nil |
| 40 | } |
| 41 | if stage == "manifest" { |
| 42 | c.ManifestHash = "" |
| 43 | c.State = StateConverting |
| 44 | } else { |
| 45 | c.InstallationHash = "" |
| 46 | } |
| 47 | return s.save(tx, p, &c, "forge.claim_"+stage) |
| 48 | }) |
| 49 | if err != nil { |
| 50 | return c, err |
| 51 | } |
| 52 | return c, refused |
| 53 | } |
| 54 | |
| 55 | // fenced loads the claimed conversion inside a write and refuses a late result. |
| 56 | func (s *Store) fenced(tx *gorm.DB, claimed Connection, result *Connection) error { |
| 57 | if e := tx.First(result, "id = ?", claimed.ID).Error; e != nil { |
| 58 | return e |
| 59 | } |
| 60 | if result.Generation != claimed.Generation || result.State != StateConverting { |
| 61 | return ErrAccess |
| 62 | } |
| 63 | var slot Pending |
| 64 | if tx.First(&slot, "id = ?", 1).Error != nil || slot.ConnectionID != result.ID { |
| 65 | return ErrAccess |
| 66 | } |
| 67 | return nil |
| 68 | } |
| 69 | func (s *Store) Convert(ctx context.Context, p Principal, state, code string) (View, error) { |
| 70 | // The code's shape is checked before the single-use state is spent. |
| 71 | if !ValidCode(code) { |
| 72 | return View{}, ErrAccess |
| 73 | } |
| 74 | claimed, err := s.claimCallback(ctx, p, state, "manifest") |
| 75 | if err != nil { |
| 76 | return s.view(claimed), err |
| 77 | } |
| 78 | app, callErr := s.API.Convert(ctx, code) |
| 79 | if callErr == nil { |
| 80 | if app.Owner.Type != claimed.AccountType || !equalLogin(app.Owner.Login, claimed.Login) { |
| 81 | callErr = ErrAccess |
| 82 | } else { |
| 83 | // Record the created App before verifying it, so a lost conversion |
| 84 | // still points cleanup at this App. |
| 85 | err = s.write(ctx, p, func(tx *gorm.DB) error { |
| 86 | var c Connection |
| 87 | if e := s.fenced(tx, claimed, &c); e != nil { |
| 88 | return e |
| 89 | } |
| 90 | c.AppID, c.Slug = app.ID, app.Slug |
| 91 | if e := s.save(tx, p, &c, "forge.convert_app"); e != nil { |
| 92 | return e |
| 93 | } |
| 94 | claimed = c |
| 95 | return nil |
| 96 | }) |
| 97 | if err != nil { |
| 98 | return View{}, err |
| 99 | } |
| 100 | app, callErr = s.API.Verify(ctx, app) |
| 101 | } |
| 102 | } |
| 103 | var result Connection |
| 104 | err = s.write(ctx, p, func(tx *gorm.DB) error { |
| 105 | if e := s.fenced(tx, claimed, &result); e != nil { |
| 106 | return e |
| 107 | } |
| 108 | if callErr != nil { |
| 109 | erase(&result, StateFailed) |
| 110 | return s.save(tx, p, &result, "forge.convert_failed") |
| 111 | } |
| 112 | sealed, e := sealKey(s.Root, s.Origin, result.ID, app.ID, app.pem) |
| 113 | if e != nil { |
| 114 | return e |
| 115 | } |
| 116 | result.AppID, result.Slug, result.AccountID, result.Login = app.ID, app.Slug, app.Owner.ID, app.Owner.Login |
| 117 | result.SealedKey, result.State = sealed, StatePending |
| 118 | result.PendingUntil = s.now().Add(24 * hour) |
| 119 | result.InstallationHash = digest(s.state(result, "installation")) |
| 120 | return s.save(tx, p, &result, "forge.convert") |
| 121 | }) |
| 122 | if err != nil { |
| 123 | return View{}, err |
| 124 | } |
| 125 | if callErr != nil { |
| 126 | return s.view(result), &Refusal{s.view(result), callErr} |
| 127 | } |
| 128 | return s.view(result), nil |
| 129 | } |
| 130 | func (s *Store) Install(ctx context.Context, p Principal, state string) (View, error) { |
| 131 | c, err := s.claimCallback(ctx, p, state, "installation") |
| 132 | if err != nil { |
| 133 | return s.view(c), err |
| 134 | } |
| 135 | return s.Check(ctx, p, c.ID) |
| 136 | } |
| 137 | |
| 138 | // InstallationURL is used only after the first callback's clean-URL redirect. |
| 139 | // The raw state is derived in memory; only its digest is stored. |
| 140 | func (s *Store) InstallationURL(ctx context.Context, p Principal, id string) (string, error) { |
| 141 | if !s.Enabled { |
| 142 | return "", ErrAccess |
| 143 | } |
| 144 | tx := s.primary(ctx) |
| 145 | if err := s.Owner(tx, p.Address); err != nil { |
| 146 | return "", err |
| 147 | } |
| 148 | var c Connection |
| 149 | if err := tx.First(&c, "id = ?", id).Error; err != nil { |
| 150 | return "", err |
| 151 | } |
| 152 | if c.Owner != p.Address || c.Session != digest(p.Session) || c.Origin != s.Origin || c.InstallationHash == "" || c.SealedKey == "" || !s.now().Before(c.RedirectUntil) { |
| 153 | return "", ErrAccess |
| 154 | } |
| 155 | return "https://github.com/apps/" + url.PathEscape(c.Slug) + "/installations/new?state=" + url.QueryEscape(s.state(c, "installation")), nil |
| 156 | } |
| 157 | |