Skip to content

Commit 7a5a959

Browse files
committed
Remove unnecessary session on API context
1 parent dd1373c commit 7a5a959

File tree

5 files changed

+20
-42
lines changed

5 files changed

+20
-42
lines changed

modules/context/api.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package context
88
import (
99
"context"
1010
"fmt"
11-
"html"
1211
"net/http"
1312
"net/url"
1413
"strings"
@@ -20,8 +19,6 @@ import (
2019
"code.gitea.io/gitea/modules/setting"
2120
"code.gitea.io/gitea/modules/web/middleware"
2221
auth_service "code.gitea.io/gitea/services/auth"
23-
24-
"gitea.com/go-chi/session"
2522
)
2623

2724
// APIContext is a specific context for API service
@@ -242,17 +239,14 @@ func APIAuth(authMethod auth_service.Method) func(*APIContext) {
242239

243240
// APIContexter returns apicontext as middleware
244241
func APIContexter() func(http.Handler) http.Handler {
245-
csrfOpts := getCsrfOpts()
246-
247242
return func(next http.Handler) http.Handler {
248243
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
249244
locale := middleware.Locale(w, req)
250245
ctx := APIContext{
251246
Context: &Context{
252-
Resp: NewResponse(w),
253-
Data: map[string]interface{}{},
254-
Locale: locale,
255-
Session: session.GetSession(req),
247+
Resp: NewResponse(w),
248+
Data: map[string]interface{}{},
249+
Locale: locale,
256250
Repo: &Repository{
257251
PullRequest: &PullRequest{},
258252
},
@@ -262,7 +256,6 @@ func APIContexter() func(http.Handler) http.Handler {
262256
}
263257

264258
ctx.Req = WithAPIContext(WithContext(req, ctx.Context), &ctx)
265-
ctx.csrf = Csrfer(csrfOpts, ctx.Context)
266259

267260
// If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.
268261
if ctx.Req.Method == "POST" && strings.Contains(ctx.Req.Header.Get("Content-Type"), "multipart/form-data") {
@@ -274,7 +267,6 @@ func APIContexter() func(http.Handler) http.Handler {
274267

275268
ctx.Resp.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)
276269

277-
ctx.Data["CsrfToken"] = html.EscapeString(ctx.csrf.GetToken())
278270
ctx.Data["Context"] = &ctx
279271

280272
next.ServeHTTP(ctx.Resp, ctx.Req)

routers/api/v1/api.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,11 +594,9 @@ func buildAuthGroup() *auth.Group {
594594
}
595595

596596
// Routes registers all v1 APIs routes to web application.
597-
func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
597+
func Routes() *web.Route {
598598
m := web.NewRoute()
599599

600-
m.Use(sessioner)
601-
602600
m.Use(securityHeaders())
603601
if setting.CORSConfig.Enabled {
604602
m.Use(cors.Handler(cors.Options{

routers/init.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ import (
4848
"code.gitea.io/gitea/services/repository/archiver"
4949
"code.gitea.io/gitea/services/task"
5050
"code.gitea.io/gitea/services/webhook"
51-
52-
"gitea.com/go-chi/session"
5351
)
5452

5553
func mustInit(fn func() error) {
@@ -174,20 +172,8 @@ func NormalRoutes() *web.Route {
174172
r.Use(middle)
175173
}
176174

177-
sessioner := session.Sessioner(session.Options{
178-
Provider: setting.SessionConfig.Provider,
179-
ProviderConfig: setting.SessionConfig.ProviderConfig,
180-
CookieName: setting.SessionConfig.CookieName,
181-
CookiePath: setting.SessionConfig.CookiePath,
182-
Gclifetime: setting.SessionConfig.Gclifetime,
183-
Maxlifetime: setting.SessionConfig.Maxlifetime,
184-
Secure: setting.SessionConfig.Secure,
185-
SameSite: setting.SessionConfig.SameSite,
186-
Domain: setting.SessionConfig.Domain,
187-
})
188-
189-
r.Mount("/", web_routers.Routes(sessioner))
190-
r.Mount("/api/v1", apiv1.Routes(sessioner))
175+
r.Mount("/", web_routers.Routes())
176+
r.Mount("/api/v1", apiv1.Routes())
191177
r.Mount("/api/internal", private.Routes())
192178
if setting.Packages.Enabled {
193179
r.Mount("/api/packages", packages_router.Routes())

routers/web/web.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
_ "code.gitea.io/gitea/modules/session" // to registers all internal adapters
4747

4848
"gitea.com/go-chi/captcha"
49+
"gitea.com/go-chi/session"
4950
"github.com/NYTimes/gziphandler"
5051
"github.com/go-chi/chi/v5/middleware"
5152
"github.com/go-chi/cors"
@@ -85,7 +86,7 @@ func buildAuthGroup() *auth_service.Group {
8586
group := auth_service.NewGroup(
8687
&auth_service.OAuth2{}, // FIXME: this should be removed and only applied in download and oauth realted routers
8788
&auth_service.Basic{}, // FIXME: this should be removed and only applied in download and git/lfs routers
88-
auth_service.SharedSession,
89+
&auth_service.Session{},
8990
)
9091
if setting.Service.EnableReverseProxyAuth {
9192
group.Add(&auth_service.ReverseProxy{})
@@ -96,7 +97,7 @@ func buildAuthGroup() *auth_service.Group {
9697
}
9798

9899
// Routes returns all web routes
99-
func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
100+
func Routes() *web.Route {
100101
routes := web.NewRoute()
101102

102103
routes.Use(web.WrapWithPrefix(public.AssetsURLPathPrefix, public.AssetsHandlerFunc(&public.Options{
@@ -105,6 +106,17 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
105106
CorsHandler: CorsHandler(),
106107
}), "AssetsHandler"))
107108

109+
sessioner := session.Sessioner(session.Options{
110+
Provider: setting.SessionConfig.Provider,
111+
ProviderConfig: setting.SessionConfig.ProviderConfig,
112+
CookieName: setting.SessionConfig.CookieName,
113+
CookiePath: setting.SessionConfig.CookiePath,
114+
Gclifetime: setting.SessionConfig.Gclifetime,
115+
Maxlifetime: setting.SessionConfig.Maxlifetime,
116+
Secure: setting.SessionConfig.Secure,
117+
SameSite: setting.SessionConfig.SameSite,
118+
Domain: setting.SessionConfig.Domain,
119+
})
108120
routes.Use(sessioner)
109121

110122
routes.Use(Recovery())

services/auth/auth.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,6 @@ import (
2020
"code.gitea.io/gitea/modules/web/middleware"
2121
)
2222

23-
// The purpose of the following three function variables is to let the linter know that
24-
// those functions are not dead code and are actually being used
25-
var (
26-
_ = handleSignIn
27-
28-
// SharedSession the session auth should only be used by web, but now both web and API/v1
29-
// will use it. We can remove this after Web removed dependent API/v1
30-
SharedSession = &Session{}
31-
)
32-
3323
// Init should be called exactly once when the application starts to allow plugins
3424
// to allocate necessary resources
3525
func Init() {

0 commit comments

Comments
 (0)