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