Auth + Encryption for cookies, based on SessionSecret via SHA512

This commit is contained in:
0xCA 2023-12-28 11:55:13 +05:00
parent 6292424591
commit 91427427f2
3 changed files with 16 additions and 4 deletions

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"crypto/sha512"
"embed" "embed"
"flag" "flag"
"fmt" "fmt"
@ -136,7 +137,7 @@ func init() {
util.SendgridApiKey = flagSendgridApiKey util.SendgridApiKey = flagSendgridApiKey
util.EmailFrom = flagEmailFrom util.EmailFrom = flagEmailFrom
util.EmailFromName = flagEmailFromName util.EmailFromName = flagEmailFromName
util.SessionSecret = []byte(flagSessionSecret) util.SessionSecret = sha512.Sum512([]byte(flagSessionSecret))
util.WgConfTemplate = flagWgConfTemplate util.WgConfTemplate = flagWgConfTemplate
util.BasePath = util.ParseBasePath(flagBasePath) util.BasePath = util.ParseBasePath(flagBasePath)
util.SubnetRanges = util.ParseSubnetRanges(flagSubnetRanges) util.SubnetRanges = util.ParseSubnetRanges(flagSubnetRanges)

View file

@ -48,9 +48,20 @@ func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c
} }
// New function // New function
func New(tmplDir fs.FS, extraData map[string]interface{}, secret []byte) *echo.Echo { func New(tmplDir fs.FS, extraData map[string]interface{}, secret [64]byte) *echo.Echo {
e := echo.New() e := echo.New()
e.Use(session.Middleware(sessions.NewCookieStore(secret)))
cookiePath := util.BasePath
if cookiePath == "" {
cookiePath = "/"
}
cookieStore := sessions.NewCookieStore(secret[:32], secret[32:])
cookieStore.Options.Path = cookiePath
cookieStore.Options.HttpOnly = true
cookieStore.MaxAge(86400 * 7)
e.Use(session.Middleware(cookieStore))
// read html template file to string // read html template file to string
tmplBaseString, err := util.StringFromEmbedFile(tmplDir, "base.html") tmplBaseString, err := util.StringFromEmbedFile(tmplDir, "base.html")

View file

@ -22,7 +22,7 @@ var (
SendgridApiKey string SendgridApiKey string
EmailFrom string EmailFrom string
EmailFromName string EmailFromName string
SessionSecret []byte SessionSecret [64]byte
WgConfTemplate string WgConfTemplate string
BasePath string BasePath string
SubnetRanges map[string]([]*net.IPNet) SubnetRanges map[string]([]*net.IPNet)