Skip to content

Commit a58db36

Browse files
committed
Do not allow to reuse TOTP passcode
1 parent fff022e commit a58db36

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ var migrations = []Migration{
176176
NewMigration("add is_fsck_enabled column for repos", addFsckEnabledToRepo),
177177
// v61 -> v62
178178
NewMigration("add size column for attachments", addSizeToAttachment),
179+
// v62 -> v63
180+
NewMigration("add last used passcode column for TOTP", addLastUsedPasscodeTOTP),
179181
}
180182

181183
// Migrate database to current version

models/migrations/v62.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/go-xorm/xorm"
11+
)
12+
13+
func addLastUsedPasscodeTOTP(x *xorm.Engine) error {
14+
type TwoFactor struct {
15+
LastUsedPasscode string `xorm:"VARCHAR(10)"`
16+
}
17+
18+
if err := x.Sync2(new(TwoFactor)); err != nil {
19+
return fmt.Errorf("Sync2: %v", err)
20+
}
21+
return nil
22+
}

models/twofactor.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ import (
2323

2424
// TwoFactor represents a two-factor authentication token.
2525
type TwoFactor struct {
26-
ID int64 `xorm:"pk autoincr"`
27-
UID int64 `xorm:"UNIQUE"`
28-
Secret string
29-
ScratchToken string
30-
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
31-
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
26+
ID int64 `xorm:"pk autoincr"`
27+
UID int64 `xorm:"UNIQUE"`
28+
Secret string
29+
ScratchToken string
30+
LastUsedPasscode string `xorm:"VARCHAR(10)"`
31+
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
32+
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
3233
}
3334

3435
// GenerateScratchToken recreates the scratch token the user is using.

routers/user/auth.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func TwoFactorPost(ctx *context.Context, form auth.TwoFactorAuthForm) {
221221
return
222222
}
223223

224-
if ok {
224+
if ok && twofa.LastUsedPasscode != form.Passcode {
225225
remember := ctx.Session.Get("twofaRemember").(bool)
226226
u, err := models.GetUserByID(id)
227227
if err != nil {
@@ -243,6 +243,12 @@ func TwoFactorPost(ctx *context.Context, form auth.TwoFactorAuthForm) {
243243
}
244244
}
245245

246+
twofa.LastUsedPasscode = form.Passcode
247+
if err = models.UpdateTwoFactor(twofa); err != nil {
248+
ctx.ServerError("UserSignIn", err)
249+
return
250+
}
251+
246252
handleSignIn(ctx, u, remember)
247253
return
248254
}

0 commit comments

Comments
 (0)