diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-01-08 20:15:06 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-08 14:15:06 +0200 |
commit | ce43d38b4ffa40255cc8f859c5b31f59351f827c (patch) | |
tree | d309312f735491278abe31493e70370259d11002 /routers/routes | |
parent | 461406070c1fa8015d6dba17ae32bb43f78db3fc (diff) | |
download | gitea-ce43d38b4ffa40255cc8f859c5b31f59351f827c.tar.gz gitea-ce43d38b4ffa40255cc8f859c5b31f59351f827c.zip |
Fix session bug when introduce chi (#14287)
* Update go-chi session
Diffstat (limited to 'routers/routes')
-rw-r--r-- | routers/routes/chi.go | 16 | ||||
-rw-r--r-- | routers/routes/recovery.go | 13 |
2 files changed, 23 insertions, 6 deletions
diff --git a/routers/routes/chi.go b/routers/routes/chi.go index c0ac88957e..6e609fc2f8 100644 --- a/routers/routes/chi.go +++ b/routers/routes/chi.go @@ -176,6 +176,10 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor } } +var ( + sessionManager *session.Manager +) + // NewChi creates a chi Router func NewChi() chi.Router { c := chi.NewRouter() @@ -185,7 +189,8 @@ func NewChi() chi.Router { c.Use(LoggerHandler(setting.RouterLogLevel)) } } - c.Use(session.Sessioner(session.Options{ + + var opt = session.Options{ Provider: setting.SessionConfig.Provider, ProviderConfig: setting.SessionConfig.ProviderConfig, CookieName: setting.SessionConfig.CookieName, @@ -194,7 +199,14 @@ func NewChi() chi.Router { Maxlifetime: setting.SessionConfig.Maxlifetime, Secure: setting.SessionConfig.Secure, Domain: setting.SessionConfig.Domain, - })) + } + opt = session.PrepareOptions([]session.Options{opt}) + + var err error + sessionManager, err = session.NewManager(opt.Provider, opt) + if err != nil { + panic(err) + } c.Use(Recovery()) if setting.EnableAccessLog { diff --git a/routers/routes/recovery.go b/routers/routes/recovery.go index cf4b1a8d84..f392d1d553 100644 --- a/routers/routes/recovery.go +++ b/routers/routes/recovery.go @@ -14,7 +14,6 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/templates" - "gitea.com/go-chi/session" "github.com/unrolled/render" ) @@ -64,7 +63,13 @@ func Recovery() func(next http.Handler) http.Handler { log.Error("%v", combinedErr) lc := middlewares.Locale(w, req) - sess := session.GetSession(req) + + // TODO: this should be replaced by real session after macaron removed totally + sessionStore, err := sessionManager.Start(w, req) + if err != nil { + // Just invoke the above recover catch + panic("session(start): " + err.Error()) + } var store = dataStore{ Data: templates.Vars{ @@ -75,7 +80,7 @@ func Recovery() func(next http.Handler) http.Handler { } // Get user from session if logged in. - user, _ := sso.SignedInUser(req, w, &store, sess) + user, _ := sso.SignedInUser(req, w, &store, sessionStore) if user != nil { store.Data["IsSigned"] = true store.Data["SignedUser"] = user @@ -92,7 +97,7 @@ func Recovery() func(next http.Handler) http.Handler { if setting.RunMode != "prod" { store.Data["ErrMsg"] = combinedErr } - err := rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store.Data)) + err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store.Data)) if err != nil { log.Error("%v", err) } |