mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
1.9 KiB
82 lines
1.9 KiB
6 years ago
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
2 years ago
|
package v1_6 // nolint
|
||
6 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
2 years ago
|
"code.gitea.io/gitea/models/migrations/base"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/timeutil"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/util"
|
||
5 years ago
|
|
||
5 years ago
|
"xorm.io/xorm"
|
||
6 years ago
|
)
|
||
|
|
||
2 years ago
|
func AddScratchHash(x *xorm.Engine) error {
|
||
6 years ago
|
// 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
|
||
5 years ago
|
LastUsedPasscode string `xorm:"VARCHAR(10)"`
|
||
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||
|
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||
6 years ago
|
}
|
||
|
|
||
|
if err := x.Sync2(new(TwoFactor)); err != nil {
|
||
2 years ago
|
return fmt.Errorf("Sync2: %w", err)
|
||
6 years ago
|
}
|
||
|
|
||
|
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)
|
||
6 years ago
|
if err := sess.Limit(batchSize, start).Find(&tfas); err != nil {
|
||
6 years ago
|
return err
|
||
|
}
|
||
|
if len(tfas) == 0 {
|
||
|
break
|
||
|
}
|
||
|
|
||
|
for _, tfa := range tfas {
|
||
|
// generate salt
|
||
3 years ago
|
salt, err := util.CryptoRandomString(10)
|
||
6 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
tfa.ScratchSalt = salt
|
||
2 years ago
|
tfa.ScratchHash = base.HashToken(tfa.ScratchToken, salt)
|
||
6 years ago
|
|
||
|
if _, err := sess.ID(tfa.ID).Cols("scratch_salt, scratch_hash").Update(tfa); err != nil {
|
||
2 years ago
|
return fmt.Errorf("couldn't add in scratch_hash and scratch_salt: %w", err)
|
||
6 years ago
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Commit and begin new transaction for dropping columns
|
||
|
if err := sess.Commit(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err := sess.Begin(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
2 years ago
|
if err := base.DropTableColumns(sess, "two_factor", "scratch_token"); err != nil {
|
||
6 years ago
|
return err
|
||
|
}
|
||
|
return sess.Commit()
|
||
|
}
|