Skip to content

Commit 8cec637

Browse files
lunnyearl-warren
authored andcommitted
Disable Oauth check if oauth disabled (go-gitea#32368)
Fix go-gitea#32367 --------- Co-authored-by: Giteabot <[email protected]> Co-authored-by: wxiaoguang <[email protected]> (cherry picked from commit 840ad7e) Conflicts: services/auth/oauth2.go trivial context conflict
1 parent 9f05c76 commit 8cec637

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

routers/web/web.go

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,13 @@ func registerRoutes(m *web.Route) {
327327
}
328328
}
329329

330+
oauth2Enabled := func(ctx *context.Context) {
331+
if !setting.OAuth2.Enabled {
332+
ctx.Error(http.StatusForbidden)
333+
return
334+
}
335+
}
336+
330337
reqMilestonesDashboardPageEnabled := func(ctx *context.Context) {
331338
if !setting.Service.ShowMilestonesDashboardPage {
332339
ctx.Error(http.StatusForbidden)
@@ -516,16 +523,18 @@ func registerRoutes(m *web.Route) {
516523
m.Any("/user/events", routing.MarkLongPolling, events.Events)
517524

518525
m.Group("/login/oauth", func() {
519-
m.Get("/authorize", web.Bind(forms.AuthorizationForm{}), auth.AuthorizeOAuth)
520-
m.Post("/grant", web.Bind(forms.GrantApplicationForm{}), auth.GrantApplicationOAuth)
521-
// TODO manage redirection
522-
m.Post("/authorize", web.Bind(forms.AuthorizationForm{}), auth.AuthorizeOAuth)
523-
}, ignSignInAndCsrf, reqSignIn)
524-
525-
m.Methods("GET, OPTIONS", "/login/oauth/userinfo", optionsCorsHandler(), ignSignInAndCsrf, auth.InfoOAuth)
526-
m.Methods("POST, OPTIONS", "/login/oauth/access_token", optionsCorsHandler(), web.Bind(forms.AccessTokenForm{}), ignSignInAndCsrf, auth.AccessTokenOAuth)
527-
m.Methods("GET, OPTIONS", "/login/oauth/keys", optionsCorsHandler(), ignSignInAndCsrf, auth.OIDCKeys)
528-
m.Methods("POST, OPTIONS", "/login/oauth/introspect", optionsCorsHandler(), web.Bind(forms.IntrospectTokenForm{}), ignSignInAndCsrf, auth.IntrospectOAuth)
526+
m.Group("", func() {
527+
m.Get("/authorize", web.Bind(forms.AuthorizationForm{}), auth.AuthorizeOAuth)
528+
m.Post("/grant", web.Bind(forms.GrantApplicationForm{}), auth.GrantApplicationOAuth)
529+
// TODO manage redirection
530+
m.Post("/authorize", web.Bind(forms.AuthorizationForm{}), auth.AuthorizeOAuth)
531+
}, ignSignInAndCsrf, reqSignIn)
532+
533+
m.Methods("GET, OPTIONS", "/userinfo", optionsCorsHandler(), ignSignInAndCsrf, auth.InfoOAuth)
534+
m.Methods("POST, OPTIONS", "/access_token", optionsCorsHandler(), web.Bind(forms.AccessTokenForm{}), ignSignInAndCsrf, auth.AccessTokenOAuth)
535+
m.Methods("GET, OPTIONS", "/keys", optionsCorsHandler(), ignSignInAndCsrf, auth.OIDCKeys)
536+
m.Methods("POST, OPTIONS", "/introspect", optionsCorsHandler(), web.Bind(forms.IntrospectTokenForm{}), ignSignInAndCsrf, auth.IntrospectOAuth)
537+
}, oauth2Enabled)
529538

530539
m.Group("/user/settings", func() {
531540
m.Get("", user_setting.Profile)
@@ -567,17 +576,24 @@ func registerRoutes(m *web.Route) {
567576
}, openIDSignInEnabled)
568577
m.Post("/account_link", linkAccountEnabled, security.DeleteAccountLink)
569578
})
570-
m.Group("/applications/oauth2", func() {
571-
m.Get("/{id}", user_setting.OAuth2ApplicationShow)
572-
m.Post("/{id}", web.Bind(forms.EditOAuth2ApplicationForm{}), user_setting.OAuthApplicationsEdit)
573-
m.Post("/{id}/regenerate_secret", user_setting.OAuthApplicationsRegenerateSecret)
574-
m.Post("", web.Bind(forms.EditOAuth2ApplicationForm{}), user_setting.OAuthApplicationsPost)
575-
m.Post("/{id}/delete", user_setting.DeleteOAuth2Application)
576-
m.Post("/{id}/revoke/{grantId}", user_setting.RevokeOAuth2Grant)
579+
580+
m.Group("/applications", func() {
581+
// oauth2 applications
582+
m.Group("/oauth2", func() {
583+
m.Get("/{id}", user_setting.OAuth2ApplicationShow)
584+
m.Post("/{id}", web.Bind(forms.EditOAuth2ApplicationForm{}), user_setting.OAuthApplicationsEdit)
585+
m.Post("/{id}/regenerate_secret", user_setting.OAuthApplicationsRegenerateSecret)
586+
m.Post("", web.Bind(forms.EditOAuth2ApplicationForm{}), user_setting.OAuthApplicationsPost)
587+
m.Post("/{id}/delete", user_setting.DeleteOAuth2Application)
588+
m.Post("/{id}/revoke/{grantId}", user_setting.RevokeOAuth2Grant)
589+
}, oauth2Enabled)
590+
591+
// access token applications
592+
m.Combo("").Get(user_setting.Applications).
593+
Post(web.Bind(forms.NewAccessTokenForm{}), user_setting.ApplicationsPost)
594+
m.Post("/delete", user_setting.DeleteApplication)
577595
})
578-
m.Combo("/applications").Get(user_setting.Applications).
579-
Post(web.Bind(forms.NewAccessTokenForm{}), user_setting.ApplicationsPost)
580-
m.Post("/applications/delete", user_setting.DeleteApplication)
596+
581597
m.Combo("/keys").Get(user_setting.Keys).
582598
Post(web.Bind(forms.AddKeyForm{}), user_setting.KeysPost)
583599
m.Post("/keys/delete", user_setting.DeleteKey)
@@ -755,12 +771,7 @@ func registerRoutes(m *web.Route) {
755771
m.Post("/regenerate_secret", admin.ApplicationsRegenerateSecret)
756772
m.Post("/delete", admin.DeleteApplication)
757773
})
758-
}, func(ctx *context.Context) {
759-
if !setting.OAuth2.Enabled {
760-
ctx.Error(http.StatusForbidden)
761-
return
762-
}
763-
})
774+
}, oauth2Enabled)
764775

765776
m.Group("/actions", func() {
766777
m.Get("", admin.RedirectToDefaultSetting)
@@ -883,12 +894,7 @@ func registerRoutes(m *web.Route) {
883894
m.Post("/regenerate_secret", org.OAuthApplicationsRegenerateSecret)
884895
m.Post("/delete", org.DeleteOAuth2Application)
885896
})
886-
}, func(ctx *context.Context) {
887-
if !setting.OAuth2.Enabled {
888-
ctx.Error(http.StatusForbidden)
889-
return
890-
}
891-
})
897+
}, oauth2Enabled)
892898

893899
m.Group("/hooks", func() {
894900
m.Get("", org.Webhooks)

services/auth/oauth2.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ func grantAdditionalScopes(grantScopes string) string {
6868
// CheckOAuthAccessToken returns uid of user from oauth token
6969
// + non default openid scopes requested
7070
func CheckOAuthAccessToken(ctx context.Context, accessToken string) (int64, string) {
71+
if !setting.OAuth2.Enabled {
72+
return 0, ""
73+
}
7174
// JWT tokens require a "."
7275
if !strings.Contains(accessToken, ".") {
7376
return 0, ""

0 commit comments

Comments
 (0)