Skip to content

Commit 45207a5

Browse files
fredbiBruno Oliveira da Silva
authored andcommitted
[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: Frederic BIDON <[email protected]>
1 parent 6f6e25d commit 45207a5

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

handlers_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ func TestLoginHandler(t *testing.T) {
138138

139139
func TestLogoutHandlerBadRequest(t *testing.T) {
140140
requests := []fakeRequest{
141-
{URI: newFakeKeycloakConfig().WithOAuthURI(logoutURL), ExpectedCode: http.StatusBadRequest},
141+
{
142+
URI: newFakeKeycloakConfig().WithOAuthURI(logoutURL),
143+
ExpectedCode: http.StatusUnauthorized,
144+
},
142145
}
143146
newFakeProxy(nil).RunTests(t, requests)
144147
}
@@ -148,18 +151,18 @@ func TestLogoutHandlerBadToken(t *testing.T) {
148151
requests := []fakeRequest{
149152
{
150153
URI: c.WithOAuthURI(logoutURL),
151-
ExpectedCode: http.StatusBadRequest,
154+
ExpectedCode: http.StatusUnauthorized,
152155
},
153156
{
154157
URI: c.WithOAuthURI(logoutURL),
155158
HasCookieToken: true,
156159
RawToken: "this.is.a.bad.token",
157-
ExpectedCode: http.StatusBadRequest,
160+
ExpectedCode: http.StatusUnauthorized,
158161
},
159162
{
160163
URI: c.WithOAuthURI(logoutURL),
161164
RawToken: "this.is.a.bad.token",
162-
ExpectedCode: http.StatusBadRequest,
165+
ExpectedCode: http.StatusUnauthorized,
163166
},
164167
}
165168
newFakeProxy(nil).RunTests(t, requests)
@@ -185,20 +188,22 @@ func TestLogoutHandlerGood(t *testing.T) {
185188

186189
func TestTokenHandler(t *testing.T) {
187190
uri := newFakeKeycloakConfig().WithOAuthURI(tokenURL)
191+
goodToken := newTestToken("example").getToken()
188192
requests := []fakeRequest{
189193
{
190194
URI: uri,
191195
HasToken: true,
196+
RawToken: (&goodToken).Encode(),
192197
ExpectedCode: http.StatusOK,
193198
},
194199
{
195200
URI: uri,
196-
ExpectedCode: http.StatusBadRequest,
201+
ExpectedCode: http.StatusUnauthorized,
197202
},
198203
{
199204
URI: uri,
200205
RawToken: "niothing",
201-
ExpectedCode: http.StatusBadRequest,
206+
ExpectedCode: http.StatusUnauthorized,
202207
},
203208
{
204209
URI: uri,

middleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (r *oauthProxy) loggingMiddleware(next http.Handler) http.Handler {
9898
}
9999

100100
// authenticationMiddleware is responsible for verifying the access token
101-
func (r *oauthProxy) authenticationMiddleware(resource *Resource) func(http.Handler) http.Handler {
101+
func (r *oauthProxy) authenticationMiddleware() func(http.Handler) http.Handler {
102102
return func(next http.Handler) http.Handler {
103103
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
104104
clientIP := req.RemoteAddr

server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ func (r *oauthProxy) createReverseProxy() error {
204204
e.Get(callbackURL, r.oauthCallbackHandler)
205205
e.Get(expiredURL, r.expirationHandler)
206206
e.Get(healthURL, r.healthHandler)
207-
e.Get(logoutURL, r.logoutHandler)
208-
e.Get(tokenURL, r.tokenHandler)
207+
e.With(r.authenticationMiddleware()).Get(logoutURL, r.logoutHandler)
208+
e.With(r.authenticationMiddleware()).Get(tokenURL, r.tokenHandler)
209209
e.Post(loginURL, r.loginHandler)
210210
if r.config.EnableMetrics {
211211
r.log.Info("enabled the service metrics middleware", zap.String("path", r.config.WithOAuthURI(metricsURL)))
@@ -260,7 +260,7 @@ func (r *oauthProxy) createReverseProxy() error {
260260
for _, x := range r.config.Resources {
261261
r.log.Info("protecting resource", zap.String("resource", x.String()))
262262
e := engine.With(
263-
r.authenticationMiddleware(x),
263+
r.authenticationMiddleware(),
264264
r.admissionMiddleware(x),
265265
r.identityHeadersMiddleware(r.config.AddClaims))
266266

0 commit comments

Comments
 (0)