Skip to content
Snippets Groups Projects
Commit 45207a56 authored by Frederic BIDON's avatar Frederic BIDON Committed by Bruno Oliveira da Silva
Browse files

[KEYCLOAK-9786] Secure token and logout endpoint


* now token validity is checked to reach those endpoints, even though a valid cookie is presented
* previous BadRequest responses on malformed tokens now yield Unauthorized

Signed-off-by: default avatarFrederic BIDON <frederic@oneconcern.com>
parent 6f6e25d2
No related branches found
No related tags found
No related merge requests found
...@@ -138,7 +138,10 @@ func TestLoginHandler(t *testing.T) { ...@@ -138,7 +138,10 @@ func TestLoginHandler(t *testing.T) {
func TestLogoutHandlerBadRequest(t *testing.T) { func TestLogoutHandlerBadRequest(t *testing.T) {
requests := []fakeRequest{ requests := []fakeRequest{
{URI: newFakeKeycloakConfig().WithOAuthURI(logoutURL), ExpectedCode: http.StatusBadRequest}, {
URI: newFakeKeycloakConfig().WithOAuthURI(logoutURL),
ExpectedCode: http.StatusUnauthorized,
},
} }
newFakeProxy(nil).RunTests(t, requests) newFakeProxy(nil).RunTests(t, requests)
} }
...@@ -148,18 +151,18 @@ func TestLogoutHandlerBadToken(t *testing.T) { ...@@ -148,18 +151,18 @@ func TestLogoutHandlerBadToken(t *testing.T) {
requests := []fakeRequest{ requests := []fakeRequest{
{ {
URI: c.WithOAuthURI(logoutURL), URI: c.WithOAuthURI(logoutURL),
ExpectedCode: http.StatusBadRequest, ExpectedCode: http.StatusUnauthorized,
}, },
{ {
URI: c.WithOAuthURI(logoutURL), URI: c.WithOAuthURI(logoutURL),
HasCookieToken: true, HasCookieToken: true,
RawToken: "this.is.a.bad.token", RawToken: "this.is.a.bad.token",
ExpectedCode: http.StatusBadRequest, ExpectedCode: http.StatusUnauthorized,
}, },
{ {
URI: c.WithOAuthURI(logoutURL), URI: c.WithOAuthURI(logoutURL),
RawToken: "this.is.a.bad.token", RawToken: "this.is.a.bad.token",
ExpectedCode: http.StatusBadRequest, ExpectedCode: http.StatusUnauthorized,
}, },
} }
newFakeProxy(nil).RunTests(t, requests) newFakeProxy(nil).RunTests(t, requests)
...@@ -185,20 +188,22 @@ func TestLogoutHandlerGood(t *testing.T) { ...@@ -185,20 +188,22 @@ func TestLogoutHandlerGood(t *testing.T) {
func TestTokenHandler(t *testing.T) { func TestTokenHandler(t *testing.T) {
uri := newFakeKeycloakConfig().WithOAuthURI(tokenURL) uri := newFakeKeycloakConfig().WithOAuthURI(tokenURL)
goodToken := newTestToken("example").getToken()
requests := []fakeRequest{ requests := []fakeRequest{
{ {
URI: uri, URI: uri,
HasToken: true, HasToken: true,
RawToken: (&goodToken).Encode(),
ExpectedCode: http.StatusOK, ExpectedCode: http.StatusOK,
}, },
{ {
URI: uri, URI: uri,
ExpectedCode: http.StatusBadRequest, ExpectedCode: http.StatusUnauthorized,
}, },
{ {
URI: uri, URI: uri,
RawToken: "niothing", RawToken: "niothing",
ExpectedCode: http.StatusBadRequest, ExpectedCode: http.StatusUnauthorized,
}, },
{ {
URI: uri, URI: uri,
......
...@@ -98,7 +98,7 @@ func (r *oauthProxy) loggingMiddleware(next http.Handler) http.Handler { ...@@ -98,7 +98,7 @@ func (r *oauthProxy) loggingMiddleware(next http.Handler) http.Handler {
} }
// authenticationMiddleware is responsible for verifying the access token // authenticationMiddleware is responsible for verifying the access token
func (r *oauthProxy) authenticationMiddleware(resource *Resource) func(http.Handler) http.Handler { func (r *oauthProxy) authenticationMiddleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
clientIP := req.RemoteAddr clientIP := req.RemoteAddr
......
...@@ -204,8 +204,8 @@ func (r *oauthProxy) createReverseProxy() error { ...@@ -204,8 +204,8 @@ func (r *oauthProxy) createReverseProxy() error {
e.Get(callbackURL, r.oauthCallbackHandler) e.Get(callbackURL, r.oauthCallbackHandler)
e.Get(expiredURL, r.expirationHandler) e.Get(expiredURL, r.expirationHandler)
e.Get(healthURL, r.healthHandler) e.Get(healthURL, r.healthHandler)
e.Get(logoutURL, r.logoutHandler) e.With(r.authenticationMiddleware()).Get(logoutURL, r.logoutHandler)
e.Get(tokenURL, r.tokenHandler) e.With(r.authenticationMiddleware()).Get(tokenURL, r.tokenHandler)
e.Post(loginURL, r.loginHandler) e.Post(loginURL, r.loginHandler)
if r.config.EnableMetrics { if r.config.EnableMetrics {
r.log.Info("enabled the service metrics middleware", zap.String("path", r.config.WithOAuthURI(metricsURL))) r.log.Info("enabled the service metrics middleware", zap.String("path", r.config.WithOAuthURI(metricsURL)))
...@@ -260,7 +260,7 @@ func (r *oauthProxy) createReverseProxy() error { ...@@ -260,7 +260,7 @@ func (r *oauthProxy) createReverseProxy() error {
for _, x := range r.config.Resources { for _, x := range r.config.Resources {
r.log.Info("protecting resource", zap.String("resource", x.String())) r.log.Info("protecting resource", zap.String("resource", x.String()))
e := engine.With( e := engine.With(
r.authenticationMiddleware(x), r.authenticationMiddleware(),
r.admissionMiddleware(x), r.admissionMiddleware(x),
r.identityHeadersMiddleware(r.config.AddClaims)) r.identityHeadersMiddleware(r.config.AddClaims))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment