You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
4.3 KiB
157 lines
4.3 KiB
8 years ago
|
// 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.
|
||
|
|
||
3 years ago
|
package auth
|
||
8 years ago
|
|
||
|
import (
|
||
|
"crypto/md5"
|
||
6 years ago
|
"crypto/sha256"
|
||
8 years ago
|
"crypto/subtle"
|
||
|
"encoding/base64"
|
||
6 years ago
|
"fmt"
|
||
8 years ago
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/secret"
|
||
8 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/timeutil"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/util"
|
||
5 years ago
|
|
||
|
"github.com/pquerna/otp/totp"
|
||
|
"golang.org/x/crypto/pbkdf2"
|
||
8 years ago
|
)
|
||
|
|
||
3 years ago
|
//
|
||
|
// Two-factor authentication
|
||
|
//
|
||
|
|
||
|
// ErrTwoFactorNotEnrolled indicates that a user is not enrolled in two-factor authentication.
|
||
|
type ErrTwoFactorNotEnrolled struct {
|
||
|
UID int64
|
||
|
}
|
||
|
|
||
|
// IsErrTwoFactorNotEnrolled checks if an error is a ErrTwoFactorNotEnrolled.
|
||
|
func IsErrTwoFactorNotEnrolled(err error) bool {
|
||
|
_, ok := err.(ErrTwoFactorNotEnrolled)
|
||
|
return ok
|
||
|
}
|
||
|
|
||
|
func (err ErrTwoFactorNotEnrolled) Error() string {
|
||
|
return fmt.Sprintf("user not enrolled in 2FA [uid: %d]", err.UID)
|
||
|
}
|
||
|
|
||
8 years ago
|
// TwoFactor represents a two-factor authentication token.
|
||
|
type TwoFactor struct {
|
||
7 years ago
|
ID int64 `xorm:"pk autoincr"`
|
||
|
UID int64 `xorm:"UNIQUE"`
|
||
|
Secret string
|
||
6 years ago
|
ScratchSalt string
|
||
|
ScratchHash string
|
||
5 years ago
|
LastUsedPasscode string `xorm:"VARCHAR(10)"`
|
||
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||
|
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
func init() {
|
||
|
db.RegisterModel(new(TwoFactor))
|
||
|
}
|
||
|
|
||
8 years ago
|
// GenerateScratchToken recreates the scratch token the user is using.
|
||
6 years ago
|
func (t *TwoFactor) GenerateScratchToken() (string, error) {
|
||
4 years ago
|
token, err := util.RandomString(8)
|
||
8 years ago
|
if err != nil {
|
||
6 years ago
|
return "", err
|
||
8 years ago
|
}
|
||
4 years ago
|
t.ScratchSalt, _ = util.RandomString(10)
|
||
3 years ago
|
t.ScratchHash = HashToken(token, t.ScratchSalt)
|
||
6 years ago
|
return token, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
// HashToken return the hashable salt
|
||
|
func HashToken(token, salt string) string {
|
||
6 years ago
|
tempHash := pbkdf2.Key([]byte(token), []byte(salt), 10000, 50, sha256.New)
|
||
|
return fmt.Sprintf("%x", tempHash)
|
||
8 years ago
|
}
|
||
|
|
||
|
// VerifyScratchToken verifies if the specified scratch token is valid.
|
||
|
func (t *TwoFactor) VerifyScratchToken(token string) bool {
|
||
|
if len(token) == 0 {
|
||
|
return false
|
||
|
}
|
||
3 years ago
|
tempHash := HashToken(token, t.ScratchSalt)
|
||
6 years ago
|
return subtle.ConstantTimeCompare([]byte(t.ScratchHash), []byte(tempHash)) == 1
|
||
8 years ago
|
}
|
||
|
|
||
|
func (t *TwoFactor) getEncryptionKey() []byte {
|
||
|
k := md5.Sum([]byte(setting.SecretKey))
|
||
|
return k[:]
|
||
|
}
|
||
|
|
||
|
// SetSecret sets the 2FA secret.
|
||
4 years ago
|
func (t *TwoFactor) SetSecret(secretString string) error {
|
||
|
secretBytes, err := secret.AesEncrypt(t.getEncryptionKey(), []byte(secretString))
|
||
8 years ago
|
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
|
||
|
}
|
||
4 years ago
|
secretBytes, err := secret.AesDecrypt(t.getEncryptionKey(), decodedStoredSecret)
|
||
8 years ago
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
4 years ago
|
secretStr := string(secretBytes)
|
||
8 years ago
|
return totp.Validate(passcode, secretStr), nil
|
||
|
}
|
||
|
|
||
|
// NewTwoFactor creates a new two-factor authentication token.
|
||
|
func NewTwoFactor(t *TwoFactor) error {
|
||
3 years ago
|
_, err := db.GetEngine(db.DefaultContext).Insert(t)
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
|
||
|
// UpdateTwoFactor updates a two-factor authentication token.
|
||
|
func UpdateTwoFactor(t *TwoFactor) error {
|
||
3 years ago
|
_, err := db.GetEngine(db.DefaultContext).ID(t.ID).AllCols().Update(t)
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
|
||
|
// GetTwoFactorByUID returns the two-factor authentication token associated with
|
||
|
// the user, if any.
|
||
|
func GetTwoFactorByUID(uid int64) (*TwoFactor, error) {
|
||
5 years ago
|
twofa := &TwoFactor{}
|
||
3 years ago
|
has, err := db.GetEngine(db.DefaultContext).Where("uid=?", uid).Get(twofa)
|
||
8 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
} else if !has {
|
||
|
return nil, ErrTwoFactorNotEnrolled{uid}
|
||
|
}
|
||
|
return twofa, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
// HasTwoFactorByUID returns the two-factor authentication token associated with
|
||
|
// the user, if any.
|
||
|
func HasTwoFactorByUID(uid int64) (bool, error) {
|
||
|
return db.GetEngine(db.DefaultContext).Where("uid=?", uid).Exist(&TwoFactor{})
|
||
|
}
|
||
|
|
||
8 years ago
|
// DeleteTwoFactorByID deletes two-factor authentication token by given ID.
|
||
|
func DeleteTwoFactorByID(id, userID int64) error {
|
||
3 years ago
|
cnt, err := db.GetEngine(db.DefaultContext).ID(id).Delete(&TwoFactor{
|
||
8 years ago
|
UID: userID,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
} else if cnt != 1 {
|
||
|
return ErrTwoFactorNotEnrolled{userID}
|
||
|
}
|
||
|
return nil
|
||
|
}
|