git.iko.soy/typeable/oauth2-proxy
on master help sign in
Move template options to their own struct
commit84f76c6060b05d0846adb19a47a71b8c3e30f93a authorJoel Speed <joel.speed@hotmail.co.uk> dateSat 6 Feb 2021, 17:40 (6 years ago) parent801edeb
3 files changed+64−26
oauthproxy.go+7−7 pkg/apis/options/app.go+43 pkg/apis/options/options.go+14−19
oauthproxy.go+7−7
@@ -116,7 +116,7 @@ func NewOAuthProxy(opts *options.Options, validator func(string) bool) (*OAuthPr
116116 return nil, fmt.Errorf("error initialising session store: %v", err)
117117 }
118118
119− templates := loadTemplates(opts.CustomTemplatesDir)
119+ templates := loadTemplates(opts.Templates.Path)
120120 proxyErrorHandler := upstream.NewProxyErrorHandler(templates.Lookup("error.html"), opts.ProxyPrefix)
121121 upstreamProxy, err := upstream.NewProxy(opts.UpstreamServers, opts.GetSignatureData(), proxyErrorHandler)
122122 if err != nil {
@@ -211,12 +211,12 @@ func NewOAuthProxy(opts *options.Options, validator func(string) bool) (*OAuthPr
211211 SkipProviderButton: opts.SkipProviderButton,
212212 templates: templates,
213213 trustedIPs: trustedIPs,
214− Banner: opts.Banner,
215− Footer: opts.Footer,
214+ Banner: opts.Templates.Banner,
215+ Footer: opts.Templates.Footer,
216216 SignInMessage: buildSignInMessage(opts),
217217
218218 basicAuthValidator: basicAuthValidator,
219− displayHtpasswdForm: basicAuthValidator != nil && opts.DisplayHtpasswdForm,
219+ displayHtpasswdForm: basicAuthValidator != nil && opts.Templates.DisplayLoginForm,
220220 sessionChain: sessionChain,
221221 headersChain: headersChain,
222222 preAuthChain: preAuthChain,
@@ -301,11 +301,11 @@ func buildHeadersChain(opts *options.Options) (alice.Chain, error) {
301301
302302func buildSignInMessage(opts *options.Options) string {
303303 var msg string
304− if len(opts.Banner) >= 1 {
305− if opts.Banner == "-" {
304+ if len(opts.Templates.Banner) >= 1 {
305+ if opts.Templates.Banner == "-" {
306306 msg = ""
307307 } else {
308− msg = opts.Banner
308+ msg = opts.Templates.Banner
309309 }
310310 } else if len(opts.EmailDomains) != 0 && opts.AuthenticatedEmailsFile == "" {
311311 if len(opts.EmailDomains) > 1 {
pkg/apis/options/app.gonew file+43
@@ -0,0 +1,43 @@
1+package options
2+
3+import "github.com/spf13/pflag"
4+
5+// Templates includes options for configuring the sign in and error pages
6+// appearance.
7+type Templates struct {
8+ // Path is the path to a folder containing a sign_in.html and an error.html
9+ // template.
10+ // These files will be used instead of the default templates if present.
11+ // If either file is missing, the default will be used instead.
12+ Path string `flag:"custom-templates-dir" cfg:"custom_templates_dir"`
13+
14+ // Banner overides the default sign_in page banner text. If unspecified,
15+ // the message will give users a list of allowed email domains.
16+ Banner string `flag:"banner" cfg:"banner"`
17+
18+ // Footer overrides the default sign_in page footer text.
19+ Footer string `flag:"footer" cfg:"footer"`
20+
21+ // DisplayLoginForm determines whether the sign_in page should render a
22+ // password form if a static passwords file (htpasswd file) has been
23+ // configured.
24+ DisplayLoginForm bool `flag:"display-htpasswd-form" cfg:"display_htpasswd_form"`
25+}
26+
27+func templatesFlagSet() *pflag.FlagSet {
28+ flagSet := pflag.NewFlagSet("templates", pflag.ExitOnError)
29+
30+ flagSet.String("custom-templates-dir", "", "path to custom html templates")
31+ flagSet.String("banner", "", "custom banner string. Use \"-\" to disable default banner.")
32+ flagSet.String("footer", "", "custom footer string. Use \"-\" to disable default footer.")
33+ flagSet.Bool("display-htpasswd-form", true, "display username / password login form if an htpasswd file is provided")
34+
35+ return flagSet
36+}
37+
38+// templatesDefaults creates a Templates and populates it with any default values
39+func templatesDefaults() Templates {
40+ return Templates{
41+ DisplayLoginForm: true,
42+ }
43+}
pkg/apis/options/options.go+14−19
@@ -53,14 +53,11 @@ type Options struct {
5353 GoogleAdminEmail string `flag:"google-admin-email" cfg:"google_admin_email"`
5454 GoogleServiceAccountJSON string `flag:"google-service-account-json" cfg:"google_service_account_json"`
5555 HtpasswdFile string `flag:"htpasswd-file" cfg:"htpasswd_file"`
56− DisplayHtpasswdForm bool `flag:"display-htpasswd-form" cfg:"display_htpasswd_form"`
57− CustomTemplatesDir string `flag:"custom-templates-dir" cfg:"custom_templates_dir"`
58− Banner string `flag:"banner" cfg:"banner"`
59− Footer string `flag:"footer" cfg:"footer"`
6056
61− Cookie Cookie `cfg:",squash"`
62− Session SessionOptions `cfg:",squash"`
63− Logging Logging `cfg:",squash"`
57+ Cookie Cookie `cfg:",squash"`
58+ Session SessionOptions `cfg:",squash"`
59+ Logging Logging `cfg:",squash"`
60+ Templates Templates `cfg:",squash"`
6461
6562 // Not used in the legacy config, name not allowed to match an external key (upstreams)
6663 // TODO(JoelSpeed): Rename when legacy config is removed
@@ -135,16 +132,17 @@ func (o *Options) SetRealClientIPParser(s ipapi.RealClientIPParser) { o.realClie
135132// NewOptions constructs a new Options with defaulted values
136133func NewOptions() *Options {
137134 return &Options{
138− ProxyPrefix: "/oauth2",
139− ProviderType: "google",
140− PingPath: "/ping",
141− HTTPAddress: "127.0.0.1:4180",
142− HTTPSAddress: ":443",
143− RealClientIPHeader: "X-Real-IP",
144− ForceHTTPS: false,
145− DisplayHtpasswdForm: true,
135+ ProxyPrefix: "/oauth2",
136+ ProviderType: "google",
137+ PingPath: "/ping",
138+ HTTPAddress: "127.0.0.1:4180",
139+ HTTPSAddress: ":443",
140+ RealClientIPHeader: "X-Real-IP",
141+ ForceHTTPS: false,
142+
146143 Cookie: cookieDefaults(),
147144 Session: sessionOptionsDefaults(),
145+ Templates: templatesDefaults(),
148146 AzureTenant: "common",
149147 SkipAuthPreflight: false,
150148 Prompt: "", // Change to "login" when ApprovalPrompt officially deprecated
@@ -200,10 +198,6 @@ func NewFlagSet() *pflag.FlagSet {
200198 flagSet.String("client-secret-file", "", "the file with OAuth Client Secret")
201199 flagSet.String("authenticated-emails-file", "", "authenticate against emails via file (one per line)")
202200 flagSet.String("htpasswd-file", "", "additionally authenticate against a htpasswd file. Entries must be created with \"htpasswd -B\" for bcrypt encryption")
203− flagSet.Bool("display-htpasswd-form", true, "display username / password login form if an htpasswd file is provided")
204− flagSet.String("custom-templates-dir", "", "path to custom html templates")
205− flagSet.String("banner", "", "custom banner string. Use \"-\" to disable default banner.")
206− flagSet.String("footer", "", "custom footer string. Use \"-\" to disable default footer.")
207201 flagSet.String("proxy-prefix", "/oauth2", "the url root path that this proxy should be nested under (e.g. /<oauth2>/sign_in)")
208202 flagSet.String("ping-path", "/ping", "the ping endpoint that can be used for basic health checks")
209203 flagSet.String("ping-user-agent", "", "special User-Agent that will be used for basic health checks")
@@ -251,6 +245,7 @@ func NewFlagSet() *pflag.FlagSet {
251245
252246 flagSet.AddFlagSet(cookieFlagSet())
253247 flagSet.AddFlagSet(loggingFlagSet())
248+ flagSet.AddFlagSet(templatesFlagSet())
254249
255250 return flagSet
256251}