Skip to content

Commit

Permalink
Fix session bug when introduce chi (#14287)
Browse files Browse the repository at this point in the history
* Update go-chi session
  • Loading branch information
lunny authored Jan 8, 2021
1 parent 4614060 commit ce43d38
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.14
require (
code.gitea.io/gitea-vet v0.2.1
code.gitea.io/sdk/gitea v0.13.1
gitea.com/go-chi/session v0.0.0-20201218134809-7209fa084f27
gitea.com/go-chi/session v0.0.0-20210108030337-0cb48c5ba8ee
gitea.com/lunny/levelqueue v0.3.0
gitea.com/macaron/binding v0.0.0-20190822013154-a5f53841ed2b
gitea.com/macaron/cache v0.0.0-20200924044943-905232fba10b
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ code.gitea.io/gitea-vet v0.2.1/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFj
code.gitea.io/sdk/gitea v0.13.1 h1:Y7bpH2iO6Q0KhhMJfjP/LZ0AmiYITeRQlCD8b0oYqhk=
code.gitea.io/sdk/gitea v0.13.1/go.mod h1:z3uwDV/b9Ls47NGukYM9XhnHtqPh/J+t40lsUrR6JDY=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gitea.com/go-chi/session v0.0.0-20201218134809-7209fa084f27 h1:cdb1OTNXGLwQ55gg+9tIPWufdsnrHWcIq8Qs+j/E8JU=
gitea.com/go-chi/session v0.0.0-20201218134809-7209fa084f27/go.mod h1:Ozg8IchVNb/Udg+ui39iHRYqVHSvf3C99ixdpLR8Vu0=
gitea.com/go-chi/session v0.0.0-20210108030337-0cb48c5ba8ee h1:9U6HuKUBt/cGK6T/64dEuz0r7Yp97WAAEJvXHDlY3ws=
gitea.com/go-chi/session v0.0.0-20210108030337-0cb48c5ba8ee/go.mod h1:Ozg8IchVNb/Udg+ui39iHRYqVHSvf3C99ixdpLR8Vu0=
gitea.com/lunny/levelqueue v0.3.0 h1:MHn1GuSZkxvVEDMyAPqlc7A3cOW+q8RcGhRgH/xtm6I=
gitea.com/lunny/levelqueue v0.3.0/go.mod h1:HBqmLbz56JWpfEGG0prskAV97ATNRoj5LDmPicD22hU=
gitea.com/lunny/log v0.0.0-20190322053110-01b5df579c4e h1:r1en/D7xJmcY24VkHkjkcJFa+7ZWubVWPBrvsHkmHxk=
Expand Down
16 changes: 14 additions & 2 deletions routers/routes/chi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
13 changes: 9 additions & 4 deletions routers/routes/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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{
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions vendor/gitea.com/go-chi/session/session.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ code.gitea.io/gitea-vet/checks
# code.gitea.io/sdk/gitea v0.13.1
## explicit
code.gitea.io/sdk/gitea
# gitea.com/go-chi/session v0.0.0-20201218134809-7209fa084f27
# gitea.com/go-chi/session v0.0.0-20210108030337-0cb48c5ba8ee
## explicit
gitea.com/go-chi/session
gitea.com/go-chi/session/couchbase
Expand Down

0 comments on commit ce43d38

Please sign in to comment.