mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-12-08 06:29:47 +00:00
- Similair spirit of forgejo/forgejo!7453. - Refactor the code in such a way that it always succeeds. - To avoid doing mathematics if you use this function, define three security level (64, 128 and 256 bits) that correspond to a specific length which has that a security guarantee. I picked them as they fit the need for the existing usages of the code. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10110 Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Reviewed-by: Lucas <sclu1034@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package v1_6
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"forgejo.org/models/gitea_migrations/base"
|
|
"forgejo.org/modules/timeutil"
|
|
"forgejo.org/modules/util"
|
|
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
func AddScratchHash(x *xorm.Engine) error {
|
|
// 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
|
|
LastUsedPasscode string `xorm:"VARCHAR(10)"`
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
|
}
|
|
|
|
if err := x.Sync(new(TwoFactor)); err != nil {
|
|
return fmt.Errorf("Sync: %w", err)
|
|
}
|
|
|
|
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)
|
|
if err := sess.Limit(batchSize, start).Find(&tfas); err != nil {
|
|
return err
|
|
}
|
|
if len(tfas) == 0 {
|
|
break
|
|
}
|
|
|
|
for _, tfa := range tfas {
|
|
// generate salt
|
|
salt := util.CryptoRandomString(util.RandomStringMedium)
|
|
tfa.ScratchSalt = salt
|
|
tfa.ScratchHash = base.HashToken(tfa.ScratchToken, salt)
|
|
|
|
if _, err := sess.ID(tfa.ID).Cols("scratch_salt, scratch_hash").Update(tfa); err != nil {
|
|
return fmt.Errorf("couldn't add in scratch_hash and scratch_salt: %w", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Commit and begin new transaction for dropping columns
|
|
if err := sess.Commit(); err != nil {
|
|
return err
|
|
}
|
|
if err := sess.Begin(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := base.DropTableColumns(sess, "two_factor", "scratch_token"); err != nil {
|
|
return err
|
|
}
|
|
return sess.Commit()
|
|
}
|