Search in sources :

Example 36 with AuthenticationManager

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

the class ResourceOwnerPasswordTokenGranterTests method testExtraParameters.

@Test
public void testExtraParameters() {
    authenticationManager = new AuthenticationManager() {

        @Override
        public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            if (authentication instanceof UsernamePasswordAuthenticationToken) {
                UsernamePasswordAuthenticationToken user = (UsernamePasswordAuthenticationToken) authentication;
                user = new UsernamePasswordAuthenticationToken(user.getPrincipal(), "N/A", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
                @SuppressWarnings("unchecked") Map<String, String> details = (Map<String, String>) authentication.getDetails();
                assertNull(details.get("password"));
                return user;
            }
            return authentication;
        }
    };
    ResourceOwnerPasswordTokenGranter granter = new ResourceOwnerPasswordTokenGranter(authenticationManager, providerTokenServices, clientDetailsService, requestFactory);
    OAuth2AccessToken token = granter.grant("password", tokenRequest);
    OAuth2Authentication authentication = providerTokenServices.loadAuthentication(token.getValue());
    assertTrue(authentication.isAuthenticated());
    assertNull(authentication.getUserAuthentication().getDetails());
}
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) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 37 with AuthenticationManager

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

the class SecurityAutoConfigurationTests method pingAuthenticationListener.

private void pingAuthenticationListener() {
    AuthenticationListener listener = new AuthenticationListener();
    this.context.addApplicationListener(listener);
    AuthenticationManager manager = this.context.getBean(AuthenticationManager.class);
    try {
        manager.authenticate(new UsernamePasswordAuthenticationToken("foo", "wrong"));
        fail("Expected BadCredentialsException");
    } catch (BadCredentialsException e) {
    // expected
    }
    assertThat(listener.event).isInstanceOf(AuthenticationFailureBadCredentialsEvent.class);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Example 38 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 39 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)

Example 40 with AuthenticationManager

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

the class JdbcUserServiceBeanDefinitionParserTests method isSupportedByAuthenticationProviderElement.

@Test
public void isSupportedByAuthenticationProviderElement() {
    setContext("<authentication-manager>" + "  <authentication-provider>" + "    <jdbc-user-service data-source-ref='dataSource'/>" + "  </authentication-provider>" + "</authentication-manager>" + DATA_SOURCE);
    AuthenticationManager mgr = (AuthenticationManager) appContext.getBean(BeanIds.AUTHENTICATION_MANAGER);
    mgr.authenticate(new UsernamePasswordAuthenticationToken("rod", "koala"));
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Aggregations

AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)45 Test (org.junit.Test)30 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