Skip to content

Commit d6cddbd

Browse files
tiltingpenguinbrechtvl
authored andcommitted
Add setting to force login through openid
This PR aims to add a setting that skips the normal login and redirects automatically to the openID login/the configured openID provider The desired behavior can be observed at https://gitea.opensuse.org/ BLENDER NOTE: this is is PR go-gitea#21851, in the hope that this will be merged upstream.
1 parent b5b3e07 commit d6cddbd

File tree

12 files changed

+70
-0
lines changed

12 files changed

+70
-0
lines changed

cmd/admin.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ var (
362362
Value: "",
363363
Usage: "Group Claim value for restricted users",
364364
},
365+
cli.BoolFlag{
366+
Name: "force-oauth",
367+
Usage: "set to force all logins to the configured oauth provider",
368+
},
365369
}
366370

367371
microcmdAuthUpdateOauth = cli.Command{
@@ -826,6 +830,7 @@ func parseOAuth2Config(c *cli.Context) *oauth2.Source {
826830
CustomURLMapping: customURLMapping,
827831
IconURL: c.String("icon-url"),
828832
SkipLocalTwoFA: c.Bool("skip-local-2fa"),
833+
ForceOAuth: c.Bool("force-oauth"),
829834
Scopes: c.StringSlice("scopes"),
830835
RequiredClaimName: c.String("required-claim-name"),
831836
RequiredClaimValue: c.String("required-claim-value"),
@@ -914,6 +919,9 @@ func runUpdateOauth(c *cli.Context) error {
914919
if c.IsSet("restricted-group") {
915920
oAuth2Config.RestrictedGroup = c.String("restricted-group")
916921
}
922+
if c.IsSet("force-oauth") {
923+
oAuth2Config.ForceOAuth = c.BoolT("force-oauth")
924+
}
917925

918926
// update custom URL mapping
919927
customURLMapping := &oauth2.CustomURLMapping{}

docs/content/doc/usage/command-line.en-us.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Admin operations:
130130
- `--custom-email-url`: Use a custom Email URL (option for GitHub).
131131
- `--icon-url`: Custom icon URL for OAuth2 login source.
132132
- `--skip-local-2fa`: Allow source to override local 2FA. (Optional)
133+
- `--force-oauth`: Automatically redirect sign in to this OAuth provider (Optional)
133134
- `--scopes`: Additional scopes to request for this OAuth2 source. (Optional)
134135
- `--required-claim-name`: Claim name that has to be set to allow users to login with this source. (Optional)
135136
- `--required-claim-value`: Claim value that has to be set to allow users to login with this source. (Optional)
@@ -153,6 +154,7 @@ Admin operations:
153154
- `--custom-email-url`: Use a custom Email URL (option for GitHub).
154155
- `--icon-url`: Custom icon URL for OAuth2 login source.
155156
- `--skip-local-2fa`: Allow source to override local 2FA. (Optional)
157+
- `--force-oauth`: Automatically redirect sign in to this OAuth provider (Optional)
156158
- `--scopes`: Additional scopes to request for this OAuth2 source.
157159
- `--required-claim-name`: Claim name that has to be set to allow users to login with this source. (Optional)
158160
- `--required-claim-value`: Claim value that has to be set to allow users to login with this source. (Optional)

options/locale/locale_de-DE.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,6 +2666,8 @@ auths.oauth2_profileURL=Profil-URL
26662666
auths.oauth2_emailURL=E-Mail-URL
26672667
auths.skip_local_two_fa=Lokale 2FA überspringen
26682668
auths.skip_local_two_fa_helper=Leer lassen bedeutet, dass lokale User die 2FA immer noch bestehen müssen, um sich anzumelden
2669+
auths.force_o_auth=Anmelden durch diese Quelle erzwingen
2670+
auths.force_o_auth_helper=Setzen um Anmeldungen automatisch auf diesen OAuth Anbieter umzuleiten
26692671
auths.oauth2_tenant=Inhaber
26702672
auths.oauth2_scopes=Zusätzliche Bereiche
26712673
auths.oauth2_required_claim_name=Benötigter Claim-Name

options/locale/locale_en-US.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,6 +2739,8 @@ auths.oauth2_profileURL = Profile URL
27392739
auths.oauth2_emailURL = Email URL
27402740
auths.skip_local_two_fa = Skip local 2FA
27412741
auths.skip_local_two_fa_helper = Leaving unset means local users with 2FA set will still have to pass 2FA to log on
2742+
auths.force_o_auth = Force login via this authentication
2743+
auths.force_o_auth_helper = Set this to automatically redirect sign in to this OAuth provider
27422744
auths.oauth2_tenant = Tenant
27432745
auths.oauth2_scopes = Additional Scopes
27442746
auths.oauth2_required_claim_name = Required Claim Name

routers/web/admin/auths.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ func parseOAuth2Config(form forms.AuthenticationForm) *oauth2.Source {
201201
RequiredClaimName: form.Oauth2RequiredClaimName,
202202
RequiredClaimValue: form.Oauth2RequiredClaimValue,
203203
SkipLocalTwoFA: form.SkipLocalTwoFA,
204+
ForceOAuth: form.ForceOAuth,
204205
GroupClaimName: form.Oauth2GroupClaimName,
205206
RestrictedGroup: form.Oauth2RestrictedGroup,
206207
AdminGroup: form.Oauth2AdminGroup,

routers/web/auth/auth.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,32 @@ func checkAutoLogin(ctx *context.Context) bool {
137137
return false
138138
}
139139

140+
func checkForceOAuth(ctx *context.Context) bool {
141+
// Check if authentication is forced to OAuth
142+
143+
authSources, err := auth.GetActiveOAuth2ProviderSources()
144+
if err != nil {
145+
return false
146+
}
147+
148+
var OAuthList []int64
149+
150+
for _, source := range authSources {
151+
if forced, ok := source.Cfg.(auth_service.ForceOAuth); ok && forced.IsOAuthForced() {
152+
OAuthList = append(OAuthList, source.ID)
153+
app, err := auth.GetOAuth2ApplicationByID(ctx, OAuthList[0])
154+
if err != nil {
155+
return false
156+
}
157+
url := app.PrimaryRedirectURI()
158+
ctx.Redirect(url)
159+
return true
160+
}
161+
}
162+
163+
return false
164+
}
165+
140166
// SignIn render sign in page
141167
func SignIn(ctx *context.Context) {
142168
ctx.Data["Title"] = ctx.Tr("sign_in")
@@ -146,6 +172,11 @@ func SignIn(ctx *context.Context) {
146172
return
147173
}
148174

175+
// Check if authentication is forced to OAuth
176+
if checkForceOAuth(ctx) {
177+
return
178+
}
179+
149180
orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers()
150181
if err != nil {
151182
ctx.ServerError("UserSignIn", err)

services/auth/interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ type LocalTwoFASkipper interface {
5959
IsSkipLocalTwoFA() bool
6060
}
6161

62+
type ForceOAuth interface {
63+
IsOAuthForced() bool
64+
}
65+
6266
// SynchronizableSource represents a source that can synchronize users
6367
type SynchronizableSource interface {
6468
Sync(ctx context.Context, updateExisting bool) error

services/auth/source/oauth2/source.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Source struct {
2323
OpenIDConnectAutoDiscoveryURL string
2424
CustomURLMapping *CustomURLMapping
2525
IconURL string
26+
ForceOAuth bool `json:",omitempty"`
2627

2728
Scopes []string
2829
RequiredClaimName string

services/auth/source/oauth2/source_authenticate.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ func (source *Source) Authenticate(user *user_model.User, login, password string
1313
return db.Authenticate(user, login, password)
1414
}
1515

16+
func (source *Source) IsOAuthForced() bool {
17+
return source.ForceOAuth
18+
}
19+
1620
// NB: Oauth2 does not implement LocalTwoFASkipper for password authentication
1721
// as its password authentication drops to db authentication

services/forms/auth_form.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ type AuthenticationForm struct {
7373
Oauth2AdminGroup string
7474
Oauth2RestrictedGroup string
7575
SkipLocalTwoFA bool
76+
ForceOAuth bool
7677
SSPIAutoCreateUsers bool
7778
SSPIAutoActivateUsers bool
7879
SSPIStripDomainNames bool

0 commit comments

Comments
 (0)