From 19862699cd4fcac672cf87310b192d3182357086 Mon Sep 17 00:00:00 2001 From: Maurizio Porrato Date: Tue, 5 Mar 2019 02:34:52 +0000 Subject: [PATCH] Override xorm type mapping for U2F counter (#6232) --- models/migrations/migrations.go | 2 ++ models/migrations/v81.go | 32 ++++++++++++++++++++++++++++++++ models/u2f.go | 2 +- models/u2f_test.go | 8 ++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 models/migrations/v81.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 652abd122a..1fde096fbe 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -215,6 +215,8 @@ var migrations = []Migration{ NewMigration("add can close issues via commit in any branch", addCanCloseIssuesViaCommitInAnyBranch), // v80 -> v81 NewMigration("add is locked to issues", addIsLockedToIssues), + // v81 -> v82 + NewMigration("update U2F counter type", changeU2FCounterType), } // Migrate database to current version diff --git a/models/migrations/v81.go b/models/migrations/v81.go new file mode 100644 index 0000000000..56bb8477e6 --- /dev/null +++ b/models/migrations/v81.go @@ -0,0 +1,32 @@ +// Copyright 2019 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 ( + "fmt" + + "github.com/go-xorm/xorm" +) + +func changeU2FCounterType(x *xorm.Engine) error { + var err error + + switch x.Dialect().DriverName() { + case "tidb": + fallthrough + case "mysql": + _, err = x.Exec("ALTER TABLE `u2f_registration` MODIFY `counter` BIGINT") + case "postgres": + _, err = x.Exec("ALTER TABLE `u2f_registration` ALTER COLUMN `counter` SET DATA TYPE bigint") + case "mssql": + _, err = x.Exec("ALTER TABLE `u2f_registration` ALTER COLUMN `counter` BIGINT") + } + + if err != nil { + return fmt.Errorf("Error changing u2f_registration counter column type: %v", err) + } + + return nil +} diff --git a/models/u2f.go b/models/u2f.go index a7b031d9e2..888c67d0c5 100644 --- a/models/u2f.go +++ b/models/u2f.go @@ -17,7 +17,7 @@ type U2FRegistration struct { Name string UserID int64 `xorm:"INDEX"` Raw []byte - Counter uint32 + Counter uint32 `xorm:"BIGINT"` CreatedUnix util.TimeStamp `xorm:"INDEX created"` UpdatedUnix util.TimeStamp `xorm:"INDEX updated"` } diff --git a/models/u2f_test.go b/models/u2f_test.go index 6d6cd495ca..0080c649cb 100644 --- a/models/u2f_test.go +++ b/models/u2f_test.go @@ -40,6 +40,14 @@ func TestU2FRegistration_UpdateCounter(t *testing.T) { AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 1}) } +func TestU2FRegistration_UpdateLargeCounter(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + reg := AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) + reg.Counter = 0xffffffff + assert.NoError(t, reg.UpdateCounter()) + AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 0xffffffff}) +} + func TestCreateRegistration(t *testing.T) { assert.NoError(t, PrepareTestDatabase()) user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)