mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-31 11:52:10 +00:00
Support webauthn (#17957)
Migrate from U2F to Webauthn Co-authored-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
parent
8808293247
commit
35c3553870
224 changed files with 35040 additions and 1079 deletions
|
@ -366,6 +366,8 @@ var migrations = []Migration{
|
|||
NewMigration("Migrate to higher varchar on user struct", migrateUserPasswordSalt),
|
||||
// v206 -> v207
|
||||
NewMigration("Add authorize column to team_unit table", addAuthorizeColForTeamUnit),
|
||||
// v207 -> v208
|
||||
NewMigration("Add webauthn table and migrate u2f data to webauthn", addWebAuthnCred),
|
||||
}
|
||||
|
||||
// GetCurrentDBVersion returns the current db version
|
||||
|
|
91
models/migrations/v207.go
Normal file
91
models/migrations/v207.go
Normal file
|
@ -0,0 +1,91 @@
|
|||
// Copyright 2021 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.
|
||||
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"crypto/elliptic"
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/tstranex/u2f"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func addWebAuthnCred(x *xorm.Engine) error {
|
||||
|
||||
// Create webauthnCredential table
|
||||
type webauthnCredential struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
Name string
|
||||
LowerName string `xorm:"unique(s)"`
|
||||
UserID int64 `xorm:"INDEX unique(s)"`
|
||||
CredentialID string `xorm:"INDEX"`
|
||||
PublicKey []byte
|
||||
AttestationType string
|
||||
AAGUID []byte
|
||||
SignCount uint32 `xorm:"BIGINT"`
|
||||
CloneWarning bool
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||||
}
|
||||
if err := x.Sync2(&webauthnCredential{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Now migrate the old u2f registrations to the new format
|
||||
type u2fRegistration struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
Name string
|
||||
UserID int64 `xorm:"INDEX"`
|
||||
Raw []byte
|
||||
Counter uint32 `xorm:"BIGINT"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||||
}
|
||||
|
||||
var start int
|
||||
regs := make([]*u2fRegistration, 0, 50)
|
||||
for {
|
||||
err := x.OrderBy("id").Limit(50, start).Find(®s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, reg := range regs {
|
||||
parsed := new(u2f.Registration)
|
||||
err = parsed.UnmarshalBinary(reg.Raw)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
c := &webauthnCredential{
|
||||
ID: reg.ID,
|
||||
Name: reg.Name,
|
||||
LowerName: strings.ToLower(reg.Name),
|
||||
UserID: reg.UserID,
|
||||
CredentialID: base64.RawStdEncoding.EncodeToString(parsed.KeyHandle),
|
||||
PublicKey: elliptic.Marshal(elliptic.P256(), parsed.PubKey.X, parsed.PubKey.Y),
|
||||
AttestationType: "fido-u2f",
|
||||
AAGUID: []byte{},
|
||||
SignCount: reg.Counter,
|
||||
}
|
||||
|
||||
_, err := x.Insert(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(regs) < 50 {
|
||||
break
|
||||
}
|
||||
start += 50
|
||||
regs = regs[:0]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue