git.iko.soy/typeable/oauth2-proxy
on master help sign in
add --set-xauthrequest flag for use in Nginx auth_request mode

This is enhancement of #173 to use "Auth Request" consistently in the command-line option, configuration file and response headers. It always sets the X-Auth-Request-User response header and if the email is available, sets X-Auth-Request-Email as well.

commit829b442302f7db79929b679a01a6a3f41388bc4b authorLukasz Siudut <lsiudut@gmail.com> dateThu 20 Oct 2016, 17:49 (10 years ago) parent93852a2
4 files changed+41
main.go+1 oauthproxy.go+8 oauthproxy_test.go+30 options.go+2
main.go+1
@@ -30,6 +30,7 @@ func main() {
3030 flagSet.String("tls-cert", "", "path to certificate file")
3131 flagSet.String("tls-key", "", "path to private key file")
3232 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)")
3334 flagSet.Var(&upstreams, "upstream", "the http url(s) of the upstream endpoint or file:// paths for static files. Routing is based on the path")
3435 flagSet.Bool("pass-basic-auth", true, "pass HTTP Basic Auth, X-Forwarded-User and X-Forwarded-Email information to upstream")
3536 flagSet.Bool("pass-user-headers", true, "pass X-Forwarded-User and X-Forwarded-Email information to upstream")
oauthproxy.go+8
@@ -60,6 +60,7 @@ type OAuthProxy struct {
6060 HtpasswdFile *HtpasswdFile
6161 DisplayHtpasswdForm bool
6262 serveMux http.Handler
63+ SetXAuthRequest bool
6364 PassBasicAuth bool
6465 SkipProviderButton bool
6566 PassUserHeaders bool
@@ -198,6 +199,7 @@ func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
198199 redirectURL: redirectURL,
199200 skipAuthRegex: opts.SkipAuthRegex,
200201 compiledRegex: opts.CompiledRegex,
202+ SetXAuthRequest: opts.SetXAuthRequest,
201203 PassBasicAuth: opts.PassBasicAuth,
202204 PassUserHeaders: opts.PassUserHeaders,
203205 BasicAuthPassword: opts.BasicAuthPassword,
@@ -663,6 +665,12 @@ func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) int
663665 req.Header["X-Forwarded-Email"] = []string{session.Email}
664666 }
665667 }
668+ if p.SetXAuthRequest {
669+ rw.Header().Set("X-Auth-Request-User", session.User)
670+ if session.Email != "" {
671+ rw.Header().Set("X-Auth-Request-Email", session.Email)
672+ }
673+ }
666674 if p.PassAccessToken && session.AccessToken != "" {
667675 req.Header["X-Forwarded-Access-Token"] = []string{session.AccessToken}
668676 }
oauthproxy_test.go+30
@@ -611,6 +611,36 @@ func TestAuthOnlyEndpointUnauthorizedOnEmailValidationFailure(t *testing.T) {
611611 assert.Equal(t, "unauthorized request\n", string(bodyBytes))
612612}
613613
614+func TestAuthOnlyEndpointSetXAuthRequestHeaders(t *testing.T) {
615+ var pc_test ProcessCookieTest
616+
617+ pc_test.opts = NewOptions()
618+ pc_test.opts.SetXAuthRequest = true
619+ pc_test.opts.Validate()
620+
621+ pc_test.proxy = NewOAuthProxy(pc_test.opts, func(email string) bool {
622+ return pc_test.validate_user
623+ })
624+ pc_test.proxy.provider = &TestProvider{
625+ ValidToken: true,
626+ }
627+
628+ pc_test.validate_user = true
629+
630+ pc_test.rw = httptest.NewRecorder()
631+ pc_test.req, _ = http.NewRequest("GET",
632+ pc_test.opts.ProxyPrefix+"/auth", nil)
633+
634+ startSession := &providers.SessionState{
635+ User: "oauth_user", Email: "oauth_user@example.com", AccessToken: "oauth_token"}
636+ pc_test.SaveSession(startSession, time.Now())
637+
638+ pc_test.proxy.ServeHTTP(pc_test.rw, pc_test.req)
639+ assert.Equal(t, http.StatusAccepted, pc_test.rw.Code)
640+ assert.Equal(t, "oauth_user", pc_test.rw.HeaderMap["X-Auth-Request-User"][0])
641+ assert.Equal(t, "oauth_user@example.com", pc_test.rw.HeaderMap["X-Auth-Request-Email"][0])
642+}
643+
614644type SignatureAuthenticator struct {
615645 auth hmacauth.HmacAuth
616646}
options.go+2
@@ -57,6 +57,7 @@ type Options struct {
5757 SkipProviderButton bool `flag:"skip-provider-button" cfg:"skip_provider_button"`
5858 PassUserHeaders bool `flag:"pass-user-headers" cfg:"pass_user_headers"`
5959 SSLInsecureSkipVerify bool `flag:"ssl-insecure-skip-verify" cfg:"ssl_insecure_skip_verify"`
60+ SetXAuthRequest bool `flag:"set-xauthrequest" cfg:"set_xauthrequest"`
6061
6162 // These options allow for other providers besides Google, with
6263 // potential overrides.
@@ -97,6 +98,7 @@ func NewOptions() *Options {
9798 CookieHttpOnly: true,
9899 CookieExpire: time.Duration(168) * time.Hour,
99100 CookieRefresh: time.Duration(0),
101+ SetXAuthRequest: false,
100102 PassBasicAuth: true,
101103 PassUserHeaders: true,
102104 PassAccessToken: false,