zrok/endpoints/publicProxy/config.go

77 lines
1.6 KiB
Go
Raw Normal View History

package publicProxy
import (
"context"
"fmt"
"github.com/michaelquigley/cf"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
zhttp "github.com/zitadel/oidc/v2/pkg/http"
"strings"
)
const V = 2
type Config struct {
V int
Identity string
Address string
HostMatch string
2023-09-05 16:55:55 +02:00
Oauth *OauthConfig
}
type OauthConfig struct {
2023-10-05 19:34:27 +02:00
RedirectHost string
RedirectPort int
RedirectHttpOnly bool
HashKey string `cf:"+secret"`
Providers []*OauthProviderConfig
2023-09-05 16:55:55 +02:00
}
func (oc *OauthConfig) GetProvider(name string) *OauthProviderConfig {
2023-09-05 16:55:55 +02:00
for _, provider := range oc.Providers {
if provider.Name == name {
return provider
}
}
return nil
}
type OauthProviderConfig struct {
2023-09-05 16:55:55 +02:00
Name string
ClientId string
2023-09-26 19:43:42 +02:00
ClientSecret string `cf:"+secret"`
}
func DefaultConfig() *Config {
return &Config{
Identity: "public",
Address: "0.0.0.0:8080",
}
}
func (c *Config) Load(path string) error {
if err := cf.BindYaml(c, path, cf.DefaultOptions()); err != nil {
return errors.Wrapf(err, "error loading frontend config '%v'", path)
}
if c.V != V {
return errors.Errorf("invalid configuration version '%d'; expected '%d'", c.V, V)
}
return nil
}
func configureOauthHandlers(ctx context.Context, cfg *Config, tls bool) error {
if cfg.Oauth == nil {
logrus.Info("no oauth configuration; skipping oauth handler startup")
return nil
}
if err := configureGoogleOauth(cfg.Oauth, tls); err != nil {
return err
}
if err := configureGithubOauth(cfg.Oauth, tls); err != nil {
return err
}
zhttp.StartServer(ctx, fmt.Sprintf("%s:%d", strings.Split(cfg.Address, ":")[0], cfg.Oauth.RedirectPort))
return nil
}