2019-04-24 21:52:08 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
2019-04-24 21:52:08 +00:00
|
|
|
"github.com/fasthttp/session"
|
|
|
|
"github.com/fasthttp/session/memory"
|
|
|
|
"github.com/fasthttp/session/redis"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewProviderConfig creates a configuration for creating the session provider
|
|
|
|
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-04-03 23:11:33 +00:00
|
|
|
// TODO(james-d-elliott): Convert to duration notation
|
2019-04-24 21:52:08 +00:00
|
|
|
if configuration.Expiration > 0 {
|
|
|
|
config.Expires = time.Duration(configuration.Expiration) * time.Second
|
|
|
|
} else {
|
|
|
|
// If Expiration is 0 then cookie expiration is disabled.
|
|
|
|
config.Expires = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(c.michaud): Make this configurable by giving the list of IPs that are trustable.
|
|
|
|
config.IsSecureFunc = func(*fasthttp.RequestCtx) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var providerConfig session.ProviderConfig
|
|
|
|
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)
|
2019-04-24 21:52:08 +00:00
|
|
|
providerConfig = &redis.Config{
|
2020-02-28 00:14:44 +00:00
|
|
|
Host: configuration.Redis.Host,
|
|
|
|
Port: configuration.Redis.Port,
|
|
|
|
Password: configuration.Redis.Password,
|
|
|
|
// DbNumber is the fasthttp/session property for the Redis DB Index
|
2020-03-28 06:10:39 +00:00
|
|
|
DbNumber: configuration.Redis.DatabaseIndex,
|
|
|
|
PoolSize: 8,
|
|
|
|
IdleTimeout: 300,
|
|
|
|
KeyPrefix: "authelia-session",
|
|
|
|
SerializeFunc: serializer.Encode,
|
|
|
|
UnSerializeFunc: serializer.Decode,
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
} else { // if no option is provided, use the memory provider.
|
|
|
|
providerName = "memory"
|
|
|
|
providerConfig = &memory.Config{}
|
|
|
|
}
|
|
|
|
return ProviderConfig{
|
|
|
|
config: config,
|
|
|
|
providerName: providerName,
|
|
|
|
providerConfig: providerConfig,
|
|
|
|
}
|
|
|
|
}
|