diff --git a/cmd/admin.go b/cmd/admin.go index 318c212d0867..d5beab8abaa6 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -372,6 +372,10 @@ var ( Value: "", Usage: "Group Claim value for restricted users", }, + cli.BoolFlag{ + Name: "force-oauth", + Usage: "set to force all logins to the configured oauth provider", + }, cli.StringFlag{ Name: "group-team-map", Value: "", @@ -856,6 +860,7 @@ func parseOAuth2Config(c *cli.Context) *oauth2.Source { CustomURLMapping: customURLMapping, IconURL: c.String("icon-url"), SkipLocalTwoFA: c.Bool("skip-local-2fa"), + ForceOAuth: c.Bool("force-oauth"), Scopes: c.StringSlice("scopes"), RequiredClaimName: c.String("required-claim-name"), RequiredClaimValue: c.String("required-claim-value"), @@ -946,6 +951,9 @@ func runUpdateOauth(c *cli.Context) error { if c.IsSet("restricted-group") { oAuth2Config.RestrictedGroup = c.String("restricted-group") } + if c.IsSet("force-oauth") { + oAuth2Config.ForceOAuth = c.BoolT("force-oauth") + } if c.IsSet("group-team-map") { oAuth2Config.GroupTeamMap = c.String("group-team-map") } diff --git a/docs/content/doc/usage/command-line.en-us.md b/docs/content/doc/usage/command-line.en-us.md index 9b861a9da39e..9cf463de3641 100644 --- a/docs/content/doc/usage/command-line.en-us.md +++ b/docs/content/doc/usage/command-line.en-us.md @@ -131,6 +131,7 @@ Admin operations: - `--custom-email-url`: Use a custom Email URL (option for GitHub). - `--icon-url`: Custom icon URL for OAuth2 login source. - `--skip-local-2fa`: Allow source to override local 2FA. (Optional) + - `--force-oauth`: Automatically redirect sign in to this OAuth provider (Optional) - `--scopes`: Additional scopes to request for this OAuth2 source. (Optional) - `--required-claim-name`: Claim name that has to be set to allow users to login with this source. (Optional) - `--required-claim-value`: Claim value that has to be set to allow users to login with this source. (Optional) @@ -157,6 +158,7 @@ Admin operations: - `--custom-email-url`: Use a custom Email URL (option for GitHub). - `--icon-url`: Custom icon URL for OAuth2 login source. - `--skip-local-2fa`: Allow source to override local 2FA. (Optional) + - `--force-oauth`: Automatically redirect sign in to this OAuth provider (Optional) - `--scopes`: Additional scopes to request for this OAuth2 source. - `--required-claim-name`: Claim name that has to be set to allow users to login with this source. (Optional) - `--required-claim-value`: Claim value that has to be set to allow users to login with this source. (Optional) diff --git a/options/locale/locale_de-DE.ini b/options/locale/locale_de-DE.ini index d8874fd5fa82..c74d36697de7 100644 --- a/options/locale/locale_de-DE.ini +++ b/options/locale/locale_de-DE.ini @@ -2666,6 +2666,8 @@ auths.oauth2_profileURL=Profil-URL auths.oauth2_emailURL=E-Mail-URL auths.skip_local_two_fa=Lokale 2FA überspringen auths.skip_local_two_fa_helper=Leer lassen bedeutet, dass lokale User die 2FA immer noch bestehen müssen, um sich anzumelden +auths.force_o_auth=Anmelden durch diese Quelle erzwingen +auths.force_o_auth_helper=Setzen um Anmeldungen automatisch auf diesen OAuth Anbieter umzuleiten auths.oauth2_tenant=Inhaber auths.oauth2_scopes=Zusätzliche Bereiche auths.oauth2_required_claim_name=Benötigter Claim-Name diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 5d0fd044f42e..5e445286d73b 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2753,6 +2753,8 @@ auths.oauth2_profileURL = Profile URL auths.oauth2_emailURL = Email URL auths.skip_local_two_fa = Skip local 2FA auths.skip_local_two_fa_helper = Leaving unset means local users with 2FA set will still have to pass 2FA to log on +auths.force_o_auth = Force login via this authentication +auths.force_o_auth_helper = Set this to automatically redirect sign in to this OAuth provider auths.oauth2_tenant = Tenant auths.oauth2_scopes = Additional Scopes auths.oauth2_required_claim_name = Required Claim Name diff --git a/routers/web/admin/auths.go b/routers/web/admin/auths.go index 8ce45720fec3..fc89c1d73d3c 100644 --- a/routers/web/admin/auths.go +++ b/routers/web/admin/auths.go @@ -201,6 +201,7 @@ func parseOAuth2Config(form forms.AuthenticationForm) *oauth2.Source { RequiredClaimName: form.Oauth2RequiredClaimName, RequiredClaimValue: form.Oauth2RequiredClaimValue, SkipLocalTwoFA: form.SkipLocalTwoFA, + ForceOAuth: form.ForceOAuth, GroupClaimName: form.Oauth2GroupClaimName, RestrictedGroup: form.Oauth2RestrictedGroup, AdminGroup: form.Oauth2AdminGroup, diff --git a/routers/web/auth/auth.go b/routers/web/auth/auth.go index 48b7dc6862ae..a213c695d3ed 100644 --- a/routers/web/auth/auth.go +++ b/routers/web/auth/auth.go @@ -137,6 +137,32 @@ func checkAutoLogin(ctx *context.Context) bool { return false } +func checkForceOAuth(ctx *context.Context) bool { + // Check if authentication is forced to OAuth + + authSources, err := auth.GetActiveOAuth2ProviderSources() + if err != nil { + return false + } + + var OAuthList []int64 + + for _, source := range authSources { + if forced, ok := source.Cfg.(auth_service.ForceOAuth); ok && forced.IsOAuthForced() { + OAuthList = append(OAuthList, source.ID) + app, err := auth.GetOAuth2ApplicationByID(ctx, OAuthList[0]) + if err != nil { + return false + } + url := app.PrimaryRedirectURI() + ctx.Redirect(url) + return true + } + } + + return false +} + // SignIn render sign in page func SignIn(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("sign_in") @@ -146,6 +172,11 @@ func SignIn(ctx *context.Context) { return } + // Check if authentication is forced to OAuth + if checkForceOAuth(ctx) { + return + } + orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers() if err != nil { ctx.ServerError("UserSignIn", err) diff --git a/services/auth/interface.go b/services/auth/interface.go index f2f1aaf39cb0..0ad2095381ef 100644 --- a/services/auth/interface.go +++ b/services/auth/interface.go @@ -59,6 +59,10 @@ type LocalTwoFASkipper interface { IsSkipLocalTwoFA() bool } +type ForceOAuth interface { + IsOAuthForced() bool +} + // SynchronizableSource represents a source that can synchronize users type SynchronizableSource interface { Sync(ctx context.Context, updateExisting bool) error diff --git a/services/auth/source/oauth2/source.go b/services/auth/source/oauth2/source.go index 675005e55ab5..21b1154bbef0 100644 --- a/services/auth/source/oauth2/source.go +++ b/services/auth/source/oauth2/source.go @@ -16,6 +16,7 @@ type Source struct { OpenIDConnectAutoDiscoveryURL string CustomURLMapping *CustomURLMapping IconURL string + ForceOAuth bool `json:",omitempty"` Scopes []string RequiredClaimName string diff --git a/services/auth/source/oauth2/source_authenticate.go b/services/auth/source/oauth2/source_authenticate.go index e3e2a9e192f5..ac756ae16410 100644 --- a/services/auth/source/oauth2/source_authenticate.go +++ b/services/auth/source/oauth2/source_authenticate.go @@ -13,5 +13,9 @@ func (source *Source) Authenticate(user *user_model.User, login, password string return db.Authenticate(user, login, password) } +func (source *Source) IsOAuthForced() bool { + return source.ForceOAuth +} + // NB: Oauth2 does not implement LocalTwoFASkipper for password authentication // as its password authentication drops to db authentication diff --git a/services/forms/auth_form.go b/services/forms/auth_form.go index 5625aa1e2ed4..b4e133c89233 100644 --- a/services/forms/auth_form.go +++ b/services/forms/auth_form.go @@ -75,6 +75,7 @@ type AuthenticationForm struct { Oauth2GroupTeamMap string `binding:"ValidGroupTeamMap"` Oauth2GroupTeamMapRemoval bool SkipLocalTwoFA bool + ForceOAuth bool SSPIAutoCreateUsers bool SSPIAutoActivateUsers bool SSPIStripDomainNames bool diff --git a/templates/admin/auth/edit.tmpl b/templates/admin/auth/edit.tmpl index a3c94a6cc216..066d62353f81 100644 --- a/templates/admin/auth/edit.tmpl +++ b/templates/admin/auth/edit.tmpl @@ -299,6 +299,13 @@

{{.locale.Tr "admin.auths.skip_local_two_fa_helper"}}

+
+
+ + +

{{.locale.Tr "admin.auths.force_o_auth_helper"}}

+
+
diff --git a/templates/admin/auth/source/oauth.tmpl b/templates/admin/auth/source/oauth.tmpl index 85c77343a528..b2a87dda318b 100644 --- a/templates/admin/auth/source/oauth.tmpl +++ b/templates/admin/auth/source/oauth.tmpl @@ -35,6 +35,13 @@

{{.locale.Tr "admin.auths.skip_local_two_fa_helper"}}

+
+
+ + +

{{.locale.Tr "admin.auths.force_o_auth_helper"}}

+
+