2018-07-27 06:54:50 -06:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-07-27 06:54:50 -06:00
|
|
|
|
2022-12-08 01:21:37 -07:00
|
|
|
package v1_6 //nolint
|
2018-07-27 06:54:50 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-11-02 02:54:36 -06:00
|
|
|
"code.gitea.io/gitea/models/migrations/base"
|
2019-08-15 08:46:21 -06:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2021-05-10 00:45:17 -06:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-08-15 08:46:21 -06:00
|
|
|
|
2019-10-17 03:26:49 -06:00
|
|
|
"xorm.io/xorm"
|
2018-07-27 06:54:50 -06:00
|
|
|
)
|
|
|
|
|
2022-11-02 02:54:36 -06:00
|
|
|
func AddScratchHash(x *xorm.Engine) error {
|
2018-07-27 06:54:50 -06:00
|
|
|
// TwoFactor see models/twofactor.go
|
|
|
|
type TwoFactor struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
UID int64 `xorm:"UNIQUE"`
|
|
|
|
Secret string
|
|
|
|
ScratchToken string
|
|
|
|
ScratchSalt string
|
|
|
|
ScratchHash string
|
2019-08-15 08:46:21 -06:00
|
|
|
LastUsedPasscode string `xorm:"VARCHAR(10)"`
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
|
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
2018-07-27 06:54:50 -06:00
|
|
|
}
|
|
|
|
|
2023-08-13 13:17:21 -06:00
|
|
|
if err := x.Sync(new(TwoFactor)); err != nil {
|
|
|
|
return fmt.Errorf("Sync: %w", err)
|
2018-07-27 06:54:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// transform all tokens to hashes
|
|
|
|
const batchSize = 100
|
|
|
|
for start := 0; ; start += batchSize {
|
|
|
|
tfas := make([]*TwoFactor, 0, batchSize)
|
2018-12-31 06:23:03 -07:00
|
|
|
if err := sess.Limit(batchSize, start).Find(&tfas); err != nil {
|
2018-07-27 06:54:50 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(tfas) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tfa := range tfas {
|
|
|
|
// generate salt
|
2022-01-25 21:10:10 -07:00
|
|
|
salt, err := util.CryptoRandomString(10)
|
2018-07-27 06:54:50 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tfa.ScratchSalt = salt
|
2022-11-02 02:54:36 -06:00
|
|
|
tfa.ScratchHash = base.HashToken(tfa.ScratchToken, salt)
|
2018-07-27 06:54:50 -06:00
|
|
|
|
|
|
|
if _, err := sess.ID(tfa.ID).Cols("scratch_salt, scratch_hash").Update(tfa); err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("couldn't add in scratch_hash and scratch_salt: %w", err)
|
2018-07-27 06:54:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit and begin new transaction for dropping columns
|
|
|
|
if err := sess.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-02 02:54:36 -06:00
|
|
|
if err := base.DropTableColumns(sess, "two_factor", "scratch_token"); err != nil {
|
2018-07-27 06:54:50 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return sess.Commit()
|
|
|
|
}
|