Use filesystem session store (#240)

This commit is contained in:
Thomas Miceli
2024-04-03 01:48:31 +02:00
parent ef004675a5
commit a97d9cdbf4
6 changed files with 48 additions and 10 deletions

View File

@ -35,10 +35,11 @@ import (
)
var (
dev bool
store *sessions.CookieStore
re = regexp.MustCompile("[^a-z0-9]+")
fm = template.FuncMap{
dev bool
flashStore *sessions.CookieStore // session store for flash messages
userStore *sessions.FilesystemStore // session store for user sessions
re = regexp.MustCompile("[^a-z0-9]+")
fm = template.FuncMap{
"split": strings.Split,
"indexByte": strings.IndexByte,
"toInt": func(i string) int {
@ -160,8 +161,13 @@ type Server struct {
func NewServer(isDev bool) *Server {
dev = isDev
store = sessions.NewCookieStore([]byte("opengist"))
gothic.Store = store
flashStore = sessions.NewCookieStore([]byte("opengist"))
userStore = sessions.NewFilesystemStore(path.Join(config.GetHomeDir(), "sessions"),
utils.ReadKey(path.Join(config.GetHomeDir(), "sessions", "session-auth.key")),
utils.ReadKey(path.Join(config.GetHomeDir(), "sessions", "session-encrypt.key")),
)
userStore.MaxLength(10 * 1024)
gothic.Store = userStore
e := echo.New()
e.HideBanner = true

View File

@ -142,6 +142,9 @@ func setup(t *testing.T) {
homePath := config.GetHomeDir()
log.Info().Msg("Data directory: " + homePath)
err = os.MkdirAll(filepath.Join(homePath, "sessions"), 0755)
require.NoError(t, err, "Could not create sessions directory")
err = os.MkdirAll(filepath.Join(homePath, "tmp", "repos"), 0755)
require.NoError(t, err, "Could not create tmp repos directory")

View File

@ -68,7 +68,7 @@ func getUserLogged(ctx echo.Context) *db.User {
}
func setErrorFlashes(ctx echo.Context) {
sess, _ := store.Get(ctx.Request(), "flash")
sess, _ := flashStore.Get(ctx.Request(), "flash")
setData(ctx, "flashErrors", sess.Flashes("error"))
setData(ctx, "flashSuccess", sess.Flashes("success"))
@ -77,13 +77,13 @@ func setErrorFlashes(ctx echo.Context) {
}
func addFlash(ctx echo.Context, flashMessage string, flashType string) {
sess, _ := store.Get(ctx.Request(), "flash")
sess, _ := flashStore.Get(ctx.Request(), "flash")
sess.AddFlash(flashMessage, flashType)
_ = sess.Save(ctx.Request(), ctx.Response())
}
func getSession(ctx echo.Context) *sessions.Session {
sess, _ := store.Get(ctx.Request(), "session")
sess, _ := userStore.Get(ctx.Request(), "session")
return sess
}