oidc-forward-auth/pkg/options/options.go

54 lines
1.6 KiB
Go

/*
Copyright (c) 2020 Stefan Kürzeder <info@stivik.de>
This code is licensed under MIT license (see LICENSE for details)
*/
package options
import (
"fmt"
"strings"
"github.com/caarlos0/env"
"github.com/tg123/go-htpasswd"
)
type Options struct {
Issuer string `env:"ISSUER"`
ClientID string `env:"CLIENT_ID"`
ClientSecret string `env:"CLIENT_SECRET"`
AuthDomain string `env:"AUTH_DOMAIN"`
CookieDomain string `env:"COOKIE_DOMAIN"`
CookiePrefix string `env:"COOKIE_PREFIX" envDefault:"oidca_"`
Port int `env:"PORT" envDefault:"4181"`
RedirectURL string `env:"REDIRECT_URL" envDefault:"/auth/resp"`
Scopes string `env:"SCOPES"`
BypassUser string `env:"BYPASS_USER"`
BypassFile string `env:"BYPASS_FILE"`
BypassPwd *htpasswd.File
SessionLifetime int `env:"SESSION_LIFETIME" envDefault:"0"`
}
// LoadOptions parses the environment vars and the options
func LoadOptions() (*Options, error) {
options := Options{}
if err := env.Parse(&options); err != nil {
return nil, fmt.Errorf("failed to parse options: %s", err)
}
if options.BypassFile != "" {
parsed, err := htpasswd.New(options.BypassFile, htpasswd.DefaultSystems, func(err error) {})
if err != nil {
return nil, err
}
options.BypassPwd = parsed
} else if options.BypassUser != "" {
prep := strings.ReplaceAll(options.BypassUser, ";", "\n")
parsed, err := htpasswd.NewFromReader(strings.NewReader(prep), htpasswd.DefaultSystems, func(err error) {})
if err != nil {
return nil, err
}
options.BypassPwd = parsed
}
return &options, nil
}