Skip to content

Commit ceaf11c

Browse files
committed
Add setting to OAuth handlers to override local 2FA settings
This PR adds a setting to OAuth and OpenID login sources to allow the source to override local 2FA requirements. Fix #13939 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 06f8264 commit ceaf11c

File tree

8 files changed

+36
-8
lines changed

8 files changed

+36
-8
lines changed

cmd/admin.go

+5
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ var (
288288
Value: "",
289289
Usage: "Custom icon URL for OAuth2 login source",
290290
},
291+
cli.BoolFlag{
292+
Name: "override-local-2fa",
293+
Usage: "Set to true to override local 2fa settings",
294+
},
291295
}
292296

293297
microcmdAuthUpdateOauth = cli.Command{
@@ -616,6 +620,7 @@ func parseOAuth2Config(c *cli.Context) *oauth2.Source {
616620
OpenIDConnectAutoDiscoveryURL: c.String("auto-discover-url"),
617621
CustomURLMapping: customURLMapping,
618622
IconURL: c.String("icon-url"),
623+
OverrideLocalTwoFA: c.Bool("override-local-2fa"),
619624
}
620625
}
621626

options/locale/locale_en-US.ini

+2
Original file line numberDiff line numberDiff line change
@@ -2448,6 +2448,8 @@ auths.oauth2_tokenURL = Token URL
24482448
auths.oauth2_authURL = Authorize URL
24492449
auths.oauth2_profileURL = Profile URL
24502450
auths.oauth2_emailURL = Email URL
2451+
auths.override_local_two_fa = Override local 2FA
2452+
auths.override_local_two_fa_helper = Leaving unset means local users with 2FA set will still have to pass 2FA to log on
24512453
auths.oauth2_tenant = Tenant
24522454
auths.enable_auto_register = Enable Auto Registration
24532455
auths.sspi_auto_create_users = Automatically create users

routers/web/admin/auths.go

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ func parseOAuth2Config(form forms.AuthenticationForm) *oauth2.Source {
181181
OpenIDConnectAutoDiscoveryURL: form.OpenIDConnectAutoDiscoveryURL,
182182
CustomURLMapping: customURLMapping,
183183
IconURL: form.Oauth2IconURL,
184+
OverrideLocalTwoFA: form.OverrideLocalTwoFA,
184185
}
185186
}
186187

routers/web/user/auth.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ func SignInOAuth(ctx *context.Context) {
574574
user, gothUser, err := oAuth2UserLoginCallback(loginSource, ctx.Req, ctx.Resp)
575575
if err == nil && user != nil {
576576
// we got the user without going through the whole OAuth2 authentication flow again
577-
handleOAuth2SignIn(ctx, user, gothUser)
577+
handleOAuth2SignIn(ctx, loginSource, user, gothUser)
578578
return
579579
}
580580

@@ -660,7 +660,7 @@ func SignInOAuthCallback(ctx *context.Context) {
660660
}
661661
}
662662

663-
handleOAuth2SignIn(ctx, u, gothUser)
663+
handleOAuth2SignIn(ctx, loginSource, u, gothUser)
664664
}
665665

666666
func getUserName(gothUser *goth.User) string {
@@ -702,18 +702,22 @@ func updateAvatarIfNeed(url string, u *models.User) {
702702
}
703703
}
704704

705-
func handleOAuth2SignIn(ctx *context.Context, u *models.User, gothUser goth.User) {
705+
func handleOAuth2SignIn(ctx *context.Context, source *models.LoginSource, u *models.User, gothUser goth.User) {
706706
updateAvatarIfNeed(gothUser.AvatarURL, u)
707707

708-
// If this user is enrolled in 2FA, we can't sign the user in just yet.
709-
// Instead, redirect them to the 2FA authentication page.
710-
_, err := models.GetTwoFactorByUID(u.ID)
711-
if err != nil {
712-
if !models.IsErrTwoFactorNotEnrolled(err) {
708+
needs2FA := false
709+
if !source.Cfg.(*oauth2.Source).OverrideLocalTwoFA {
710+
_, err := models.GetTwoFactorByUID(u.ID)
711+
if err != nil && !models.IsErrTwoFactorNotEnrolled(err) {
713712
ctx.ServerError("UserSignIn", err)
714713
return
715714
}
715+
needs2FA = err == nil
716+
}
716717

718+
// If this user is enrolled in 2FA and this source doesn't override it,
719+
// we can't sign the user in just yet. Instead, redirect them to the 2FA authentication page.
720+
if !needs2FA {
717721
if err := ctx.Session.Set("uid", u.ID); err != nil {
718722
log.Error("Error setting uid in session: %v", err)
719723
}

services/auth/source/oauth2/source.go

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Source struct {
2424
OpenIDConnectAutoDiscoveryURL string
2525
CustomURLMapping *CustomURLMapping
2626
IconURL string
27+
OverrideLocalTwoFA bool
2728

2829
// reference to the loginSource
2930
loginSource *models.LoginSource

services/forms/auth_form.go

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type AuthenticationForm struct {
6666
Oauth2EmailURL string
6767
Oauth2IconURL string
6868
Oauth2Tenant string
69+
OverrideLocalTwoFA bool
6970
SSPIAutoCreateUsers bool
7071
SSPIAutoActivateUsers bool
7172
SSPIStripDomainNames bool

templates/admin/auth/edit.tmpl

+7
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,13 @@
255255
<label for="open_id_connect_auto_discovery_url">{{.i18n.Tr "admin.auths.openIdConnectAutoDiscoveryURL"}}</label>
256256
<input id="open_id_connect_auto_discovery_url" name="open_id_connect_auto_discovery_url" value="{{$cfg.OpenIDConnectAutoDiscoveryURL}}">
257257
</div>
258+
<div class="optional field">
259+
<div class="ui checkbox">
260+
<label for="override_local_two_fa"><strong>{{.i18n.Tr "admin.auths.override_local_two_fa"}}</strong></label>
261+
<input id="override_local_two_fa" name="override_local_two_fa" type="checkbox" {{if $cfg.OverrideLocalTwoFA}}checked{{end}}>
262+
<p class="help">{{.i18n.Tr "admin.auths.override_local_two_fa_helper"}}</p>
263+
</div>
264+
</div>
258265

259266
<div class="oauth2_use_custom_url inline field">
260267
<div class="ui checkbox">

templates/admin/auth/source/oauth.tmpl

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
<label for="open_id_connect_auto_discovery_url">{{.i18n.Tr "admin.auths.openIdConnectAutoDiscoveryURL"}}</label>
2929
<input id="open_id_connect_auto_discovery_url" name="open_id_connect_auto_discovery_url" value="{{.open_id_connect_auto_discovery_url}}">
3030
</div>
31+
<div class="optional field">
32+
<div class="ui checkbox">
33+
<label for="override_local_two_fa"><strong>{{.i18n.Tr "admin.auths.override_local_two_fa"}}</strong></label>
34+
<input id="override_local_two_fa" name="override_local_two_fa" type="checkbox" {{if .override_local_two_fa}}checked{{end}}>
35+
<p class="help">{{.i18n.Tr "admin.auths.override_local_two_fa_helper"}}</p>
36+
</div>
37+
</div>
3138

3239
<div class="oauth2_use_custom_url inline field">
3340
<div class="ui checkbox">

0 commit comments

Comments
 (0)