Skip to content

Commit 8e1e339

Browse files
committed
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 go-gitea#18070 Signed-off-by: Andrew Thornton <[email protected]>
1 parent a17fce3 commit 8e1e339

File tree

3 files changed

+19
-28
lines changed

3 files changed

+19
-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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ 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+
"gitea.com/go-chi/session"
4344
)
4445

4546
// NewServices init new services
@@ -144,8 +145,20 @@ func NormalRoutes() *web.Route {
144145
r.Use(middle)
145146
}
146147

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

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)