Session refresh, shorter default ttl

- Session cookies also no longer expire on the client
This commit is contained in:
adro 2022-02-23 13:34:52 +01:00
parent 68845fc715
commit c56ba425c7
3 changed files with 5 additions and 3 deletions

View File

@ -27,7 +27,7 @@ var configPath string
func init() { func init() {
Config = config{ Config = config{
Server: ":8080", Server: ":8080",
SessionTTL: 1440, SessionTTL: 10,
} }
// Locations to look for a config file for // Locations to look for a config file for

View File

@ -1,6 +1,6 @@
Server = ":8080" # The address the webserver should bind to Server = ":8080" # The address the webserver should bind to
PassHash = "$2a$10$I.26oCzkjZ8qwfhbmeYM3.kppBjxtPsxkeE1Y.ULjVvA1IBPcQP42" # "password" PassHash = "$2a$10$I.26oCzkjZ8qwfhbmeYM3.kppBjxtPsxkeE1Y.ULjVvA1IBPcQP42" # "password"
SessionTTL = 60 # How many minutes sessions last for SessionTTL = 10 # How many minutes sessions last for
[[Devices]] [[Devices]]
Alias = "SomeDevice" Alias = "SomeDevice"

View File

@ -37,9 +37,12 @@ func checkAuth(token string) error {
func withAuth(handler echo.HandlerFunc) echo.HandlerFunc { func withAuth(handler echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error { return func(c echo.Context) error {
session, err := c.Cookie("session") session, err := c.Cookie("session")
// Redirect to login if session expired/invalid
if err != nil || checkAuth(session.Value) != nil { if err != nil || checkAuth(session.Value) != nil {
return c.Redirect(http.StatusSeeOther, "/") return c.Redirect(http.StatusSeeOther, "/")
} }
// Refresh session
sessions[session.Value] = time.Now().Add(time.Second * time.Duration(config.Config.SessionTTL*60))
return handler(c) return handler(c)
} }
} }
@ -59,7 +62,6 @@ func auth(c echo.Context) error {
Secure: true, Secure: true,
HttpOnly: true, HttpOnly: true,
SameSite: http.SameSiteStrictMode, SameSite: http.SameSiteStrictMode,
Expires: sessions[token],
}) })
return c.Redirect(http.StatusSeeOther, "/") return c.Redirect(http.StatusSeeOther, "/")
} }