use of org.springframework.security.AuthenticationException in project gocd by gocd.
the class OauthAuthenticationFilter method doFilterHttp.
protected void doFilterHttp(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
//Token token="ACCESS_TOKEN"
String header = request.getHeader(AUTHORIZATION);
if (header != null) {
if (logger.isDebugEnabled()) {
logger.debug("Oauth authorization header: " + header);
}
Matcher matcher = OAUTH_TOKEN_PATTERN.matcher(header);
if (matcher.matches()) {
String token = matcher.group(1);
OauthAuthenticationToken authenticationToken = new OauthAuthenticationToken(token);
try {
Authentication authResult = authenticationManager.authenticate(authenticationToken);
SecurityContextHolder.getContext().setAuthentication(authResult);
} catch (AuthenticationException e) {
if (logger.isDebugEnabled()) {
logger.debug("Oauth authentication request for token: " + token, e);
}
SecurityContextHolder.getContext().setAuthentication(null);
}
}
}
chain.doFilter(request, response);
}
use of org.springframework.security.AuthenticationException in project gocd by gocd.
the class GoExceptionTranslationFilterTest method setUp.
@Before
public void setUp() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
filterChain = mock(FilterChain.class);
authenticationException = mock(AuthenticationException.class);
basicAuth = mock(BasicProcessingFilterEntryPoint.class);
cruiseLoginFormAuth = mock(AuthenticationEntryPoint.class);
securityService = mock(SecurityService.class);
filter = new GoExceptionTranslationFilter();
filter.setUrlPatternsThatShouldNotBeRedirectedToAfterLogin("(\\.json)|(/images/)");
filter.setAuthenticationEntryPoint(cruiseLoginFormAuth);
filter.setBasicAuthenticationEntryPoint(basicAuth);
filter.setSecurityService(securityService);
}
use of org.springframework.security.AuthenticationException in project gocd by gocd.
the class BasicAuthenticationFilterTest method shouldConvey_itsBasicProcessingFilter.
@Test
public void shouldConvey_itsBasicProcessingFilter() throws IOException, ServletException {
BasicAuthenticationFilter filter = new BasicAuthenticationFilter(localizer);
final Boolean[] hadBasicMarkOnInsideAuthenticationManager = new Boolean[] { false };
filter.setAuthenticationManager(new AuthenticationManager() {
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
hadBasicMarkOnInsideAuthenticationManager[0] = BasicAuthenticationFilter.isProcessingBasicAuth();
return new UsernamePasswordAuthenticationToken("school-principal", "u can be principal if you know this!");
}
});
assertThat(BasicAuthenticationFilter.isProcessingBasicAuth(), is(false));
MockHttpServletRequest httpRequest = new MockHttpServletRequest();
httpRequest.addHeader("Authorization", "Basic " + java.util.Base64.getEncoder().encodeToString("loser:boozer".getBytes()));
filter.doFilterHttp(httpRequest, new MockHttpServletResponse(), new FilterChain() {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
}
});
assertThat(BasicAuthenticationFilter.isProcessingBasicAuth(), is(false));
assertThat(hadBasicMarkOnInsideAuthenticationManager[0], is(true));
}
use of org.springframework.security.AuthenticationException in project gocd by gocd.
the class AuthenticationProcessingFilter method onUnsuccessfulAuthentication.
@Override
protected void onUnsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException {
super.onUnsuccessfulAuthentication(request, response, failed);
if (failed.getClass() == AuthenticationServiceException.class) {
request.getSession().setAttribute(SPRING_SECURITY_LAST_EXCEPTION_KEY, new Exception(localizer.localize("AUTHENTICATION_SERVICE_EXCEPTION")));
LOGGER.error(failed.getMessage());
LOGGER.trace(failed.getMessage(), failed);
}
}
use of org.springframework.security.AuthenticationException in project gocd by gocd.
the class OauthAuthenticationProviderTest method shouldRaiseAuthenticationExceptionWhenNoMatchForTokenExists.
@Test
public void shouldRaiseAuthenticationExceptionWhenNoMatchForTokenExists() {
when(dataSource.findOauthTokenByAccessToken("token-string")).thenReturn(null);
try {
provider.authenticate(new OauthAuthenticationToken("token-string"));
fail("should have thrown an AuthenticationException");
} catch (AuthenticationException e) {
assertThat(e.getMessage(), is("No match for OAuth token: token-string"));
}
}
Aggregations