Skip to content

Commit 2c03745

Browse files
committed
DRAFT: try split web.go (registerWebRoutes)
a early draft to try split registerWebRoutes, it's too long now. maybe this change will be usefull for #1872 , #33951 ... Signed-off-by: a1012112796 <[email protected]>
1 parent fd7c364 commit 2c03745

File tree

9 files changed

+586
-488
lines changed

9 files changed

+586
-488
lines changed

routers/web/auth/auth.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"code.gitea.io/gitea/modules/util"
2727
"code.gitea.io/gitea/modules/web"
2828
"code.gitea.io/gitea/modules/web/middleware"
29+
"code.gitea.io/gitea/routers/web/shared"
2930
auth_service "code.gitea.io/gitea/services/auth"
3031
"code.gitea.io/gitea/services/auth/source/oauth2"
3132
"code.gitea.io/gitea/services/context"
@@ -864,3 +865,57 @@ func updateSession(ctx *context.Context, deletes []string, updates map[string]an
864865
}
865866
return nil
866867
}
868+
869+
// /user/* auth routes
870+
func ProvideUserAuthRouter(m *web.Router) {
871+
// "user/login" doesn't need signOut, then logged-in users can still access this route for redirection purposes by "/user/login?redirec_to=..."
872+
m.Get("/login", SignIn)
873+
874+
m.Group("", provideUserAuthRouterRequestSignOut(m), shared.ReqSignOut)
875+
}
876+
877+
func provideUserAuthRouterRequestSignOut(m *web.Router) func() {
878+
linkAccountEnabled := func(ctx *context.Context) {
879+
if !setting.Service.EnableOpenIDSignIn && !setting.Service.EnableOpenIDSignUp && !setting.OAuth2.Enabled {
880+
ctx.HTTPError(http.StatusForbidden)
881+
return
882+
}
883+
}
884+
885+
return func() {
886+
m.Post("/login", web.Bind(forms.SignInForm{}), SignInPost)
887+
m.Group("", func() {
888+
m.Combo("/login/openid").
889+
Get(SignInOpenID).
890+
Post(web.Bind(forms.SignInOpenIDForm{}), SignInOpenIDPost)
891+
}, shared.OpenIDSignInEnabled)
892+
m.Group("/openid", func() {
893+
m.Combo("/connect").
894+
Get(ConnectOpenID).
895+
Post(web.Bind(forms.ConnectOpenIDForm{}), ConnectOpenIDPost)
896+
m.Group("/register", func() {
897+
m.Combo("").
898+
Get(RegisterOpenID, shared.OpenIDSignUpEnabled).
899+
Post(web.Bind(forms.SignUpOpenIDForm{}), RegisterOpenIDPost)
900+
}, shared.OpenIDSignUpEnabled)
901+
}, shared.OpenIDSignInEnabled)
902+
m.Get("/sign_up", SignUp)
903+
m.Post("/sign_up", web.Bind(forms.RegisterForm{}), SignUpPost)
904+
m.Get("/link_account", linkAccountEnabled, LinkAccount)
905+
m.Post("/link_account_signin", linkAccountEnabled, web.Bind(forms.SignInForm{}), LinkAccountPostSignIn)
906+
m.Post("/link_account_signup", linkAccountEnabled, web.Bind(forms.RegisterForm{}), LinkAccountPostRegister)
907+
m.Group("/two_factor", func() {
908+
m.Get("", TwoFactor)
909+
m.Post("", web.Bind(forms.TwoFactorAuthForm{}), TwoFactorPost)
910+
m.Get("/scratch", TwoFactorScratch)
911+
m.Post("/scratch", web.Bind(forms.TwoFactorScratchAuthForm{}), TwoFactorScratchPost)
912+
})
913+
m.Group("/webauthn", func() {
914+
m.Get("", WebAuthn)
915+
m.Get("/passkey/assertion", WebAuthnPasskeyAssertion)
916+
m.Post("/passkey/login", WebAuthnPasskeyLogin)
917+
m.Get("/assertion", WebAuthnLoginAssertion)
918+
m.Post("/assertion", WebAuthnLoginAssertionPost)
919+
})
920+
}
921+
}

routers/web/explore/explore.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package explore
5+
6+
import (
7+
"code.gitea.io/gitea/models/unit"
8+
"code.gitea.io/gitea/modules/setting"
9+
"code.gitea.io/gitea/modules/web"
10+
"code.gitea.io/gitea/routers/web/shared"
11+
"code.gitea.io/gitea/services/context"
12+
)
13+
14+
// /explore/* routes
15+
func ProvideExploreRoutes(m *web.Router) func() {
16+
return func() {
17+
m.Get("", func(ctx *context.Context) {
18+
ctx.Redirect(setting.AppSubURL + "/explore/repos")
19+
})
20+
m.Get("/repos", Repos)
21+
m.Get("/repos/sitemap-{idx}.xml", shared.SitemapEnabled, Repos)
22+
m.Get("/users", Users)
23+
m.Get("/users/sitemap-{idx}.xml", shared.SitemapEnabled, Users)
24+
m.Get("/organizations", Organizations)
25+
m.Get("/code", func(ctx *context.Context) {
26+
if unit.TypeCode.UnitGlobalDisabled() {
27+
ctx.NotFound(nil)
28+
return
29+
}
30+
}, Code)
31+
m.Get("/topics/search", TopicSearch)
32+
}
33+
}

routers/web/githttp.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package web
66
import (
77
"code.gitea.io/gitea/modules/web"
88
"code.gitea.io/gitea/routers/web/repo"
9+
"code.gitea.io/gitea/routers/web/shared"
910
"code.gitea.io/gitea/services/context"
1011
)
1112

@@ -22,5 +23,5 @@ func addOwnerRepoGitHTTPRouters(m *web.Router) {
2223
m.Methods("GET,OPTIONS", "/objects/{head:[0-9a-f]{2}}/{hash:[0-9a-f]{38,62}}", repo.GetLooseObject)
2324
m.Methods("GET,OPTIONS", "/objects/pack/pack-{file:[0-9a-f]{40,64}}.pack", repo.GetPackFile)
2425
m.Methods("GET,OPTIONS", "/objects/pack/pack-{file:[0-9a-f]{40,64}}.idx", repo.GetIdxFile)
25-
}, optSignInIgnoreCsrf, repo.HTTPGitEnabledHandler, repo.CorsHandler(), context.UserAssignmentWeb())
26+
}, shared.OptSignInIgnoreCsrf, repo.HTTPGitEnabledHandler, repo.CorsHandler(), context.UserAssignmentWeb())
2627
}

routers/web/repo/setting/secrets.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010
user_model "code.gitea.io/gitea/models/user"
1111
"code.gitea.io/gitea/modules/setting"
1212
"code.gitea.io/gitea/modules/templates"
13+
"code.gitea.io/gitea/modules/web"
1314
shared "code.gitea.io/gitea/routers/web/shared/secrets"
1415
shared_user "code.gitea.io/gitea/routers/web/shared/user"
1516
"code.gitea.io/gitea/services/context"
17+
"code.gitea.io/gitea/services/forms"
1618
)
1719

1820
const (
@@ -127,3 +129,11 @@ func SecretsDelete(ctx *context.Context) {
127129
sCtx.RedirectLink,
128130
)
129131
}
132+
133+
func AddSettingsSecretsRoutes(m *web.Router) {
134+
m.Group("/secrets", func() {
135+
m.Get("", Secrets)
136+
m.Post("", web.Bind(forms.AddSecretForm{}), SecretsPost)
137+
m.Post("/delete", SecretsDelete)
138+
})
139+
}

routers/web/repo/setting/setting.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,3 +1068,32 @@ func handleSettingRemoteAddrError(ctx *context.Context, err error, form *forms.R
10681068
}
10691069
ctx.RenderWithErr(ctx.Tr("repo.mirror_address_url_invalid"), tplSettingsOptions, form)
10701070
}
1071+
1072+
func AddWebhookAddRoutes(m *web.Router) {
1073+
m.Get("/{type}/new", WebhooksNew)
1074+
m.Post("/gitea/new", web.Bind(forms.NewWebhookForm{}), GiteaHooksNewPost)
1075+
m.Post("/gogs/new", web.Bind(forms.NewGogshookForm{}), GogsHooksNewPost)
1076+
m.Post("/slack/new", web.Bind(forms.NewSlackHookForm{}), SlackHooksNewPost)
1077+
m.Post("/discord/new", web.Bind(forms.NewDiscordHookForm{}), DiscordHooksNewPost)
1078+
m.Post("/dingtalk/new", web.Bind(forms.NewDingtalkHookForm{}), DingtalkHooksNewPost)
1079+
m.Post("/telegram/new", web.Bind(forms.NewTelegramHookForm{}), TelegramHooksNewPost)
1080+
m.Post("/matrix/new", web.Bind(forms.NewMatrixHookForm{}), MatrixHooksNewPost)
1081+
m.Post("/msteams/new", web.Bind(forms.NewMSTeamsHookForm{}), MSTeamsHooksNewPost)
1082+
m.Post("/feishu/new", web.Bind(forms.NewFeishuHookForm{}), FeishuHooksNewPost)
1083+
m.Post("/wechatwork/new", web.Bind(forms.NewWechatWorkHookForm{}), WechatworkHooksNewPost)
1084+
m.Post("/packagist/new", web.Bind(forms.NewPackagistHookForm{}), PackagistHooksNewPost)
1085+
}
1086+
1087+
func AddWebhookEditRoutes(m *web.Router) {
1088+
m.Post("/gitea/{id}", web.Bind(forms.NewWebhookForm{}), GiteaHooksEditPost)
1089+
m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), GogsHooksEditPost)
1090+
m.Post("/slack/{id}", web.Bind(forms.NewSlackHookForm{}), SlackHooksEditPost)
1091+
m.Post("/discord/{id}", web.Bind(forms.NewDiscordHookForm{}), DiscordHooksEditPost)
1092+
m.Post("/dingtalk/{id}", web.Bind(forms.NewDingtalkHookForm{}), DingtalkHooksEditPost)
1093+
m.Post("/telegram/{id}", web.Bind(forms.NewTelegramHookForm{}), TelegramHooksEditPost)
1094+
m.Post("/matrix/{id}", web.Bind(forms.NewMatrixHookForm{}), MatrixHooksEditPost)
1095+
m.Post("/msteams/{id}", web.Bind(forms.NewMSTeamsHookForm{}), MSTeamsHooksEditPost)
1096+
m.Post("/feishu/{id}", web.Bind(forms.NewFeishuHookForm{}), FeishuHooksEditPost)
1097+
m.Post("/wechatwork/{id}", web.Bind(forms.NewWechatWorkHookForm{}), WechatworkHooksEditPost)
1098+
m.Post("/packagist/{id}", web.Bind(forms.NewPackagistHookForm{}), PackagistHooksEditPost)
1099+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package actions
5+
6+
import (
7+
"code.gitea.io/gitea/modules/web"
8+
"code.gitea.io/gitea/services/forms"
9+
)
10+
11+
// AddSettingsRunnersRoutes adds routes for settings runners
12+
func AddSettingsRunnersRoutes(m *web.Router) {
13+
m.Group("/runners", func() {
14+
m.Get("", Runners)
15+
m.Combo("/{runnerid}").Get(RunnersEdit).
16+
Post(web.Bind(forms.EditRunnerForm{}), RunnersEditPost)
17+
m.Post("/{runnerid}/delete", RunnerDeletePost)
18+
m.Post("/reset_registration_token", ResetRunnerRegistrationToken)
19+
})
20+
}
21+
22+
func AddSettingsVariablesRoutes(m *web.Router) {
23+
m.Group("/variables", func() {
24+
m.Get("", Variables)
25+
m.Post("/new", web.Bind(forms.EditVariableForm{}), VariableCreate)
26+
m.Post("/{variable_id}/edit", web.Bind(forms.EditVariableForm{}), VariableUpdate)
27+
m.Post("/{variable_id}/delete", VariableDelete)
28+
})
29+
}

routers/web/shared/utils.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package shared
5+
6+
import (
7+
"net/http"
8+
"strings"
9+
10+
"code.gitea.io/gitea/modules/log"
11+
"code.gitea.io/gitea/modules/setting"
12+
"code.gitea.io/gitea/modules/web/middleware"
13+
"code.gitea.io/gitea/routers/common"
14+
"code.gitea.io/gitea/services/context"
15+
)
16+
17+
func SitemapEnabled(ctx *context.Context) {
18+
if !setting.Other.EnableSitemap {
19+
ctx.HTTPError(http.StatusNotFound)
20+
return
21+
}
22+
}
23+
24+
// verifyAuthWithOptions checks authentication according to options
25+
func verifyAuthWithOptions(options *common.VerifyOptions) func(ctx *context.Context) {
26+
return func(ctx *context.Context) {
27+
// Check prohibit login users.
28+
if ctx.IsSigned {
29+
if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm {
30+
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
31+
ctx.HTML(http.StatusOK, "user/auth/activate")
32+
return
33+
}
34+
if !ctx.Doer.IsActive || ctx.Doer.ProhibitLogin {
35+
log.Info("Failed authentication attempt for %s from %s", ctx.Doer.Name, ctx.RemoteAddr())
36+
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
37+
ctx.HTML(http.StatusOK, "user/auth/prohibit_login")
38+
return
39+
}
40+
41+
if ctx.Doer.MustChangePassword {
42+
if ctx.Req.URL.Path != "/user/settings/change_password" {
43+
if strings.HasPrefix(ctx.Req.UserAgent(), "git") {
44+
ctx.HTTPError(http.StatusUnauthorized, ctx.Locale.TrString("auth.must_change_password"))
45+
return
46+
}
47+
ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
48+
ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/change_password"
49+
if ctx.Req.URL.Path != "/user/events" {
50+
middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
51+
}
52+
ctx.Redirect(setting.AppSubURL + "/user/settings/change_password")
53+
return
54+
}
55+
} else if ctx.Req.URL.Path == "/user/settings/change_password" {
56+
// make sure that the form cannot be accessed by users who don't need this
57+
ctx.Redirect(setting.AppSubURL + "/")
58+
return
59+
}
60+
}
61+
62+
// Redirect to dashboard (or alternate location) if user tries to visit any non-login page.
63+
if options.SignOutRequired && ctx.IsSigned && ctx.Req.URL.RequestURI() != "/" {
64+
ctx.RedirectToCurrentSite(ctx.FormString("redirect_to"))
65+
return
66+
}
67+
68+
if !options.SignOutRequired && !options.DisableCSRF && ctx.Req.Method == http.MethodPost {
69+
ctx.Csrf.Validate(ctx)
70+
if ctx.Written() {
71+
return
72+
}
73+
}
74+
75+
if options.SignInRequired {
76+
if !ctx.IsSigned {
77+
if ctx.Req.URL.Path != "/user/events" {
78+
middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
79+
}
80+
ctx.Redirect(setting.AppSubURL + "/user/login")
81+
return
82+
} else if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm {
83+
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
84+
ctx.HTML(http.StatusOK, "user/auth/activate")
85+
return
86+
}
87+
}
88+
89+
// Redirect to log in page if auto-signin info is provided and has not signed in.
90+
if !options.SignOutRequired && !ctx.IsSigned &&
91+
ctx.GetSiteCookie(setting.CookieRememberName) != "" {
92+
if ctx.Req.URL.Path != "/user/events" {
93+
middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
94+
}
95+
ctx.Redirect(setting.AppSubURL + "/user/login")
96+
return
97+
}
98+
99+
if options.AdminRequired {
100+
if !ctx.Doer.IsAdmin {
101+
ctx.HTTPError(http.StatusForbidden)
102+
return
103+
}
104+
ctx.Data["PageIsAdmin"] = true
105+
}
106+
}
107+
}
108+
109+
var (
110+
OptSignInIgnoreCsrf = verifyAuthWithOptions(&common.VerifyOptions{DisableCSRF: true})
111+
112+
// required to be signed in or signed out
113+
ReqSignIn = verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: true})
114+
ReqSignOut = verifyAuthWithOptions(&common.VerifyOptions{SignOutRequired: true})
115+
// optional sign in (if signed in, use the user as doer, if not, no doer)
116+
OptSignIn = verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: setting.Service.RequireSignInViewStrict})
117+
OptExploreSignIn = verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: setting.Service.RequireSignInViewStrict || setting.Service.Explore.RequireSigninView})
118+
119+
AdminReq = verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: true, AdminRequired: true})
120+
)
121+
122+
func OpenIDSignUpEnabled(ctx *context.Context) {
123+
if !setting.Service.EnableOpenIDSignUp {
124+
ctx.HTTPError(http.StatusForbidden)
125+
return
126+
}
127+
}
128+
129+
func OpenIDSignInEnabled(ctx *context.Context) {
130+
if !setting.Service.EnableOpenIDSignIn {
131+
ctx.HTTPError(http.StatusForbidden)
132+
return
133+
}
134+
}
135+
136+
func LinkAccountEnabled(ctx *context.Context) {
137+
if !setting.Service.EnableOpenIDSignIn && !setting.Service.EnableOpenIDSignUp && !setting.OAuth2.Enabled {
138+
ctx.HTTPError(http.StatusForbidden)
139+
return
140+
}
141+
}
142+
143+
func Oauth2Enabled(ctx *context.Context) {
144+
if !setting.OAuth2.Enabled {
145+
ctx.HTTPError(http.StatusForbidden)
146+
return
147+
}
148+
}
149+
150+
func PackagesEnabled(ctx *context.Context) {
151+
if !setting.Packages.Enabled {
152+
ctx.HTTPError(http.StatusForbidden)
153+
return
154+
}
155+
}
156+
157+
// WebhooksEnabled requires webhooks to be enabled by admin.
158+
func WebhooksEnabled(ctx *context.Context) {
159+
if setting.DisableWebhooks {
160+
ctx.HTTPError(http.StatusForbidden)
161+
return
162+
}
163+
}

0 commit comments

Comments
 (0)