2018-07-27 20:54:50 +08:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-07-27 20:54:50 +08:00
|
|
|
|
2022-12-08 16:21:37 +08:00
|
|
|
package v1_6 //nolint
|
2018-07-27 20:54:50 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-11-02 16:54:36 +08:00
|
|
|
"code.gitea.io/gitea/models/migrations/base"
|
2019-08-15 22:46:21 +08:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2021-05-10 14:45:17 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-08-15 22:46:21 +08:00
|
|
|
|
2019-10-17 17:26:49 +08:00
|
|
|
"xorm.io/xorm"
|
2018-07-27 20:54:50 +08:00
|
|
|
)
|
|
|
|
|
2022-11-02 16:54:36 +08:00
|
|
|
func AddScratchHash(x *xorm.Engine) error {
|
2018-07-27 20:54:50 +08: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 22:46:21 +08:00
|
|
|
LastUsedPasscode string `xorm:"VARCHAR(10)"`
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
|
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
2018-07-27 20:54:50 +08:00
|
|
|
}
|
|
|
|
|
2023-08-14 03:17:21 +08:00
|
|
|
if err := x.Sync(new(TwoFactor)); err != nil {
|
|
|
|
return fmt.Errorf("Sync: %w", err)
|
2018-07-27 20:54:50 +08: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 21:23:03 +08:00
|
|
|
if err := sess.Limit(batchSize, start).Find(&tfas); err != nil {
|
2018-07-27 20:54:50 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(tfas) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tfa := range tfas {
|
|
|
|
// generate salt
|
2022-01-26 12:10:10 +08:00
|
|
|
salt, err := util.CryptoRandomString(10)
|
2018-07-27 20:54:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tfa.ScratchSalt = salt
|
2022-11-02 16:54:36 +08:00
|
|
|
tfa.ScratchHash = base.HashToken(tfa.ScratchToken, salt)
|
2018-07-27 20:54:50 +08:00
|
|
|
|
|
|
|
if _, err := sess.ID(tfa.ID).Cols("scratch_salt, scratch_hash").Update(tfa); err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("couldn't add in scratch_hash and scratch_salt: %w", err)
|
2018-07-27 20:54:50 +08: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 16:54:36 +08:00
|
|
|
if err := base.DropTableColumns(sess, "two_factor", "scratch_token"); err != nil {
|
2018-07-27 20:54:50 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return sess.Commit()
|
|
|
|
}
|