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

75 lines
1.6 KiB
Go
Raw Normal View History

2020-11-11 14:03:16 +01:00
package session
import (
"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
// Name of the session cookie. This cookie will store session key.
// Optional. Default value "session_id".
CookieName 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
// Indicates if CSRF cookie is HTTP only.
// Optional. Default value false.
CookieSameSite string
// KeyGenerator generates the session key.
// Optional. Default value utils.UUID
KeyGenerator func() string
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,
2020-11-11 23:51:32 +01:00
CookieName: "session_id",
2020-11-11 14:03:16 +01:00
KeyGenerator: utils.UUID,
}
// 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.KeyGenerator == nil {
cfg.KeyGenerator = ConfigDefault.KeyGenerator
}
2020-11-11 14:03:16 +01:00
return cfg
}