Skip to content
Snippets Groups Projects
Commit cdfb2a93 authored by Philippe Gagnon's avatar Philippe Gagnon Committed by Bruno Oliveira da Silva
Browse files

Add base URI to OAuth routes and set cookies path to Base URI

parent 2d9c8fdc
No related branches found
No related tags found
No related merge requests found
...@@ -32,11 +32,15 @@ func (r *oauthProxy) dropCookie(w http.ResponseWriter, host, name, value string, ...@@ -32,11 +32,15 @@ func (r *oauthProxy) dropCookie(w http.ResponseWriter, host, name, value string,
if r.config.CookieDomain != "" { if r.config.CookieDomain != "" {
domain = r.config.CookieDomain domain = r.config.CookieDomain
} }
path := r.config.BaseURI
if path == "" {
path = "/"
}
cookie := &http.Cookie{ cookie := &http.Cookie{
Domain: domain, Domain: domain,
HttpOnly: r.config.HTTPOnlyCookie, HttpOnly: r.config.HTTPOnlyCookie,
Name: name, Name: name,
Path: "/", Path: path,
Secure: r.config.SecureCookie, Secure: r.config.SecureCookie,
Value: value, Value: value,
} }
......
...@@ -40,6 +40,45 @@ func TestCookieDomainHostHeader(t *testing.T) { ...@@ -40,6 +40,45 @@ func TestCookieDomainHostHeader(t *testing.T) {
assert.Equal(t, cookie.Domain, "127.0.0.1") assert.Equal(t, cookie.Domain, "127.0.0.1")
} }
func TestCookieBasePath(t *testing.T) {
cfg := newFakeKeycloakConfig()
cfg.BaseURI = "/base-uri"
_, _, svc := newTestProxyService(cfg)
resp, err := makeTestCodeFlowLogin(svc + "/admin")
assert.NoError(t, err)
assert.NotNil(t, resp)
var cookie *http.Cookie
for _, c := range resp.Cookies() {
if c.Name == "kc-access" {
cookie = c
}
}
assert.NotNil(t, cookie)
assert.Equal(t, "/base-uri", cookie.Path)
}
func TestCookieWithoutBasePath(t *testing.T) {
cfg := newFakeKeycloakConfig()
_, _, svc := newTestProxyService(cfg)
resp, err := makeTestCodeFlowLogin(svc + "/admin")
assert.NoError(t, err)
assert.NotNil(t, resp)
var cookie *http.Cookie
for _, c := range resp.Cookies() {
if c.Name == "kc-access" {
cookie = c
}
}
assert.NotNil(t, cookie)
assert.Equal(t, "/", cookie.Path)
}
func TestCookieDomain(t *testing.T) { func TestCookieDomain(t *testing.T) {
p, _, svc := newTestProxyService(nil) p, _, svc := newTestProxyService(nil)
p.config.CookieDomain = "domain.com" p.config.CookieDomain = "domain.com"
......
...@@ -210,10 +210,6 @@ func (r *oauthProxy) oauthCallbackHandler(w http.ResponseWriter, req *http.Reque ...@@ -210,10 +210,6 @@ func (r *oauthProxy) oauthCallbackHandler(w http.ResponseWriter, req *http.Reque
redirectURI = string(decoded) redirectURI = string(decoded)
} }
} }
if r.config.BaseURI != "" {
// assuming state starts with slash
redirectURI = r.config.BaseURI + redirectURI
}
r.redirectToURL(redirectURI, w, req, http.StatusTemporaryRedirect) r.redirectToURL(redirectURI, w, req, http.StatusTemporaryRedirect)
} }
......
...@@ -354,6 +354,44 @@ func TestOauthRequests(t *testing.T) { ...@@ -354,6 +354,44 @@ func TestOauthRequests(t *testing.T) {
newFakeProxy(cfg).RunTests(t, requests) newFakeProxy(cfg).RunTests(t, requests)
} }
func TestOauthRequestsWithBaseURI(t *testing.T) {
cfg := newFakeKeycloakConfig()
cfg.BaseURI = "/base-uri"
requests := []fakeRequest{
{
URI: "/base-uri/oauth/authorize",
Redirects: true,
ExpectedCode: http.StatusTemporaryRedirect,
},
{
URI: "/base-uri/oauth/callback",
Redirects: true,
ExpectedCode: http.StatusBadRequest,
},
{
URI: "/base-uri/oauth/health",
Redirects: true,
ExpectedCode: http.StatusOK,
},
{
URI: "/oauth/authorize",
ExpectedProxy: true,
ExpectedCode: http.StatusOK,
},
{
URI: "/oauth/callback",
ExpectedProxy: true,
ExpectedCode: http.StatusOK,
},
{
URI: "/oauth/health",
ExpectedProxy: true,
ExpectedCode: http.StatusOK,
},
}
newFakeProxy(cfg).RunTests(t, requests)
}
func TestMethodExclusions(t *testing.T) { func TestMethodExclusions(t *testing.T) {
cfg := newFakeKeycloakConfig() cfg := newFakeKeycloakConfig()
cfg.Resources = []*Resource{ cfg.Resources = []*Resource{
......
...@@ -197,7 +197,7 @@ func (r *oauthProxy) createReverseProxy() error { ...@@ -197,7 +197,7 @@ func (r *oauthProxy) createReverseProxy() error {
} }
// step: add the routing for oauth // step: add the routing for oauth
engine.With(proxyDenyMiddleware).Route(r.config.OAuthURI, func(e chi.Router) { engine.With(proxyDenyMiddleware).Route(r.config.BaseURI+r.config.OAuthURI, func(e chi.Router) {
e.MethodNotAllowed(methodNotAllowHandlder) e.MethodNotAllowed(methodNotAllowHandlder)
e.HandleFunc(authorizationURL, r.oauthAuthorizationHandler) e.HandleFunc(authorizationURL, r.oauthAuthorizationHandler)
e.Get(callbackURL, r.oauthCallbackHandler) e.Get(callbackURL, r.oauthCallbackHandler)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment