Skip to content

Commit ee7116a

Browse files
committed
Fix test and refactor web routers
1 parent 4597aeb commit ee7116a

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

routers/web/web.go

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,13 @@ func registerRoutes(m *web.Router) {
325325
}
326326
}
327327

328+
oauth2Enabled := func(ctx *context.Context) {
329+
if !setting.OAuth2.Enabled {
330+
ctx.Error(http.StatusForbidden)
331+
return
332+
}
333+
}
334+
328335
reqMilestonesDashboardPageEnabled := func(ctx *context.Context) {
329336
if !setting.Service.ShowMilestonesDashboardPage {
330337
ctx.Error(http.StatusForbidden)
@@ -546,19 +553,19 @@ func registerRoutes(m *web.Router) {
546553

547554
m.Any("/user/events", routing.MarkLongPolling, events.Events)
548555

549-
if setting.OAuth2.Enabled {
550-
m.Group("/login/oauth", func() {
556+
m.Group("/login/oauth", func() {
557+
m.Group("", func() {
551558
m.Get("/authorize", web.Bind(forms.AuthorizationForm{}), auth.AuthorizeOAuth)
552559
m.Post("/grant", web.Bind(forms.GrantApplicationForm{}), auth.GrantApplicationOAuth)
553560
// TODO manage redirection
554561
m.Post("/authorize", web.Bind(forms.AuthorizationForm{}), auth.AuthorizeOAuth)
555562
}, ignSignInAndCsrf, reqSignIn)
556563

557-
m.Methods("GET, OPTIONS", "/login/oauth/userinfo", optionsCorsHandler(), ignSignInAndCsrf, auth.InfoOAuth)
558-
m.Methods("POST, OPTIONS", "/login/oauth/access_token", optionsCorsHandler(), web.Bind(forms.AccessTokenForm{}), ignSignInAndCsrf, auth.AccessTokenOAuth)
559-
m.Methods("GET, OPTIONS", "/login/oauth/keys", optionsCorsHandler(), ignSignInAndCsrf, auth.OIDCKeys)
560-
m.Methods("POST, OPTIONS", "/login/oauth/introspect", optionsCorsHandler(), web.Bind(forms.IntrospectTokenForm{}), ignSignInAndCsrf, auth.IntrospectOAuth)
561-
}
564+
m.Methods("GET, OPTIONS", "/userinfo", optionsCorsHandler(), ignSignInAndCsrf, auth.InfoOAuth)
565+
m.Methods("POST, OPTIONS", "/access_token", optionsCorsHandler(), web.Bind(forms.AccessTokenForm{}), ignSignInAndCsrf, auth.AccessTokenOAuth)
566+
m.Methods("GET, OPTIONS", "/keys", optionsCorsHandler(), ignSignInAndCsrf, auth.OIDCKeys)
567+
m.Methods("POST, OPTIONS", "/introspect", optionsCorsHandler(), web.Bind(forms.IntrospectTokenForm{}), ignSignInAndCsrf, auth.IntrospectOAuth)
568+
}, oauth2Enabled)
562569

563570
m.Group("/user/settings", func() {
564571
m.Get("", user_setting.Profile)
@@ -600,20 +607,23 @@ func registerRoutes(m *web.Router) {
600607
m.Post("/account_link", linkAccountEnabled, security.DeleteAccountLink)
601608
})
602609

603-
if setting.OAuth2.Enabled {
604-
m.Group("/applications/oauth2", func() {
610+
m.Group("/applications", func() {
611+
// oauth2 applications
612+
m.Group("/oauth2", func() {
605613
m.Get("/{id}", user_setting.OAuth2ApplicationShow)
606614
m.Post("/{id}", web.Bind(forms.EditOAuth2ApplicationForm{}), user_setting.OAuthApplicationsEdit)
607615
m.Post("/{id}/regenerate_secret", user_setting.OAuthApplicationsRegenerateSecret)
608616
m.Post("", web.Bind(forms.EditOAuth2ApplicationForm{}), user_setting.OAuthApplicationsPost)
609617
m.Post("/{id}/delete", user_setting.DeleteOAuth2Application)
610618
m.Post("/{id}/revoke/{grantId}", user_setting.RevokeOAuth2Grant)
611-
})
612-
m.Combo("/applications").Get(user_setting.Applications).
619+
}, oauth2Enabled)
620+
621+
// access token applications
622+
m.Combo("").Get(user_setting.Applications).
613623
Post(web.Bind(forms.NewAccessTokenForm{}), user_setting.ApplicationsPost)
614-
}
624+
m.Post("/delete", user_setting.DeleteApplication)
625+
})
615626

616-
m.Post("/applications/delete", user_setting.DeleteApplication)
617627
m.Combo("/keys").Get(user_setting.Keys).
618628
Post(web.Bind(forms.AddKeyForm{}), user_setting.KeysPost)
619629
m.Post("/keys/delete", user_setting.DeleteKey)
@@ -779,17 +789,15 @@ func registerRoutes(m *web.Router) {
779789
m.Post("/empty", admin.EmptyNotices)
780790
})
781791

782-
if setting.OAuth2.Enabled {
783-
m.Group("/applications", func() {
784-
m.Get("", admin.Applications)
785-
m.Post("/oauth2", web.Bind(forms.EditOAuth2ApplicationForm{}), admin.ApplicationsPost)
786-
m.Group("/oauth2/{id}", func() {
787-
m.Combo("").Get(admin.EditApplication).Post(web.Bind(forms.EditOAuth2ApplicationForm{}), admin.EditApplicationPost)
788-
m.Post("/regenerate_secret", admin.ApplicationsRegenerateSecret)
789-
m.Post("/delete", admin.DeleteApplication)
790-
})
792+
m.Group("/applications", func() {
793+
m.Get("", admin.Applications)
794+
m.Post("/oauth2", web.Bind(forms.EditOAuth2ApplicationForm{}), admin.ApplicationsPost)
795+
m.Group("/oauth2/{id}", func() {
796+
m.Combo("").Get(admin.EditApplication).Post(web.Bind(forms.EditOAuth2ApplicationForm{}), admin.EditApplicationPost)
797+
m.Post("/regenerate_secret", admin.ApplicationsRegenerateSecret)
798+
m.Post("/delete", admin.DeleteApplication)
791799
})
792-
}
800+
}, oauth2Enabled)
793801

794802
m.Group("/actions", func() {
795803
m.Get("", admin.RedirectToDefaultSetting)
@@ -913,12 +921,7 @@ func registerRoutes(m *web.Router) {
913921
m.Post("/regenerate_secret", org.OAuthApplicationsRegenerateSecret)
914922
m.Post("/delete", org.DeleteOAuth2Application)
915923
})
916-
}, func(ctx *context.Context) {
917-
if !setting.OAuth2.Enabled {
918-
ctx.Error(http.StatusForbidden)
919-
return
920-
}
921-
})
924+
}, oauth2Enabled)
922925

923926
m.Group("/hooks", func() {
924927
m.Get("", org.Webhooks)

tests/integration/oauth_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,9 @@ func TestOAuthIntrospection(t *testing.T) {
483483
func TestGitOpWithOAuthDisabled(t *testing.T) {
484484
defer tests.PrepareTestEnv(t)()
485485

486-
setting.OAuth2.Enabled = true
486+
setting.OAuth2.Enabled = false
487487
defer func() {
488-
setting.OAuth2.Enabled = false
488+
setting.OAuth2.Enabled = true
489489
}()
490490

491491
onGiteaRun(t, func(t *testing.T, u *url.URL) {

0 commit comments

Comments
 (0)