aboutsummaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2021-09-10 17:37:57 +0100
committerGitHub <noreply@github.com>2021-09-10 18:37:57 +0200
commit9ca0e7905c24f18ed246e65397589f0f41b50506 (patch)
tree5c0c03dc297f47e7f60711563efbaf87993463a6 /routers
parent51578d64188a7077848cb60d3ead8e818637ab59 (diff)
downloadgitea-9ca0e7905c24f18ed246e65397589f0f41b50506.tar.gz
gitea-9ca0e7905c24f18ed246e65397589f0f41b50506.zip
Add setting to OAuth handlers to skip local 2FA authentication (#16594)
This PR adds a setting to OAuth and OpenID login sources to allow the source to skip local 2FA authentication. Fix #13939 Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'routers')
-rw-r--r--routers/web/admin/auths.go1
-rw-r--r--routers/web/user/auth.go20
2 files changed, 13 insertions, 8 deletions
diff --git a/routers/web/admin/auths.go b/routers/web/admin/auths.go
index 342318e04e..b2879d7c4f 100644
--- a/routers/web/admin/auths.go
+++ b/routers/web/admin/auths.go
@@ -181,6 +181,7 @@ func parseOAuth2Config(form forms.AuthenticationForm) *oauth2.Source {
OpenIDConnectAutoDiscoveryURL: form.OpenIDConnectAutoDiscoveryURL,
CustomURLMapping: customURLMapping,
IconURL: form.Oauth2IconURL,
+ SkipLocalTwoFA: form.SkipLocalTwoFA,
}
}
diff --git a/routers/web/user/auth.go b/routers/web/user/auth.go
index 313a583004..38e0d989b8 100644
--- a/routers/web/user/auth.go
+++ b/routers/web/user/auth.go
@@ -574,7 +574,7 @@ func SignInOAuth(ctx *context.Context) {
user, gothUser, err := oAuth2UserLoginCallback(loginSource, ctx.Req, ctx.Resp)
if err == nil && user != nil {
// we got the user without going through the whole OAuth2 authentication flow again
- handleOAuth2SignIn(ctx, user, gothUser)
+ handleOAuth2SignIn(ctx, loginSource, user, gothUser)
return
}
@@ -660,7 +660,7 @@ func SignInOAuthCallback(ctx *context.Context) {
}
}
- handleOAuth2SignIn(ctx, u, gothUser)
+ handleOAuth2SignIn(ctx, loginSource, u, gothUser)
}
func getUserName(gothUser *goth.User) string {
@@ -702,18 +702,22 @@ func updateAvatarIfNeed(url string, u *models.User) {
}
}
-func handleOAuth2SignIn(ctx *context.Context, u *models.User, gothUser goth.User) {
+func handleOAuth2SignIn(ctx *context.Context, source *models.LoginSource, u *models.User, gothUser goth.User) {
updateAvatarIfNeed(gothUser.AvatarURL, u)
- // If this user is enrolled in 2FA, we can't sign the user in just yet.
- // Instead, redirect them to the 2FA authentication page.
- _, err := models.GetTwoFactorByUID(u.ID)
- if err != nil {
- if !models.IsErrTwoFactorNotEnrolled(err) {
+ needs2FA := false
+ if !source.Cfg.(*oauth2.Source).SkipLocalTwoFA {
+ _, err := models.GetTwoFactorByUID(u.ID)
+ if err != nil && !models.IsErrTwoFactorNotEnrolled(err) {
ctx.ServerError("UserSignIn", err)
return
}
+ needs2FA = err == nil
+ }
+ // If this user is enrolled in 2FA and this source doesn't override it,
+ // we can't sign the user in just yet. Instead, redirect them to the 2FA authentication page.
+ if !needs2FA {
if err := ctx.Session.Set("uid", u.ID); err != nil {
log.Error("Error setting uid in session: %v", err)
}