mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-07-25 02:48:05 +00:00
Upgrade Forgejo to our forked [xorm v1.4.0](https://code.forgejo.org/xorm/xorm/compare/v1.3.9-forgejo.12...v1.4.0), which is now named `code.forgejo.org/xorm/xorm` to reflect the current expectation that it is a permanent fork. A small number of API changes were made recently in https://code.forgejo.org/xorm/xorm/issues/120 which are accounted for in this PR, in addition to the module rename. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12639 Reviewed-by: Otto <otto@codeberg.org>
115 lines
3 KiB
Go
115 lines
3 KiB
Go
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package v1_9
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"forgejo.org/models/gitea_migrations/base"
|
|
"forgejo.org/modules/log"
|
|
"forgejo.org/modules/timeutil"
|
|
"forgejo.org/modules/util"
|
|
|
|
"code.forgejo.org/xorm/xorm"
|
|
)
|
|
|
|
func HashAppToken(x *xorm.Engine) error {
|
|
// AccessToken see models/token.go
|
|
type AccessToken struct {
|
|
ID int64 `xorm:"pk autoincr"`
|
|
UID int64 `xorm:"INDEX"`
|
|
Name string
|
|
Sha1 string
|
|
Token string `xorm:"-"`
|
|
TokenHash string // sha256 of token - we will ensure UNIQUE later
|
|
TokenSalt string
|
|
TokenLastEight string `xorm:"token_last_eight"`
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
|
HasRecentActivity bool `xorm:"-"`
|
|
HasUsed bool `xorm:"-"`
|
|
}
|
|
|
|
// First remove the index
|
|
sess := x.NewSession()
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := sess.Sync(new(AccessToken)); err != nil {
|
|
return fmt.Errorf("Sync: %w", err)
|
|
}
|
|
|
|
if err := sess.Commit(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// transform all tokens to hashes
|
|
const batchSize = 100
|
|
for start := 0; ; start += batchSize {
|
|
tokens := make([]*AccessToken, 0, batchSize)
|
|
if err := sess.Limit(batchSize, start).Find(&tokens); err != nil {
|
|
return err
|
|
}
|
|
if len(tokens) == 0 {
|
|
break
|
|
}
|
|
|
|
for _, token := range tokens {
|
|
// generate salt
|
|
salt := util.CryptoRandomString(util.RandomStringMedium)
|
|
token.TokenSalt = salt
|
|
token.TokenHash = base.HashToken(token.Sha1, salt)
|
|
if len(token.Sha1) < 8 {
|
|
log.Warn("Unable to transform token %s with name %s belonging to user ID %d, skipping transformation", token.Sha1, token.Name, token.UID)
|
|
continue
|
|
}
|
|
token.TokenLastEight = token.Sha1[len(token.Sha1)-8:]
|
|
token.Sha1 = "" // ensure to blank out column in case drop column doesn't work
|
|
|
|
if _, err := sess.ID(token.ID).Cols("token_hash, token_salt, token_last_eight, sha1").Update(token); err != nil {
|
|
return fmt.Errorf("couldn't add in sha1, token_hash, token_salt and token_last_eight: %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, "access_token", "sha1"); err != nil {
|
|
return err
|
|
}
|
|
if err := sess.Commit(); err != nil {
|
|
return err
|
|
}
|
|
return resyncHashAppTokenWithUniqueHash(x)
|
|
}
|
|
|
|
func resyncHashAppTokenWithUniqueHash(x *xorm.Engine) error {
|
|
// AccessToken see models/token.go
|
|
type AccessToken struct {
|
|
TokenHash string `xorm:"UNIQUE"` // sha256 of token - we will ensure UNIQUE later
|
|
}
|
|
sess := x.NewSession()
|
|
defer sess.Close()
|
|
if err := sess.Begin(); err != nil {
|
|
return err
|
|
}
|
|
if err := sess.Sync(new(AccessToken)); err != nil {
|
|
return fmt.Errorf("Sync: %w", err)
|
|
}
|
|
return sess.Commit()
|
|
}
|