Skip to content

Commit de3216e

Browse files
authored
Use common sessioner for API and web routes (#18114)
* Use common sessioner for API and web routes Since the regenerate session ID PR some users of the memory session provider have been reporting difficulties with getting API results. I am uncertain as to why this is happening - but I think that the sessioner being created twice may be a potential cause for this. Therefore this PR attempts to move this out to a common sessioner as it is in 1.16. Fix #18070 Signed-off-by: Andrew Thornton <[email protected]> * Update routers/init.go
1 parent 353d88a commit de3216e

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

routers/api/v1/api.go

Lines changed: 2 additions & 13 deletions
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,10 @@ 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(next 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)
564553
m.Use(securityHeaders())
565554
if setting.CORSConfig.Enabled {
566555
m.Use(cors.Handler(cors.Options{

routers/init.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import (
4040
pull_service "code.gitea.io/gitea/services/pull"
4141
"code.gitea.io/gitea/services/repository"
4242
"code.gitea.io/gitea/services/webhook"
43+
44+
"gitea.com/go-chi/session"
4345
)
4446

4547
// NewServices init new services
@@ -144,8 +146,20 @@ func NormalRoutes() *web.Route {
144146
r.Use(middle)
145147
}
146148

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

routers/web/web.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import (
3939
_ "code.gitea.io/gitea/modules/session"
4040

4141
"gitea.com/go-chi/captcha"
42-
"gitea.com/go-chi/session"
4342
"github.com/NYTimes/gziphandler"
4443
"github.com/go-chi/chi/middleware"
4544
"github.com/go-chi/cors"
@@ -71,7 +70,7 @@ func CorsHandler() func(next http.Handler) http.Handler {
7170
}
7271

7372
// Routes returns all web routes
74-
func Routes() *web.Route {
73+
func Routes(sessioner func(next http.Handler) http.Handler) *web.Route {
7574
routes := web.NewRoute()
7675

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

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

9584
routes.Use(Recovery())
9685

0 commit comments

Comments
 (0)