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