From 34145c990d4fd9f278f29cdf9c61378a75e9b934 Mon Sep 17 00:00:00 2001 From: ᴜɴᴋɴᴡᴏɴ Date: Sat, 4 Apr 2020 21:14:15 +0800 Subject: lfs: implement HTTP routes (#6035) * Bootstrap with GORM * Fix lint error * Set conn max lifetime to one minute * Fallback to use gorm v1 * Define HTTP routes * Finish authentication * Save token updated * Add docstring * Finish authorization * serveBatch rundown * Define types in lfsutil * Finish Batch * authutil * Finish basic * Formalize response error * Fix lint errors * authutil: add tests * dbutil: add tests * lfsutil: add tests * strutil: add tests * Formalize 401 response --- internal/db/two_factor.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'internal/db/two_factor.go') diff --git a/internal/db/two_factor.go b/internal/db/two_factor.go index a46fb992..e827d59b 100644 --- a/internal/db/two_factor.go +++ b/internal/db/two_factor.go @@ -12,7 +12,6 @@ import ( "github.com/pquerna/otp/totp" "github.com/unknwon/com" - log "unknwon.dev/clog/v2" "xorm.io/xorm" "gogs.io/gogs/internal/conf" @@ -54,15 +53,6 @@ func (t *TwoFactor) ValidateTOTP(passcode string) (bool, error) { return totp.Validate(passcode, string(decryptSecret)), nil } -// IsUserEnabledTwoFactor returns true if user has enabled two-factor authentication. -func IsUserEnabledTwoFactor(userID int64) bool { - has, err := x.Where("user_id = ?", userID).Get(new(TwoFactor)) - if err != nil { - log.Error("IsUserEnabledTwoFactor [user_id: %d]: %v", userID, err) - } - return has -} - func generateRecoveryCodes(userID int64) ([]*TwoFactorRecoveryCode, error) { recoveryCodes := make([]*TwoFactorRecoveryCode, 10) for i := 0; i < 10; i++ { @@ -182,6 +172,19 @@ func RegenerateRecoveryCodes(userID int64) error { return sess.Commit() } +type ErrTwoFactorRecoveryCodeNotFound struct { + Code string +} + +func IsTwoFactorRecoveryCodeNotFound(err error) bool { + _, ok := err.(ErrTwoFactorRecoveryCodeNotFound) + return ok +} + +func (err ErrTwoFactorRecoveryCodeNotFound) Error() string { + return fmt.Sprintf("two-factor recovery code does not found [code: %s]", err.Code) +} + // UseRecoveryCode validates recovery code of given user and marks it is used if valid. func UseRecoveryCode(userID int64, code string) error { recoveryCode := new(TwoFactorRecoveryCode) @@ -189,7 +192,7 @@ func UseRecoveryCode(userID int64, code string) error { if err != nil { return fmt.Errorf("get unused code: %v", err) } else if !has { - return errors.TwoFactorRecoveryCodeNotFound{Code: code} + return ErrTwoFactorRecoveryCodeNotFound{Code: code} } recoveryCode.IsUsed = true -- cgit v1.2.3