summaryrefslogtreecommitdiffstats
path: root/services/auth
diff options
context:
space:
mode:
authorGiteabot <teabot@gitea.io>2023-11-07 02:07:22 +0800
committerGitHub <noreply@github.com>2023-11-06 18:07:22 +0000
commita8e505a44bcc0b15dfb18c57309e1616ad741633 (patch)
tree2fb3bee5c9f862280fda796a8361c97399407e18 /services/auth
parente47b31c691c64581b9f3644f6fc32338ef67f8b3 (diff)
downloadgitea-a8e505a44bcc0b15dfb18c57309e1616ad741633.tar.gz
gitea-a8e505a44bcc0b15dfb18c57309e1616ad741633.zip
Unify two factor check (#27915) (#27929)
Backport #27915 by @KN4CK3R Fixes #27819 We have support for two factor logins with the normal web login and with basic auth. For basic auth the two factor check was implemented at three different places and you need to know that this check is necessary. This PR moves the check into the basic auth itself. Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Diffstat (limited to 'services/auth')
-rw-r--r--services/auth/basic.go24
1 files changed, 22 insertions, 2 deletions
diff --git a/services/auth/basic.go b/services/auth/basic.go
index 6c3fbf595e..1184d12d1c 100644
--- a/services/auth/basic.go
+++ b/services/auth/basic.go
@@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
+ "code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web/middleware"
)
@@ -131,11 +132,30 @@ func (b *Basic) Verify(req *http.Request, w http.ResponseWriter, store DataStore
return nil, err
}
- if skipper, ok := source.Cfg.(LocalTwoFASkipper); ok && skipper.IsSkipLocalTwoFA() {
- store.GetData()["SkipLocalTwoFA"] = true
+ if skipper, ok := source.Cfg.(LocalTwoFASkipper); !ok || !skipper.IsSkipLocalTwoFA() {
+ if err := validateTOTP(req, u); err != nil {
+ return nil, err
+ }
}
log.Trace("Basic Authorization: Logged in user %-v", u)
return u, nil
}
+
+func validateTOTP(req *http.Request, u *user_model.User) error {
+ twofa, err := auth_model.GetTwoFactorByUID(req.Context(), u.ID)
+ if err != nil {
+ if auth_model.IsErrTwoFactorNotEnrolled(err) {
+ // No 2FA enrollment for this user
+ return nil
+ }
+ return err
+ }
+ if ok, err := twofa.ValidateTOTP(req.Header.Get("X-Gitea-OTP")); err != nil {
+ return err
+ } else if !ok {
+ return util.NewInvalidArgumentErrorf("invalid provided OTP")
+ }
+ return nil
+}