Skip to content

Commit a334a95

Browse files
authored
Use common sessioner for API and Web (#17027)
* Use common sessioner for API and Web Instead of creating separate sessioner and doubly initialising the provider just use the same sessioner for the API and Web routes. Signed-off-by: Andrew Thornton <art27@cantab.net>
1 parent aac7f68 commit a334a95

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

routers/api/v1/api.go

+3-13
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ import (
8787
"code.gitea.io/gitea/services/forms"
8888

8989
"gitea.com/go-chi/binding"
90-
"gitea.com/go-chi/session"
9190
"github.com/go-chi/cors"
9291
)
9392

@@ -547,20 +546,11 @@ func bind(obj interface{}) http.HandlerFunc {
547546
}
548547

549548
// Routes registers all v1 APIs routes to web application.
550-
func Routes() *web.Route {
549+
func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
551550
var m = web.NewRoute()
552551

553-
m.Use(session.Sessioner(session.Options{
554-
Provider: setting.SessionConfig.Provider,
555-
ProviderConfig: setting.SessionConfig.ProviderConfig,
556-
CookieName: setting.SessionConfig.CookieName,
557-
CookiePath: setting.SessionConfig.CookiePath,
558-
Gclifetime: setting.SessionConfig.Gclifetime,
559-
Maxlifetime: setting.SessionConfig.Maxlifetime,
560-
Secure: setting.SessionConfig.Secure,
561-
SameSite: setting.SessionConfig.SameSite,
562-
Domain: setting.SessionConfig.Domain,
563-
}))
552+
m.Use(sessioner)
553+
564554
m.Use(securityHeaders())
565555
if setting.CORSConfig.Enabled {
566556
m.Use(cors.Handler(cors.Options{

routers/init.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ import (
4141
pull_service "code.gitea.io/gitea/services/pull"
4242
"code.gitea.io/gitea/services/repository"
4343
"code.gitea.io/gitea/services/webhook"
44+
45+
"gitea.com/go-chi/session"
4446
)
4547

4648
// NewServices init new services
@@ -145,8 +147,20 @@ func NormalRoutes() *web.Route {
145147
r.Use(middle)
146148
}
147149

148-
r.Mount("/", web_routers.Routes())
149-
r.Mount("/api/v1", apiv1.Routes())
150+
sessioner := session.Sessioner(session.Options{
151+
Provider: setting.SessionConfig.Provider,
152+
ProviderConfig: setting.SessionConfig.ProviderConfig,
153+
CookieName: setting.SessionConfig.CookieName,
154+
CookiePath: setting.SessionConfig.CookiePath,
155+
Gclifetime: setting.SessionConfig.Gclifetime,
156+
Maxlifetime: setting.SessionConfig.Maxlifetime,
157+
Secure: setting.SessionConfig.Secure,
158+
SameSite: setting.SessionConfig.SameSite,
159+
Domain: setting.SessionConfig.Domain,
160+
})
161+
162+
r.Mount("/", web_routers.Routes(sessioner))
163+
r.Mount("/api/v1", apiv1.Routes(sessioner))
150164
r.Mount("/api/internal", private.Routes())
151165
return r
152166
}

routers/web/web.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import (
4040
_ "code.gitea.io/gitea/modules/session"
4141

4242
"gitea.com/go-chi/captcha"
43-
"gitea.com/go-chi/session"
4443
"github.com/NYTimes/gziphandler"
4544
"github.com/go-chi/chi/middleware"
4645
"github.com/go-chi/cors"
@@ -72,7 +71,7 @@ func CorsHandler() func(next http.Handler) http.Handler {
7271
}
7372

7473
// Routes returns all web routes
75-
func Routes() *web.Route {
74+
func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
7675
routes := web.NewRoute()
7776

7877
routes.Use(public.AssetsHandler(&public.Options{
@@ -81,17 +80,7 @@ func Routes() *web.Route {
8180
CorsHandler: CorsHandler(),
8281
}))
8382

84-
routes.Use(session.Sessioner(session.Options{
85-
Provider: setting.SessionConfig.Provider,
86-
ProviderConfig: setting.SessionConfig.ProviderConfig,
87-
CookieName: setting.SessionConfig.CookieName,
88-
CookiePath: setting.SessionConfig.CookiePath,
89-
Gclifetime: setting.SessionConfig.Gclifetime,
90-
Maxlifetime: setting.SessionConfig.Maxlifetime,
91-
Secure: setting.SessionConfig.Secure,
92-
SameSite: setting.SessionConfig.SameSite,
93-
Domain: setting.SessionConfig.Domain,
94-
}))
83+
routes.Use(sessioner)
9584

9685
routes.Use(Recovery())
9786

0 commit comments

Comments
 (0)