1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-23 09:23:39 +00:00

122 lines
2.8 KiB
Go
Raw Normal View History

2020-11-11 14:03:16 +01:00
package session
import (
"fmt"
"strings"
2020-11-11 14:03:16 +01:00
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
)
// Config defines the config for middleware.
type Config struct {
// Allowed session duration
2020-11-11 23:51:32 +01:00
// Optional. Default value 24 * time.Hour
2020-11-11 14:03:16 +01:00
Expiration time.Duration
2020-11-11 23:51:32 +01:00
// Storage interface to store the session data
// Optional. Default value memory.New()
2020-11-11 14:03:16 +01:00
Storage fiber.Storage
2020-11-11 23:51:32 +01:00
// KeyLookup is a string in the form of "<source>:<name>" that is used
// to extract session id from the request.
// Possible values: "header:<name>", "query:<name>" or "cookie:<name>"
// Optional. Default value "cookie:session_id".
KeyLookup string
2020-11-11 23:51:32 +01:00
// Domain of the CSRF cookie.
// Optional. Default value "".
CookieDomain string
// Path of the CSRF cookie.
// Optional. Default value "".
CookiePath string
// Indicates if CSRF cookie is secure.
// Optional. Default value false.
CookieSecure bool
// Indicates if CSRF cookie is HTTP only.
// Optional. Default value false.
CookieHTTPOnly bool
// Value of SameSite cookie.
// Optional. Default value "Lax".
2020-11-11 23:51:32 +01:00
CookieSameSite string
// KeyGenerator generates the session key.
2020-11-30 14:31:10 +01:00
// Optional. Default value utils.UUIDv4
2020-11-11 23:51:32 +01:00
KeyGenerator func() string
// Deprecated, please use KeyLookup
CookieName string
// Source defines where to obtain the session id
source Source
// The session name
sessionName string
2020-11-11 14:03:16 +01:00
}
type Source string
const (
SourceCookie Source = "cookie"
SourceHeader Source = "header"
SourceURLQuery Source = "query"
)
2020-11-11 14:03:16 +01:00
// ConfigDefault is the default config
var ConfigDefault = Config{
2020-11-11 18:49:07 +01:00
Expiration: 24 * time.Hour,
KeyLookup: "cookie:session_id",
2020-11-30 14:31:10 +01:00
KeyGenerator: utils.UUIDv4,
source: "cookie",
sessionName: "session_id",
2020-11-11 14:03:16 +01:00
}
// Helper function to set default values
func configDefault(config ...Config) Config {
// Return default config if nothing provided
if len(config) < 1 {
return ConfigDefault
}
// Override default config
cfg := config[0]
// Set default values
2020-11-13 18:30:14 +01:00
if int(cfg.Expiration.Seconds()) <= 0 {
2020-11-11 23:51:32 +01:00
cfg.Expiration = ConfigDefault.Expiration
}
if cfg.CookieName != "" {
fmt.Println("[session] CookieName is deprecated, please use KeyLookup")
cfg.KeyLookup = fmt.Sprintf("cookie:%s", cfg.CookieName)
}
if cfg.KeyLookup == "" {
cfg.KeyLookup = ConfigDefault.KeyLookup
}
2020-11-11 23:51:32 +01:00
if cfg.KeyGenerator == nil {
cfg.KeyGenerator = ConfigDefault.KeyGenerator
}
selectors := strings.Split(cfg.KeyLookup, ":")
if len(selectors) != 2 {
panic("[session] KeyLookup must in the form of <source>:<name>")
}
switch Source(selectors[0]) {
case SourceCookie:
cfg.source = SourceCookie
case SourceHeader:
cfg.source = SourceHeader
case SourceURLQuery:
cfg.source = SourceURLQuery
default:
panic("[session] source is not supported")
}
cfg.sessionName = selectors[1]
2020-11-11 14:03:16 +01:00
return cfg
}