2017-01-16 10:14:29 +08:00
|
|
|
// Copyright 2017 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 models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
2018-07-27 20:54:50 +08:00
|
|
|
"crypto/sha256"
|
2017-01-16 10:14:29 +08:00
|
|
|
"crypto/subtle"
|
|
|
|
"encoding/base64"
|
2018-07-27 20:54:50 +08:00
|
|
|
"fmt"
|
2017-01-16 10:14:29 +08:00
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2020-10-05 13:49:33 +08:00
|
|
|
"code.gitea.io/gitea/modules/secret"
|
2017-01-16 10:14:29 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-08-15 22:46:21 +08:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2021-05-10 14:45:17 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-08-15 22:46:21 +08:00
|
|
|
|
|
|
|
"github.com/pquerna/otp/totp"
|
|
|
|
"golang.org/x/crypto/pbkdf2"
|
2017-01-16 10:14:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// TwoFactor represents a two-factor authentication token.
|
|
|
|
type TwoFactor struct {
|
2018-05-02 23:02:02 +08:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
UID int64 `xorm:"UNIQUE"`
|
|
|
|
Secret string
|
2018-07-27 20:54:50 +08:00
|
|
|
ScratchSalt string
|
|
|
|
ScratchHash string
|
2019-08-15 22:46:21 +08:00
|
|
|
LastUsedPasscode string `xorm:"VARCHAR(10)"`
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
|
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
2017-01-16 10:14:29 +08:00
|
|
|
}
|
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(TwoFactor))
|
|
|
|
}
|
|
|
|
|
2017-01-16 10:14:29 +08:00
|
|
|
// GenerateScratchToken recreates the scratch token the user is using.
|
2018-07-27 20:54:50 +08:00
|
|
|
func (t *TwoFactor) GenerateScratchToken() (string, error) {
|
2021-05-10 14:45:17 +08:00
|
|
|
token, err := util.RandomString(8)
|
2017-01-16 10:14:29 +08:00
|
|
|
if err != nil {
|
2018-07-27 20:54:50 +08:00
|
|
|
return "", err
|
2017-01-16 10:14:29 +08:00
|
|
|
}
|
2021-05-10 14:45:17 +08:00
|
|
|
t.ScratchSalt, _ = util.RandomString(10)
|
2018-07-27 20:54:50 +08:00
|
|
|
t.ScratchHash = hashToken(token, t.ScratchSalt)
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashToken(token, salt string) string {
|
|
|
|
tempHash := pbkdf2.Key([]byte(token), []byte(salt), 10000, 50, sha256.New)
|
|
|
|
return fmt.Sprintf("%x", tempHash)
|
2017-01-16 10:14:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyScratchToken verifies if the specified scratch token is valid.
|
|
|
|
func (t *TwoFactor) VerifyScratchToken(token string) bool {
|
|
|
|
if len(token) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2018-07-27 20:54:50 +08:00
|
|
|
tempHash := hashToken(token, t.ScratchSalt)
|
|
|
|
return subtle.ConstantTimeCompare([]byte(t.ScratchHash), []byte(tempHash)) == 1
|
2017-01-16 10:14:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TwoFactor) getEncryptionKey() []byte {
|
|
|
|
k := md5.Sum([]byte(setting.SecretKey))
|
|
|
|
return k[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSecret sets the 2FA secret.
|
2020-10-05 13:49:33 +08:00
|
|
|
func (t *TwoFactor) SetSecret(secretString string) error {
|
|
|
|
secretBytes, err := secret.AesEncrypt(t.getEncryptionKey(), []byte(secretString))
|
2017-01-16 10:14:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.Secret = base64.StdEncoding.EncodeToString(secretBytes)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateTOTP validates the provided passcode.
|
|
|
|
func (t *TwoFactor) ValidateTOTP(passcode string) (bool, error) {
|
|
|
|
decodedStoredSecret, err := base64.StdEncoding.DecodeString(t.Secret)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-10-05 13:49:33 +08:00
|
|
|
secretBytes, err := secret.AesDecrypt(t.getEncryptionKey(), decodedStoredSecret)
|
2017-01-16 10:14:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-10-05 13:49:33 +08:00
|
|
|
secretStr := string(secretBytes)
|
2017-01-16 10:14:29 +08:00
|
|
|
return totp.Validate(passcode, secretStr), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTwoFactor creates a new two-factor authentication token.
|
|
|
|
func NewTwoFactor(t *TwoFactor) error {
|
2021-09-23 23:45:36 +08:00
|
|
|
_, err := db.GetEngine(db.DefaultContext).Insert(t)
|
2017-01-16 10:14:29 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTwoFactor updates a two-factor authentication token.
|
|
|
|
func UpdateTwoFactor(t *TwoFactor) error {
|
2021-09-23 23:45:36 +08:00
|
|
|
_, err := db.GetEngine(db.DefaultContext).ID(t.ID).AllCols().Update(t)
|
2017-01-16 10:14:29 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTwoFactorByUID returns the two-factor authentication token associated with
|
|
|
|
// the user, if any.
|
|
|
|
func GetTwoFactorByUID(uid int64) (*TwoFactor, error) {
|
2020-06-18 01:50:11 +08:00
|
|
|
twofa := &TwoFactor{}
|
2021-09-23 23:45:36 +08:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).Where("uid=?", uid).Get(twofa)
|
2017-01-16 10:14:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrTwoFactorNotEnrolled{uid}
|
|
|
|
}
|
|
|
|
return twofa, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTwoFactorByID deletes two-factor authentication token by given ID.
|
|
|
|
func DeleteTwoFactorByID(id, userID int64) error {
|
2021-09-23 23:45:36 +08:00
|
|
|
cnt, err := db.GetEngine(db.DefaultContext).ID(id).Delete(&TwoFactor{
|
2017-01-16 10:14:29 +08:00
|
|
|
UID: userID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if cnt != 1 {
|
|
|
|
return ErrTwoFactorNotEnrolled{userID}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|