mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-22 21:23:46 +00:00
* 🔥 Feature: Define KeyLookup configuration (#1110) * 🔥 Feature: Allow session ID to be written in header (#1110) * 🔥 Feature: Allow session ID to be obtained from different sources (#1110) * 📚 Doc: Add Source configuration (#1110)
122 lines
2.8 KiB
Go
122 lines
2.8 KiB
Go
package session
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/utils"
|
|
)
|
|
|
|
// Config defines the config for middleware.
|
|
type Config struct {
|
|
// Allowed session duration
|
|
// Optional. Default value 24 * time.Hour
|
|
Expiration time.Duration
|
|
|
|
// Storage interface to store the session data
|
|
// Optional. Default value memory.New()
|
|
Storage fiber.Storage
|
|
|
|
// 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
|
|
|
|
// 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".
|
|
CookieSameSite string
|
|
|
|
// KeyGenerator generates the session key.
|
|
// Optional. Default value utils.UUIDv4
|
|
KeyGenerator func() string
|
|
|
|
// Deprecated, please use KeyLookup
|
|
CookieName string
|
|
|
|
// Source defines where to obtain the session id
|
|
source Source
|
|
|
|
// The session name
|
|
sessionName string
|
|
}
|
|
|
|
type Source string
|
|
|
|
const (
|
|
SourceCookie Source = "cookie"
|
|
SourceHeader Source = "header"
|
|
SourceURLQuery Source = "query"
|
|
)
|
|
|
|
// ConfigDefault is the default config
|
|
var ConfigDefault = Config{
|
|
Expiration: 24 * time.Hour,
|
|
KeyLookup: "cookie:session_id",
|
|
KeyGenerator: utils.UUIDv4,
|
|
source: "cookie",
|
|
sessionName: "session_id",
|
|
}
|
|
|
|
// 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
|
|
if int(cfg.Expiration.Seconds()) <= 0 {
|
|
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
|
|
}
|
|
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]
|
|
|
|
return cfg
|
|
}
|