aboutsummaryrefslogtreecommitdiff
path: root/internal/db/two_factor.go
diff options
context:
space:
mode:
authorᴜɴᴋɴᴡᴏɴ <u@gogs.io>2020-04-04 21:14:15 +0800
committerGitHub <noreply@github.com>2020-04-04 21:14:15 +0800
commit34145c990d4fd9f278f29cdf9c61378a75e9b934 (patch)
tree7b151bbd5aef9e487759953e3a775a82244d268d /internal/db/two_factor.go
parent2bd9d0b9c8238ded727cd98a3ace20b53c10a44f (diff)
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
Diffstat (limited to 'internal/db/two_factor.go')
-rw-r--r--internal/db/two_factor.go25
1 files changed, 14 insertions, 11 deletions
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