Search in sources :

Example 21 with AuthenticationManager

use of org.springframework.security.authentication.AuthenticationManager in project spring-security by spring-projects.

the class RemoteAuthenticationManagerImplTests method testSuccessfulAuthentication.

@Test
public void testSuccessfulAuthentication() {
    RemoteAuthenticationManagerImpl manager = new RemoteAuthenticationManagerImpl();
    AuthenticationManager am = mock(AuthenticationManager.class);
    when(am.authenticate(any(Authentication.class))).thenReturn(new TestingAuthenticationToken("u", "p", "A"));
    manager.setAuthenticationManager(am);
    manager.attemptAuthentication("rod", "password");
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) Authentication(org.springframework.security.core.Authentication) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.Test)

Example 22 with AuthenticationManager

use of org.springframework.security.authentication.AuthenticationManager in project spring-security by spring-projects.

the class UsernamePasswordAuthenticationFilterTests method createAuthenticationManager.

private AuthenticationManager createAuthenticationManager() {
    AuthenticationManager am = mock(AuthenticationManager.class);
    when(am.authenticate(any(Authentication.class))).thenAnswer(new Answer<Authentication>() {

        public Authentication answer(InvocationOnMock invocation) throws Throwable {
            return (Authentication) invocation.getArguments()[0];
        }
    });
    return am;
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) Authentication(org.springframework.security.core.Authentication) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 23 with AuthenticationManager

use of org.springframework.security.authentication.AuthenticationManager in project spring-security-oauth by spring-projects.

the class DefaultTokenServicesWithInMemoryTests method testRefreshTokenWithUnauthenticatedUser.

@Test
public void testRefreshTokenWithUnauthenticatedUser() throws Exception {
    OAuth2Authentication expectedAuthentication = new OAuth2Authentication(RequestTokenFactory.createOAuth2Request("id", false, Collections.singleton("read")), new TestAuthentication("test2", false));
    getTokenServices().setAuthenticationManager(new AuthenticationManager() {

        @Override
        public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            throw new AccountExpiredException("Not valid");
        }
    });
    DefaultOAuth2AccessToken firstAccessToken = (DefaultOAuth2AccessToken) getTokenServices().createAccessToken(expectedAuthentication);
    assertNotNull(firstAccessToken.getRefreshToken());
    expected.expect(AccountExpiredException.class);
    TokenRequest tokenRequest = new TokenRequest(Collections.singletonMap("client_id", "id"), "id", null, null);
    getTokenServices().refreshAccessToken(firstAccessToken.getRefreshToken().getValue(), tokenRequest);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) AuthenticationException(org.springframework.security.core.AuthenticationException) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) AccountExpiredException(org.springframework.security.authentication.AccountExpiredException) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) TokenRequest(org.springframework.security.oauth2.provider.TokenRequest) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test)

Example 24 with AuthenticationManager

use of org.springframework.security.authentication.AuthenticationManager in project spring-security by spring-projects.

the class CasAuthenticationFilterTests method testDoFilterAuthenticateAll.

@Test
public void testDoFilterAuthenticateAll() throws Exception {
    AuthenticationSuccessHandler successHandler = mock(AuthenticationSuccessHandler.class);
    AuthenticationManager manager = mock(AuthenticationManager.class);
    Authentication authentication = new TestingAuthenticationToken("un", "pwd", "ROLE_USER");
    when(manager.authenticate(any(Authentication.class))).thenReturn(authentication);
    ServiceProperties serviceProperties = new ServiceProperties();
    serviceProperties.setAuthenticateAllArtifacts(true);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setParameter("ticket", "ST-1-123");
    request.setServletPath("/authenticate");
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain chain = mock(FilterChain.class);
    CasAuthenticationFilter filter = new CasAuthenticationFilter();
    filter.setServiceProperties(serviceProperties);
    filter.setAuthenticationSuccessHandler(successHandler);
    filter.setProxyGrantingTicketStorage(mock(ProxyGrantingTicketStorage.class));
    filter.setAuthenticationManager(manager);
    filter.afterPropertiesSet();
    filter.doFilter(request, response, chain);
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull().withFailMessage("Authentication should not be null");
    verify(chain).doFilter(request, response);
    verifyZeroInteractions(successHandler);
    // validate for when the filterProcessUrl matches
    filter.setFilterProcessesUrl(request.getServletPath());
    SecurityContextHolder.clearContext();
    filter.doFilter(request, response, chain);
    verifyNoMoreInteractions(chain);
    verify(successHandler).onAuthenticationSuccess(request, response, authentication);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) AuthenticationSuccessHandler(org.springframework.security.web.authentication.AuthenticationSuccessHandler) ServiceProperties(org.springframework.security.cas.ServiceProperties) ProxyGrantingTicketStorage(org.jasig.cas.client.proxy.ProxyGrantingTicketStorage) Authentication(org.springframework.security.core.Authentication) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 25 with AuthenticationManager

use of org.springframework.security.authentication.AuthenticationManager in project spring-security by spring-projects.

the class CasAuthenticationFilterTests method testNormalOperation.

@Test
public void testNormalOperation() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServletPath("/login/cas");
    request.addParameter("ticket", "ST-0-ER94xMJmn6pha35CQRoZ");
    CasAuthenticationFilter filter = new CasAuthenticationFilter();
    filter.setAuthenticationManager(new AuthenticationManager() {

        public Authentication authenticate(Authentication a) {
            return a;
        }
    });
    assertThat(filter.requiresAuthentication(request, new MockHttpServletResponse())).isTrue();
    Authentication result = filter.attemptAuthentication(request, new MockHttpServletResponse());
    assertThat(result != null).isTrue();
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)44 Test (org.junit.Test)29 Authentication (org.springframework.security.core.Authentication)24 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)19 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)19 MockFilterChain (org.springframework.mock.web.MockFilterChain)11 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)11 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)10 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)8 FilterChain (javax.servlet.FilterChain)7 AuthenticationException (org.springframework.security.core.AuthenticationException)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)4 Before (org.junit.Before)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 MockServletContext (org.springframework.mock.web.MockServletContext)2 User (org.springframework.security.core.userdetails.User)2 UserDetails (org.springframework.security.core.userdetails.UserDetails)2 OAuth2AuthenticationProcessingFilter (org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter)2