git.iko.soy/typeable/oauth2-proxy
on v3.0.0 help sign in
133 lines 6.9 kB history raw
1package main
3import (
4 "flag"
5 "fmt"
6 "log"
7 "os"
8 "runtime"
9 "strings"
10 "time"
12 "github.com/BurntSushi/toml"
13 "github.com/mreiferson/go-options"
16func main() {
17 log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
18 flagSet := flag.NewFlagSet("oauth2_proxy", flag.ExitOnError)
20 emailDomains := StringArray{}
21 upstreams := StringArray{}
22 skipAuthRegex := StringArray{}
23 googleGroups := StringArray{}
25 config := flagSet.String("config", "", "path to config file")
26 showVersion := flagSet.Bool("version", false, "print version string")
28 flagSet.String("http-address", "127.0.0.1:4180", "[http://]<addr>:<port> or unix://<path> to listen on for HTTP clients")
29 flagSet.String("https-address", ":443", "<addr>:<port> to listen on for HTTPS clients")
30 flagSet.String("tls-cert", "", "path to certificate file")
31 flagSet.String("tls-key", "", "path to private key file")
32 flagSet.String("redirect-url", "", "the OAuth Redirect URL. ie: \"https://internalapp.yourcompany.com/oauth2/callback\"")
33 flagSet.Bool("set-xauthrequest", false, "set X-Auth-Request-User and X-Auth-Request-Email response headers (useful in Nginx auth_request mode)")
34 flagSet.Var(&upstreams, "upstream", "the http url(s) of the upstream endpoint or file:// paths for static files. Routing is based on the path")
35 flagSet.Bool("pass-basic-auth", true, "pass HTTP Basic Auth, X-Forwarded-User and X-Forwarded-Email information to upstream")
36 flagSet.Bool("pass-user-headers", true, "pass X-Forwarded-User and X-Forwarded-Email information to upstream")
37 flagSet.String("basic-auth-password", "", "the password to set when passing the HTTP Basic Auth header")
38 flagSet.Bool("pass-access-token", false, "pass OAuth access_token to upstream via X-Forwarded-Access-Token header")
39 flagSet.Bool("pass-host-header", true, "pass the request Host Header to upstream")
40 flagSet.Var(&skipAuthRegex, "skip-auth-regex", "bypass authentication for requests path's that match (may be given multiple times)")
41 flagSet.Bool("skip-provider-button", false, "will skip sign-in-page to directly reach the next step: oauth/start")
42 flagSet.Bool("skip-auth-preflight", false, "will skip authentication for OPTIONS requests")
43 flagSet.Bool("ssl-insecure-skip-verify", false, "skip validation of certificates presented when using HTTPS")
45 flagSet.Var(&emailDomains, "email-domain", "authenticate emails with the specified domain (may be given multiple times). Use * to authenticate any email")
46 flagSet.String("azure-tenant", "common", "go to a tenant-specific or common (tenant-independent) endpoint.")
47 flagSet.String("github-org", "", "restrict logins to members of this organisation")
48 flagSet.String("github-team", "", "restrict logins to members of this team")
49 flagSet.Var(&googleGroups, "google-group", "restrict logins to members of this google group (may be given multiple times).")
50 flagSet.String("google-admin-email", "", "the google admin to impersonate for api calls")
51 flagSet.String("google-service-account-json", "", "the path to the service account json credentials")
52 flagSet.String("client-id", "", "the OAuth Client ID: ie: \"123456.apps.googleusercontent.com\"")
53 flagSet.String("client-secret", "", "the OAuth Client Secret")
54 flagSet.String("authenticated-emails-file", "", "authenticate against emails via file (one per line)")
55 flagSet.String("htpasswd-file", "", "additionally authenticate against a htpasswd file. Entries must be created with \"htpasswd -s\" for SHA encryption or \"htpasswd -B\" for bcrypt encryption")
56 flagSet.Bool("display-htpasswd-form", true, "display username / password login form if an htpasswd file is provided")
57 flagSet.String("custom-templates-dir", "", "path to custom html templates")
58 flagSet.String("footer", "", "custom footer string. Use \"-\" to disable default footer.")
59 flagSet.String("proxy-prefix", "/oauth2", "the url root path that this proxy should be nested under (e.g. /<oauth2>/sign_in)")
61 flagSet.String("cookie-name", "_oauth2_proxy", "the name of the cookie that the oauth_proxy creates")
62 flagSet.String("cookie-secret", "", "the seed string for secure cookies (optionally base64 encoded)")
63 flagSet.String("cookie-domain", "", "an optional cookie domain to force cookies to (ie: .yourcompany.com)*")
64 flagSet.Duration("cookie-expire", time.Duration(168)*time.Hour, "expire timeframe for cookie")
65 flagSet.Duration("cookie-refresh", time.Duration(0), "refresh the cookie after this duration; 0 to disable")
66 flagSet.Bool("cookie-secure", true, "set secure (HTTPS) cookie flag")
67 flagSet.Bool("cookie-httponly", true, "set HttpOnly cookie flag")
69 flagSet.Bool("request-logging", true, "Log requests to stdout")
70 flagSet.String("request-logging-format", defaultRequestLoggingFormat, "Template for log lines")
72 flagSet.String("provider", "google", "OAuth provider")
73 flagSet.String("oidc-issuer-url", "", "OpenID Connect issuer URL (ie: https://accounts.google.com)")
74 flagSet.String("login-url", "", "Authentication endpoint")
75 flagSet.String("redeem-url", "", "Token redemption endpoint")
76 flagSet.String("profile-url", "", "Profile access endpoint")
77 flagSet.String("resource", "", "The resource that is protected (Azure AD only)")
78 flagSet.String("validate-url", "", "Access token validation endpoint")
79 flagSet.String("scope", "", "OAuth scope specification")
80 flagSet.String("approval-prompt", "force", "OAuth approval_prompt")
82 flagSet.String("signature-key", "", "GAP-Signature request signature key (algorithm:secretkey)")
84 flagSet.Parse(os.Args[1:])
86 if *showVersion {
87 fmt.Printf("oauth2_proxy %s (built with %s)\n", VERSION, runtime.Version())
88 return
89 }
91 opts := NewOptions()
93 cfg := make(EnvOptions)
94 if *config != "" {
95 _, err := toml.DecodeFile(*config, &cfg)
96 if err != nil {
97 log.Fatalf("ERROR: failed to load config file %s - %s", *config, err)
98 }
99 }
100 cfg.LoadEnvForStruct(opts)
101 options.Resolve(opts, flagSet, cfg)
103 err := opts.Validate()
104 if err != nil {
105 log.Printf("%s", err)
106 os.Exit(1)
107 }
108 validator := NewValidator(opts.EmailDomains, opts.AuthenticatedEmailsFile)
109 oauthproxy := NewOAuthProxy(opts, validator)
111 if len(opts.EmailDomains) != 0 && opts.AuthenticatedEmailsFile == "" {
112 if len(opts.EmailDomains) > 1 {
113 oauthproxy.SignInMessage = fmt.Sprintf("Authenticate using one of the following domains: %v", strings.Join(opts.EmailDomains, ", "))
114 } else if opts.EmailDomains[0] != "*" {
115 oauthproxy.SignInMessage = fmt.Sprintf("Authenticate using %v", opts.EmailDomains[0])
116 }
117 }
119 if opts.HtpasswdFile != "" {
120 log.Printf("using htpasswd file %s", opts.HtpasswdFile)
121 oauthproxy.HtpasswdFile, err = NewHtpasswdFromFile(opts.HtpasswdFile)
122 oauthproxy.DisplayHtpasswdForm = opts.DisplayHtpasswdForm
123 if err != nil {
124 log.Fatalf("FATAL: unable to open %s %s", opts.HtpasswdFile, err)
125 }
126 }
128 s := &Server{
129 Handler: LoggingHandler(os.Stdout, oauthproxy, opts.RequestLogging, opts.RequestLoggingFormat),
130 Opts: opts,
131 }
132 s.ListenAndServe()