bouncer/internal/proxy/director/layer/authn/layer_options.go

67 lines
1.5 KiB
Go

package authn
import (
"time"
"forge.cadoles.com/cadoles/bouncer/internal/store"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)
const DefaultSessionName = "bouncer-authn"
type LayerOptions struct {
MatchURLs []string `mapstructure:"matchURLs"`
Cookie CookieOptions `mapstructure:"cookie"`
Session SessionOptions `mapstructure:"session"`
}
type CookieOptions struct {
Domain string `mapstructure:"domain"`
Name string `mapstructure:"name"`
Path string `mapstructure:"path"`
SameSite bool `mapstructure:"sameSite"`
Secure bool `mapstructure:"secure"`
HTTPOnly bool `mapstructure:"httpOnly"`
MaxAge time.Duration `mapstructure:"maxAge"`
}
type SessionOptions struct {
Name string `mapstructure:"name"`
TTL time.Duration `mapstructure:"ttl"`
}
func DefaultLayerOptions() LayerOptions {
return LayerOptions{
MatchURLs: []string{"*"},
Cookie: CookieOptions{
Path: "/",
HTTPOnly: true,
MaxAge: time.Hour,
},
Session: SessionOptions{
Name: DefaultSessionName,
TTL: time.Hour,
},
}
}
func fromStoreOptions(storeOptions store.LayerOptions) (*LayerOptions, error) {
layerOptions := DefaultLayerOptions()
config := mapstructure.DecoderConfig{
Result: &layerOptions,
}
decoder, err := mapstructure.NewDecoder(&config)
if err != nil {
return nil, err
}
if err := decoder.Decode(storeOptions); err != nil {
return nil, errors.WithStack(err)
}
return &layerOptions, nil
}