git.iko.soy/typeable/oauth2-proxy
on master help sign in
Add set basic auth param (#413)

* addint redirect capability to sign_out * updating changelog * Add a new param to set the Authorization header to up-stream systems as Basic user:password * Resolving code review * mutual exclusiv changes for Basic and Bearer Authorization header * Fixed the merge mixup and comment error * Updated changelog and fixed typo * Adding the new entry in changelog Co-authored-by: Costel Moraru <costel.moraru-germany@ibm.com>

commitb0b87563dcc51f03bf666e7ccd8628c9ae73193c authorMoraru Costel <moraru_costel@yahoo.com> dateFri 10 Apr 2020, 15:41 (7 years ago) parent7efc162
6 files changed+85
CHANGELOG.md+1 docs/configuration/configuration.md+1 main.go+1 oauthproxy.go+10 oauthproxy_test.go+66 options.go+6
CHANGELOG.md+1
@@ -27,6 +27,7 @@
2727- [#432](https://github.com/oauth2-proxy/oauth2-proxy/pull/432) Update ruby dependencies for documentation (@theobarberbany)
2828- [#471](https://github.com/oauth2-proxy/oauth2-proxy/pull/471) Add logging in case of invalid redirects (@gargath)
2929- [#462](https://github.com/oauth2-proxy/oauth2-proxy/pull/462) Allow HTML in banner message (@eritikass).
30+- [#413](https://github.com/oauth2-proxy/oauth2-proxy/pull/413) Add -set-basic-auth param to set the Basic Authorization header for upstreams (@morarucostel).
3031
3132# v5.1.0
3233
docs/configuration/configuration.md+1
@@ -103,6 +103,7 @@ An example [oauth2-proxy.cfg]({{ site.gitweb }}/contrib/oauth2-proxy.cfg.example
103103| `-session-store-type` | string | [Session data storage backend](configuration/sessions); redis or cookie | cookie |
104104| `-set-xauthrequest` | bool | set X-Auth-Request-User, X-Auth-Request-Email and X-Auth-Request-Preferred-Username response headers (useful in Nginx auth_request mode) | false |
105105| `-set-authorization-header` | bool | set Authorization Bearer response header (useful in Nginx auth_request mode) | false |
106+| `-set-basic-auth` | bool | set HTTP Basic Auth information in response (useful in Nginx auth_request mode) | true |
106107| `-signature-key` | string | GAP-Signature request signature key (algorithm:secretkey) | |
107108| `-silence-ping-logging` | bool | disable logging of requests to ping endpoint | false |
108109| `-skip-auth-preflight` | bool | will skip authentication for OPTIONS requests | false |
main.go+1
@@ -43,6 +43,7 @@ func main() {
4343 flagSet.Bool("set-xauthrequest", false, "set X-Auth-Request-User and X-Auth-Request-Email response headers (useful in Nginx auth_request mode)")
4444 flagSet.Var(&upstreams, "upstream", "the http url(s) of the upstream endpoint, file:// paths for static files or static://<status_code> for static response. Routing is based on the path")
4545 flagSet.Bool("pass-basic-auth", true, "pass HTTP Basic Auth, X-Forwarded-User and X-Forwarded-Email information to upstream")
46+ flagSet.Bool("set-basic-auth", true, "set HTTP Basic Auth information in response (useful in Nginx auth_request mode)")
4647 flagSet.Bool("prefer-email-to-user", false, "Prefer to use the Email address as the Username when passing information to upstream. Will only use Username if Email is unavailable, eg. htaccess authentication. Used in conjunction with -pass-basic-auth and -pass-user-headers")
4748 flagSet.Bool("pass-user-headers", true, "pass X-Forwarded-User and X-Forwarded-Email information to upstream")
4849 flagSet.String("basic-auth-password", "", "the password to set when passing the HTTP Basic Auth header")
oauthproxy.go+10
@@ -94,6 +94,7 @@ type OAuthProxy struct {
9494 serveMux http.Handler
9595 SetXAuthRequest bool
9696 PassBasicAuth bool
97+ SetBasicAuth bool
9798 SkipProviderButton bool
9899 PassUserHeaders bool
99100 BasicAuthPassword string
@@ -302,6 +303,7 @@ func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
302303 compiledRegex: opts.CompiledRegex,
303304 SetXAuthRequest: opts.SetXAuthRequest,
304305 PassBasicAuth: opts.PassBasicAuth,
306+ SetBasicAuth: opts.SetBasicAuth,
305307 PassUserHeaders: opts.PassUserHeaders,
306308 BasicAuthPassword: opts.BasicAuthPassword,
307309 PassAccessToken: opts.PassAccessToken,
@@ -1037,6 +1039,14 @@ func (p *OAuthProxy) addHeadersForProxying(rw http.ResponseWriter, req *http.Req
10371039 req.Header.Del("Authorization")
10381040 }
10391041 }
1042+ if p.SetBasicAuth {
1043+ if session.User != "" {
1044+ authVal := b64.StdEncoding.EncodeToString([]byte(session.User + ":" + p.BasicAuthPassword))
1045+ rw.Header().Set("Authorization", "Basic "+authVal)
1046+ } else {
1047+ rw.Header().Del("Authorization")
1048+ }
1049+ }
10401050 if p.SetAuthorization {
10411051 if session.IDToken != "" {
10421052 rw.Header().Set("Authorization", fmt.Sprintf("Bearer %s", session.IDToken))
oauthproxy_test.go+66
@@ -404,6 +404,7 @@ func TestBasicAuthPassword(t *testing.T) {
404404 opts.ClientSecret = "alkgret"
405405 opts.CookieSecure = false
406406 opts.PassBasicAuth = true
407+ opts.SetBasicAuth = true
407408 opts.PassUserHeaders = true
408409 opts.PreferEmailToUser = true
409410 opts.BasicAuthPassword = "This is a secure password"
@@ -1075,6 +1076,71 @@ func TestAuthOnlyEndpointSetXAuthRequestHeaders(t *testing.T) {
10751076 assert.Equal(t, "oauth_user@example.com", pcTest.rw.HeaderMap["X-Auth-Request-Email"][0])
10761077}
10771078
1079+func TestAuthOnlyEndpointSetBasicAuthTrueRequestHeaders(t *testing.T) {
1080+ var pcTest ProcessCookieTest
1081+
1082+ pcTest.opts = NewOptions()
1083+ pcTest.opts.SetXAuthRequest = true
1084+ pcTest.opts.SetBasicAuth = true
1085+ pcTest.opts.Validate()
1086+
1087+ pcTest.proxy = NewOAuthProxy(pcTest.opts, func(email string) bool {
1088+ return pcTest.validateUser
1089+ })
1090+ pcTest.proxy.provider = &TestProvider{
1091+ ValidToken: true,
1092+ }
1093+
1094+ pcTest.validateUser = true
1095+
1096+ pcTest.rw = httptest.NewRecorder()
1097+ pcTest.req, _ = http.NewRequest("GET",
1098+ pcTest.opts.ProxyPrefix+"/auth", nil)
1099+
1100+ startSession := &sessions.SessionState{
1101+ User: "oauth_user", Email: "oauth_user@example.com", AccessToken: "oauth_token", CreatedAt: time.Now()}
1102+ pcTest.SaveSession(startSession)
1103+
1104+ pcTest.proxy.ServeHTTP(pcTest.rw, pcTest.req)
1105+ assert.Equal(t, http.StatusAccepted, pcTest.rw.Code)
1106+ assert.Equal(t, "oauth_user", pcTest.rw.HeaderMap["X-Auth-Request-User"][0])
1107+ assert.Equal(t, "oauth_user@example.com", pcTest.rw.HeaderMap["X-Auth-Request-Email"][0])
1108+ expectedHeader := "Basic " + base64.StdEncoding.EncodeToString([]byte("oauth_user:"+pcTest.opts.BasicAuthPassword))
1109+ assert.Equal(t, expectedHeader, pcTest.rw.HeaderMap["Authorization"][0])
1110+}
1111+
1112+func TestAuthOnlyEndpointSetBasicAuthFalseRequestHeaders(t *testing.T) {
1113+ var pcTest ProcessCookieTest
1114+
1115+ pcTest.opts = NewOptions()
1116+ pcTest.opts.SetXAuthRequest = true
1117+ pcTest.opts.SetBasicAuth = false
1118+ pcTest.opts.Validate()
1119+
1120+ pcTest.proxy = NewOAuthProxy(pcTest.opts, func(email string) bool {
1121+ return pcTest.validateUser
1122+ })
1123+ pcTest.proxy.provider = &TestProvider{
1124+ ValidToken: true,
1125+ }
1126+
1127+ pcTest.validateUser = true
1128+
1129+ pcTest.rw = httptest.NewRecorder()
1130+ pcTest.req, _ = http.NewRequest("GET",
1131+ pcTest.opts.ProxyPrefix+"/auth", nil)
1132+
1133+ startSession := &sessions.SessionState{
1134+ User: "oauth_user", Email: "oauth_user@example.com", AccessToken: "oauth_token", CreatedAt: time.Now()}
1135+ pcTest.SaveSession(startSession)
1136+
1137+ pcTest.proxy.ServeHTTP(pcTest.rw, pcTest.req)
1138+ assert.Equal(t, http.StatusAccepted, pcTest.rw.Code)
1139+ assert.Equal(t, "oauth_user", pcTest.rw.HeaderMap["X-Auth-Request-User"][0])
1140+ assert.Equal(t, "oauth_user@example.com", pcTest.rw.HeaderMap["X-Auth-Request-Email"][0])
1141+ assert.Equal(t, 0, len(pcTest.rw.HeaderMap["Authorization"]), "should not have Authorization header entries")
1142+}
1143+
10781144func TestAuthSkippedForPreflightRequests(t *testing.T) {
10791145 upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
10801146 w.WriteHeader(200)
options.go+6
@@ -73,6 +73,7 @@ type Options struct {
7373 SkipJwtBearerTokens bool `flag:"skip-jwt-bearer-tokens" cfg:"skip_jwt_bearer_tokens" env:"OAUTH2_PROXY_SKIP_JWT_BEARER_TOKENS"`
7474 ExtraJwtIssuers []string `flag:"extra-jwt-issuers" cfg:"extra_jwt_issuers" env:"OAUTH2_PROXY_EXTRA_JWT_ISSUERS"`
7575 PassBasicAuth bool `flag:"pass-basic-auth" cfg:"pass_basic_auth" env:"OAUTH2_PROXY_PASS_BASIC_AUTH"`
76+ SetBasicAuth bool `flag:"set-basic-auth" cfg:"set_basic_auth" env:"OAUTH2_PROXY_SET_BASIC_AUTH"`
7677 PreferEmailToUser bool `flag:"prefer-email-to-user" cfg:"prefer_email_to_user" env:"OAUTH2_PROXY_PREFER_EMAIL_TO_USER"`
7778 BasicAuthPassword string `flag:"basic-auth-password" cfg:"basic_auth_password" env:"OAUTH2_PROXY_BASIC_AUTH_PASSWORD"`
7879 PassAccessToken bool `flag:"pass-access-token" cfg:"pass_access_token" env:"OAUTH2_PROXY_PASS_ACCESS_TOKEN"`
@@ -166,6 +167,7 @@ func NewOptions() *Options {
166167 SetXAuthRequest: false,
167168 SkipAuthPreflight: false,
168169 PassBasicAuth: true,
170+ SetBasicAuth: false,
169171 PassUserHeaders: true,
170172 PassAccessToken: false,
171173 PassHostHeader: true,
@@ -243,6 +245,10 @@ func (o *Options) Validate() error {
243245 "\n use email-domain=* to authorize all email addresses")
244246 }
245247
248+ if o.SetBasicAuth && o.SetAuthorization {
249+ msgs = append(msgs, "mutually exclusive: set-basic-auth and set-authorization-header can not both be true")
250+ }
251+
246252 if o.OIDCIssuerURL != "" {
247253
248254 ctx := context.Background()