From a7e3c3a7effac84d91fc23169ea3b898693892aa Mon Sep 17 00:00:00 2001 From: Christian Groschupp Date: Wed, 18 Sep 2019 22:40:33 +0200 Subject: [PATCH] Add static upstream --- docs/configuration/configuration.md | 2 +- main.go | 2 +- oauthproxy.go | 11 +++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index 6fca562..62cd861 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -105,7 +105,7 @@ An example [oauth2_proxy.cfg]({{ site.gitweb }}/contrib/oauth2_proxy.cfg.example | `-standard-logging-format` | string | Template for standard log lines | see [Logging Configuration](#logging-configuration) | | `-tls-cert-file` | string | path to certificate file | | | `-tls-key-file` | string | path to private key file | | -| `-upstream` | string \| list | the http url(s) of the upstream endpoint or `file://` paths for static files. Routing is based on the path | | +| `-upstream` | string \| list | the http url(s) of the upstream endpoint, file:// paths for static files or `static://` for static response. Routing is based on the path | | | `-validate-url` | string | Access token validation endpoint | | | `-version` | n/a | print version string | | | `-whitelist-domain` | string \| list | allowed domains for redirection after authentication. Prefix domain with a `.` to allow subdomains (eg `.example.com`) | | diff --git a/main.go b/main.go index a9f1e4a..9a44700 100644 --- a/main.go +++ b/main.go @@ -36,7 +36,7 @@ func main() { flagSet.String("tls-key-file", "", "path to private key file") flagSet.String("redirect-url", "", "the OAuth Redirect URL. ie: \"https://internalapp.yourcompany.com/oauth2/callback\"") flagSet.Bool("set-xauthrequest", false, "set X-Auth-Request-User and X-Auth-Request-Email response headers (useful in Nginx auth_request mode)") - flagSet.Var(&upstreams, "upstream", "the http url(s) of the upstream endpoint or file:// paths for static files. Routing is based on the path") + flagSet.Var(&upstreams, "upstream", "the http url(s) of the upstream endpoint, file:// paths for static files or static:// for static response. Routing is based on the path") flagSet.Bool("pass-basic-auth", true, "pass HTTP Basic Auth, X-Forwarded-User and X-Forwarded-Email information to upstream") flagSet.Bool("pass-user-headers", true, "pass X-Forwarded-User and X-Forwarded-Email information to upstream") flagSet.String("basic-auth-password", "", "the password to set when passing the HTTP Basic Auth header") diff --git a/oauthproxy.go b/oauthproxy.go index 2418e73..f40c249 100644 --- a/oauthproxy.go +++ b/oauthproxy.go @@ -12,6 +12,7 @@ import ( "net/http/httputil" "net/url" "regexp" + "strconv" "strings" "time" @@ -207,6 +208,16 @@ func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy { proxy := NewWebSocketOrRestReverseProxy(u, opts, auth) serveMux.Handle(path, proxy) + case "static": + serveMux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) { + responseCode, err := strconv.Atoi(u.Host) + if err != nil { + logger.Printf("unable to convert %q to int, use default \"200\"", u.Host) + responseCode = 200 + } + rw.WriteHeader(responseCode) + fmt.Fprintf(rw, "Authenticated") + }) case "file": if u.Fragment != "" { path = u.Fragment