Skip to content

Commit 840ad7e

Browse files
lunnyGiteaBotwxiaoguang
authored
Disable Oauth check if oauth disabled (#32368)
Fix #32367 --------- Co-authored-by: Giteabot <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent 5bed7b9 commit 840ad7e

File tree

2 files changed

+44
-33
lines changed

2 files changed

+44
-33
lines changed

Diff for: routers/web/web.go

+38-32
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,13 @@ func registerRoutes(m *web.Router) {
324324
}
325325
}
326326

327+
oauth2Enabled := func(ctx *context.Context) {
328+
if !setting.OAuth2.Enabled {
329+
ctx.Error(http.StatusForbidden)
330+
return
331+
}
332+
}
333+
327334
reqMilestonesDashboardPageEnabled := func(ctx *context.Context) {
328335
if !setting.Service.ShowMilestonesDashboardPage {
329336
ctx.Error(http.StatusForbidden)
@@ -546,16 +553,18 @@ func registerRoutes(m *web.Router) {
546553
m.Any("/user/events", routing.MarkLongPolling, events.Events)
547554

548555
m.Group("/login/oauth", func() {
549-
m.Get("/authorize", web.Bind(forms.AuthorizationForm{}), auth.AuthorizeOAuth)
550-
m.Post("/grant", web.Bind(forms.GrantApplicationForm{}), auth.GrantApplicationOAuth)
551-
// TODO manage redirection
552-
m.Post("/authorize", web.Bind(forms.AuthorizationForm{}), auth.AuthorizeOAuth)
553-
}, ignSignInAndCsrf, reqSignIn)
554-
555-
m.Methods("GET, OPTIONS", "/login/oauth/userinfo", optionsCorsHandler(), ignSignInAndCsrf, auth.InfoOAuth)
556-
m.Methods("POST, OPTIONS", "/login/oauth/access_token", optionsCorsHandler(), web.Bind(forms.AccessTokenForm{}), ignSignInAndCsrf, auth.AccessTokenOAuth)
557-
m.Methods("GET, OPTIONS", "/login/oauth/keys", optionsCorsHandler(), ignSignInAndCsrf, auth.OIDCKeys)
558-
m.Methods("POST, OPTIONS", "/login/oauth/introspect", optionsCorsHandler(), web.Bind(forms.IntrospectTokenForm{}), ignSignInAndCsrf, auth.IntrospectOAuth)
556+
m.Group("", func() {
557+
m.Get("/authorize", web.Bind(forms.AuthorizationForm{}), auth.AuthorizeOAuth)
558+
m.Post("/grant", web.Bind(forms.GrantApplicationForm{}), auth.GrantApplicationOAuth)
559+
// TODO manage redirection
560+
m.Post("/authorize", web.Bind(forms.AuthorizationForm{}), auth.AuthorizeOAuth)
561+
}, ignSignInAndCsrf, reqSignIn)
562+
563+
m.Methods("GET, OPTIONS", "/userinfo", optionsCorsHandler(), ignSignInAndCsrf, auth.InfoOAuth)
564+
m.Methods("POST, OPTIONS", "/access_token", optionsCorsHandler(), web.Bind(forms.AccessTokenForm{}), ignSignInAndCsrf, auth.AccessTokenOAuth)
565+
m.Methods("GET, OPTIONS", "/keys", optionsCorsHandler(), ignSignInAndCsrf, auth.OIDCKeys)
566+
m.Methods("POST, OPTIONS", "/introspect", optionsCorsHandler(), web.Bind(forms.IntrospectTokenForm{}), ignSignInAndCsrf, auth.IntrospectOAuth)
567+
}, oauth2Enabled)
559568

560569
m.Group("/user/settings", func() {
561570
m.Get("", user_setting.Profile)
@@ -596,17 +605,24 @@ func registerRoutes(m *web.Router) {
596605
}, openIDSignInEnabled)
597606
m.Post("/account_link", linkAccountEnabled, security.DeleteAccountLink)
598607
})
599-
m.Group("/applications/oauth2", func() {
600-
m.Get("/{id}", user_setting.OAuth2ApplicationShow)
601-
m.Post("/{id}", web.Bind(forms.EditOAuth2ApplicationForm{}), user_setting.OAuthApplicationsEdit)
602-
m.Post("/{id}/regenerate_secret", user_setting.OAuthApplicationsRegenerateSecret)
603-
m.Post("", web.Bind(forms.EditOAuth2ApplicationForm{}), user_setting.OAuthApplicationsPost)
604-
m.Post("/{id}/delete", user_setting.DeleteOAuth2Application)
605-
m.Post("/{id}/revoke/{grantId}", user_setting.RevokeOAuth2Grant)
608+
609+
m.Group("/applications", func() {
610+
// oauth2 applications
611+
m.Group("/oauth2", func() {
612+
m.Get("/{id}", user_setting.OAuth2ApplicationShow)
613+
m.Post("/{id}", web.Bind(forms.EditOAuth2ApplicationForm{}), user_setting.OAuthApplicationsEdit)
614+
m.Post("/{id}/regenerate_secret", user_setting.OAuthApplicationsRegenerateSecret)
615+
m.Post("", web.Bind(forms.EditOAuth2ApplicationForm{}), user_setting.OAuthApplicationsPost)
616+
m.Post("/{id}/delete", user_setting.DeleteOAuth2Application)
617+
m.Post("/{id}/revoke/{grantId}", user_setting.RevokeOAuth2Grant)
618+
}, oauth2Enabled)
619+
620+
// access token applications
621+
m.Combo("").Get(user_setting.Applications).
622+
Post(web.Bind(forms.NewAccessTokenForm{}), user_setting.ApplicationsPost)
623+
m.Post("/delete", user_setting.DeleteApplication)
606624
})
607-
m.Combo("/applications").Get(user_setting.Applications).
608-
Post(web.Bind(forms.NewAccessTokenForm{}), user_setting.ApplicationsPost)
609-
m.Post("/applications/delete", user_setting.DeleteApplication)
625+
610626
m.Combo("/keys").Get(user_setting.Keys).
611627
Post(web.Bind(forms.AddKeyForm{}), user_setting.KeysPost)
612628
m.Post("/keys/delete", user_setting.DeleteKey)
@@ -780,12 +796,7 @@ func registerRoutes(m *web.Router) {
780796
m.Post("/regenerate_secret", admin.ApplicationsRegenerateSecret)
781797
m.Post("/delete", admin.DeleteApplication)
782798
})
783-
}, func(ctx *context.Context) {
784-
if !setting.OAuth2.Enabled {
785-
ctx.Error(http.StatusForbidden)
786-
return
787-
}
788-
})
799+
}, oauth2Enabled)
789800

790801
m.Group("/actions", func() {
791802
m.Get("", admin.RedirectToDefaultSetting)
@@ -909,12 +920,7 @@ func registerRoutes(m *web.Router) {
909920
m.Post("/regenerate_secret", org.OAuthApplicationsRegenerateSecret)
910921
m.Post("/delete", org.DeleteOAuth2Application)
911922
})
912-
}, func(ctx *context.Context) {
913-
if !setting.OAuth2.Enabled {
914-
ctx.Error(http.StatusForbidden)
915-
return
916-
}
917-
})
923+
}, oauth2Enabled)
918924

919925
m.Group("/hooks", func() {
920926
m.Get("", org.Webhooks)

Diff for: services/auth/oauth2.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ var (
2727

2828
// CheckOAuthAccessToken returns uid of user from oauth token
2929
func CheckOAuthAccessToken(ctx context.Context, accessToken string) int64 {
30-
// JWT tokens require a "."
30+
if !setting.OAuth2.Enabled {
31+
return 0
32+
}
33+
34+
// JWT tokens require a ".", if the token isn't like that, return early
3135
if !strings.Contains(accessToken, ".") {
3236
return 0
3337
}
38+
3439
token, err := oauth2_provider.ParseToken(accessToken, oauth2_provider.DefaultSigningKey)
3540
if err != nil {
3641
log.Trace("oauth2.ParseToken: %v", err)

0 commit comments

Comments
 (0)