forked from spring-projects/spring-authorization-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reusable default authentication failure handler
Fixes spring-projectsgh-1369
- Loading branch information
Showing
6 changed files
with
131 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...uth2/server/authorization/web/authentication/OAuth2ErrorAuthenticationFailureHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2020-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.security.oauth2.server.authorization.web.authentication; | ||
|
||
import java.io.IOException; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.http.server.ServletServerHttpResponse; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.OAuth2Error; | ||
import org.springframework.security.oauth2.core.http.converter.OAuth2ErrorHttpMessageConverter; | ||
import org.springframework.security.web.authentication.AuthenticationFailureHandler; | ||
|
||
/** | ||
* A default implementation of an {@link AuthenticationFailureHandler} used for handling an {@link OAuth2AuthenticationException} | ||
* and returning the {@link OAuth2Error Error Response}. | ||
* | ||
* @author Dmitriy Dubson | ||
* @since 1.2.0 | ||
* @see AuthenticationFailureHandler | ||
*/ | ||
public final class OAuth2ErrorAuthenticationFailureHandler implements AuthenticationFailureHandler { | ||
private HttpMessageConverter<OAuth2Error> errorHttpResponseConverter = new OAuth2ErrorHttpMessageConverter(); | ||
|
||
@Override | ||
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { | ||
OAuth2Error error = ((OAuth2AuthenticationException) exception).getError(); | ||
ServletServerHttpResponse httpResponse = new ServletServerHttpResponse(response); | ||
httpResponse.setStatusCode(HttpStatus.BAD_REQUEST); | ||
this.errorHttpResponseConverter.write(error, null, httpResponse); | ||
} | ||
|
||
/** | ||
* Sets OAuth error HTTP message converter to write to upon authentication failure | ||
* @param errorHttpResponseConverter the error HTTP message converter to set | ||
*/ | ||
public void setErrorHttpResponseConverter(HttpMessageConverter<OAuth2Error> errorHttpResponseConverter) { | ||
this.errorHttpResponseConverter = errorHttpResponseConverter; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...server/authorization/web/authentication/OAuth2ErrorAuthenticationFailureHandlerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2020-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.security.oauth2.server.authorization.web.authentication; | ||
|
||
import java.io.IOException; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.http.server.ServletServerHttpResponse; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.OAuth2Error; | ||
import org.springframework.security.oauth2.core.OAuth2ErrorCodes; | ||
import org.springframework.security.web.authentication.AuthenticationFailureHandler; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.ArgumentMatchers.isNull; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
/** | ||
* Tests for {@link OAuth2ErrorAuthenticationFailureHandler} | ||
* | ||
* @author Dmitriy Dubson | ||
*/ | ||
public class OAuth2ErrorAuthenticationFailureHandlerTests { | ||
|
||
@Test | ||
@SuppressWarnings("unchecked") | ||
public void onAuthenticationFailure() throws IOException, ServletException { | ||
HttpMessageConverter<OAuth2Error> errorHttpMessageConverter = (HttpMessageConverter<OAuth2Error>) mock(HttpMessageConverter.class); | ||
HttpServletRequest mockRequest = mock(HttpServletRequest.class); | ||
HttpServletResponse mockResponse = mock(HttpServletResponse.class); | ||
|
||
OAuth2Error invalidRequestError = new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST); | ||
AuthenticationException authenticationException = new OAuth2AuthenticationException(invalidRequestError); | ||
AuthenticationFailureHandler handler = new OAuth2ErrorAuthenticationFailureHandler(); | ||
|
||
handler.onAuthenticationFailure(mockRequest, mockResponse, authenticationException); | ||
|
||
verify(errorHttpMessageConverter).write(eq(invalidRequestError), isNull(), any(ServletServerHttpResponse.class)); | ||
verify(mockResponse).setStatus(eq(HttpStatus.BAD_REQUEST.value())); | ||
} | ||
|
||
} |