authelia/internal/session/provider_config.go
James Elliott 8aade7f40e
[MISC] Update durations to notation format and housekeeping (#824)
* added regulation validator
* made regulations find_time and ban_time values duration notation strings
* added DefaultRegulationConfiguration for the validator
* made session expiration and inactivity values duration notation strings
* TOTP period does not need to be converted because adjustment should be discouraged
* moved TOTP defaults to DefaultTOTPConfiguration and removed the consts
* arranged the root config validator in configuration file order
* adjusted tests for the changes
* moved duration notation docs to root of configuration
* added references to duration notation where applicable
* project wide gofmt and goimports:
* run gofmt
* run goimports -local github.com/authelia/authelia -w on all files
* Make jwt_secret error uniform and add tests
* now at 100% coverage for internal/configuration/validator/configuration.go
2020-04-05 22:37:21 +10:00

63 lines
1.9 KiB
Go

package session
import (
"github.com/fasthttp/session"
"github.com/fasthttp/session/memory"
"github.com/fasthttp/session/redis"
"github.com/valyala/fasthttp"
"github.com/authelia/authelia/internal/configuration/schema"
"github.com/authelia/authelia/internal/utils"
)
// 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
// Ignore the error as it will be handled by validator
config.Expires, _ = utils.ParseDurationString(configuration.Expiration)
// 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"
serializer := NewEncryptingSerializer(configuration.Secret)
providerConfig = &redis.Config{
Host: configuration.Redis.Host,
Port: configuration.Redis.Port,
Password: configuration.Redis.Password,
// DbNumber is the fasthttp/session property for the Redis DB Index
DbNumber: configuration.Redis.DatabaseIndex,
PoolSize: 8,
IdleTimeout: 300,
KeyPrefix: "authelia-session",
SerializeFunc: serializer.Encode,
UnSerializeFunc: serializer.Decode,
}
} else { // if no option is provided, use the memory provider.
providerName = "memory"
providerConfig = &memory.Config{}
}
return ProviderConfig{
config: config,
providerName: providerName,
providerConfig: providerConfig,
}
}