| 1 | package github |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/rand" |
| 6 | "errors" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "gorm.io/gorm" |
| 11 | ) |
| 12 | |
| 13 | const hour = time.Hour |
| 14 | |
| 15 | // conversionBound covers the conversion and verification requests and their |
| 16 | // commit; a converting row older than this was lost. |
| 17 | const conversionBound = 2 * time.Minute |
| 18 | |
| 19 | func equalLogin(a, b string) bool { return strings.EqualFold(a, b) } |
| 20 | func (s *Store) expired(c Connection) State { |
| 21 | if !c.Confirmed && !c.PendingUntil.IsZero() && !s.now().Before(c.PendingUntil) { |
| 22 | return StateExpired |
| 23 | } |
| 24 | if c.Confirmed && !c.UninstalledAt.IsZero() && !s.now().Before(c.UninstalledAt.Add(7*24*time.Hour)) { |
| 25 | return StateUninstalledExpired |
| 26 | } |
| 27 | return "" |
| 28 | } |
| 29 | func (s *Store) Check(ctx context.Context, p Principal, id string) (View, error) { |
| 30 | var claimed Connection |
| 31 | var refused error |
| 32 | err := s.write(ctx, p, func(tx *gorm.DB) error { |
| 33 | if err := tx.First(&claimed, "id = ?", id).Error; err != nil { |
| 34 | return err |
| 35 | } |
| 36 | if state := s.expired(claimed); state != "" && claimed.SealedKey != "" { |
| 37 | erase(&claimed, state) |
| 38 | if err := s.save(tx, p, &claimed, "forge.expire"); err != nil { |
| 39 | return err |
| 40 | } |
| 41 | if err := tx.Where("connection_id = ?", id).Delete(&Pending{}).Error; err != nil { |
| 42 | return err |
| 43 | } |
| 44 | refused = &Refusal{s.view(claimed), ErrAccess} |
| 45 | return nil |
| 46 | } |
| 47 | if claimed.SealedKey == "" { |
| 48 | return ErrAccess |
| 49 | } |
| 50 | if claimed.Claim != "" && s.now().Before(claimed.ClaimUntil) { |
| 51 | return ErrBusy |
| 52 | } |
| 53 | claimed.Claim = strings.ToLower(rand.Text()) |
| 54 | claimed.ClaimUntil = s.now().Add(30 * time.Second) |
| 55 | return s.save(tx, p, &claimed, "forge.check_claim") |
| 56 | }) |
| 57 | if err != nil { |
| 58 | return View{}, err |
| 59 | } |
| 60 | if refused != nil { |
| 61 | s.API.Forget(ctx, claimed.AppID) |
| 62 | return s.view(claimed), refused |
| 63 | } |
| 64 | app, callErr := s.app(claimed) |
| 65 | var installation Installation |
| 66 | var inventory Inventory |
| 67 | found, seen := false, false |
| 68 | if callErr == nil { |
| 69 | app, callErr = s.API.Verify(ctx, app) |
| 70 | } |
| 71 | if callErr == nil { |
| 72 | var rows []Installation |
| 73 | rows, callErr = s.API.Installations(ctx, app) |
| 74 | if callErr == nil { |
| 75 | for _, row := range rows { |
| 76 | if row.AppID == app.ID && row.Account.ID == app.Owner.ID { |
| 77 | if found { |
| 78 | callErr = ErrAccess |
| 79 | break |
| 80 | } |
| 81 | installation = row |
| 82 | found = true |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | if callErr == nil && found { |
| 88 | installation, callErr = s.API.Installation(ctx, app, installation.ID) |
| 89 | seen = callErr == nil |
| 90 | } |
| 91 | if callErr == nil && found && installation.Suspended == nil { |
| 92 | inventory, callErr = s.API.Inventory(ctx, app, installation) |
| 93 | } |
| 94 | var result Connection |
| 95 | err = s.write(ctx, p, func(tx *gorm.DB) error { |
| 96 | if err := tx.First(&result, "id = ?", id).Error; err != nil { |
| 97 | return err |
| 98 | } |
| 99 | if result.Generation != claimed.Generation || result.Claim != claimed.Claim || !s.now().Before(result.ClaimUntil) { |
| 100 | return ErrBusy |
| 101 | } |
| 102 | if app.ID == result.AppID && app.Slug != "" { |
| 103 | result.Slug = app.Slug |
| 104 | } |
| 105 | result.Claim = "" |
| 106 | result.ClaimUntil = time.Time{} |
| 107 | expired := s.expired(result) |
| 108 | if expired == "" && seen { |
| 109 | // The JWT sees this installation again, in any state. |
| 110 | result.UninstalledAt = time.Time{} |
| 111 | } |
| 112 | if expired != "" { |
| 113 | erase(&result, expired) |
| 114 | refused = &Refusal{s.view(result), ErrAccess} |
| 115 | } else if errors.Is(callErr, ErrReconnect) { |
| 116 | erase(&result, StateReconnect) |
| 117 | refused = &Refusal{s.view(result), ErrReconnect} |
| 118 | } else if errors.Is(callErr, ErrTooMany) { |
| 119 | result.State = StateTooMany |
| 120 | result.CountKnown = false |
| 121 | result.InstallationID = installation.ID |
| 122 | result.Selection = installation.Selection |
| 123 | } else if callErr != nil { |
| 124 | result.State = StateCheckFailed |
| 125 | result.CountKnown = false |
| 126 | refused = &Refusal{s.view(result), callErr} |
| 127 | } else if !found { |
| 128 | result.CountKnown = false |
| 129 | if result.Confirmed { |
| 130 | result.State = StateUninstalled |
| 131 | if result.UninstalledAt.IsZero() { |
| 132 | result.UninstalledAt = s.now() |
| 133 | } |
| 134 | } else { |
| 135 | result.State = StatePending |
| 136 | } |
| 137 | } else if installation.Suspended != nil { |
| 138 | result.State = StateSuspended |
| 139 | result.CountKnown = false |
| 140 | result.InstallationID = installation.ID |
| 141 | } else { |
| 142 | result.InstallationID = installation.ID |
| 143 | result.Selection = installation.Selection |
| 144 | result.Count = inventory.Count |
| 145 | result.CountKnown = true |
| 146 | result.UninstalledAt = time.Time{} |
| 147 | result.Login = installation.Account.Login |
| 148 | result.State = StateReady |
| 149 | if result.Confirmed { |
| 150 | result.State = StateLive |
| 151 | } |
| 152 | } |
| 153 | return s.save(tx, p, &result, "forge.check") |
| 154 | }) |
| 155 | if err != nil || result.SealedKey == "" || result.State == StateUninstalled || result.State == StateSuspended { |
| 156 | s.API.Forget(ctx, claimed.AppID) |
| 157 | } |
| 158 | if err != nil { |
| 159 | return View{}, err |
| 160 | } |
| 161 | return s.view(result), refused |
| 162 | } |
| 163 | func (s *Store) Connect(ctx context.Context, p Principal, id string) (View, error) { |
| 164 | checked, err := s.Check(ctx, p, id) |
| 165 | if err != nil { |
| 166 | return checked, err |
| 167 | } |
| 168 | var c Connection |
| 169 | err = s.write(ctx, p, func(tx *gorm.DB) error { |
| 170 | if err := tx.First(&c, "id = ?", id).Error; err != nil { |
| 171 | return err |
| 172 | } |
| 173 | if c.Generation != checked.Generation { |
| 174 | return ErrBusy |
| 175 | } |
| 176 | if c.Confirmed && c.State == StateLive { |
| 177 | return nil |
| 178 | } |
| 179 | if c.State != StateReady || !c.CountKnown || c.Count > MaxRepositories || c.SealedKey == "" || s.expired(c) != "" { |
| 180 | return &Refusal{s.view(c), ErrAccess} |
| 181 | } |
| 182 | c.Confirmed = true |
| 183 | c.State = StateLive |
| 184 | c.ActiveAccount = &c.AccountID |
| 185 | if err := s.save(tx, p, &c, "forge.connect"); err != nil { |
| 186 | return err |
| 187 | } |
| 188 | return tx.Where("connection_id = ?", id).Delete(&Pending{}).Error |
| 189 | }) |
| 190 | result := s.view(c) |
| 191 | if err == nil && checked.Confirmed { |
| 192 | result.State = StateAlreadyConnected |
| 193 | } |
| 194 | return result, err |
| 195 | } |
| 196 | |