2019-04-24 21:52:08 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
2020-05-18 02:45:47 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/fasthttp/session/v2"
|
|
|
|
"github.com/fasthttp/session/v2/providers/redis"
|
|
|
|
|
2020-04-05 12:37:21 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/internal/utils"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// NewProviderConfig creates a configuration for creating the session provider.
|
2019-04-24 21:52:08 +00:00
|
|
|
func NewProviderConfig(configuration schema.SessionConfiguration) ProviderConfig {
|
|
|
|
config := session.NewDefaultConfig()
|
|
|
|
|
|
|
|
// Override the cookie name.
|
|
|
|
config.CookieName = configuration.Name
|
|
|
|
|
|
|
|
// Set the cookie to the given domain.
|
|
|
|
config.Domain = configuration.Domain
|
|
|
|
|
|
|
|
// Only serve the header over HTTPS.
|
|
|
|
config.Secure = true
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Ignore the error as it will be handled by validator.
|
2020-05-18 02:45:47 +00:00
|
|
|
config.Expiration, _ = utils.ParseDurationString(configuration.Expiration)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
// TODO(c.michaud): Make this configurable by giving the list of IPs that are trustable.
|
|
|
|
config.IsSecureFunc = func(*fasthttp.RequestCtx) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-05-18 02:45:47 +00:00
|
|
|
var redisConfig *redis.Config
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
var providerName string
|
|
|
|
|
|
|
|
// If redis configuration is provided, then use the redis provider.
|
|
|
|
if configuration.Redis != nil {
|
|
|
|
providerName = "redis"
|
2020-03-28 06:10:39 +00:00
|
|
|
serializer := NewEncryptingSerializer(configuration.Secret)
|
2020-05-18 02:45:47 +00:00
|
|
|
network := "tcp"
|
|
|
|
|
|
|
|
var addr string
|
|
|
|
|
|
|
|
if configuration.Redis.Port == 0 {
|
|
|
|
network = "unix"
|
|
|
|
addr = configuration.Redis.Host
|
|
|
|
} else {
|
|
|
|
addr = fmt.Sprintf("%s:%d", configuration.Redis.Host, configuration.Redis.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
redisConfig = &redis.Config{
|
|
|
|
Network: network,
|
|
|
|
Addr: addr,
|
2020-02-28 00:14:44 +00:00
|
|
|
Password: configuration.Redis.Password,
|
2020-05-18 02:45:47 +00:00
|
|
|
// DB is the fasthttp/session property for the Redis DB Index.
|
|
|
|
DB: configuration.Redis.DatabaseIndex,
|
|
|
|
PoolSize: 8,
|
|
|
|
IdleTimeout: 300,
|
|
|
|
KeyPrefix: "authelia-session",
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
2020-05-18 02:45:47 +00:00
|
|
|
config.EncodeFunc = serializer.Encode
|
|
|
|
config.DecodeFunc = serializer.Decode
|
2019-04-24 21:52:08 +00:00
|
|
|
} else { // if no option is provided, use the memory provider.
|
|
|
|
providerName = "memory"
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
return ProviderConfig{
|
2020-05-18 02:45:47 +00:00
|
|
|
config: config,
|
|
|
|
redisConfig: redisConfig,
|
|
|
|
providerName: providerName,
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
}
|