Search in sources :

Example 76 with Authentication

use of org.springframework.security.core.Authentication in project spring-security by spring-projects.

the class DefaultAuthenticationEventPublisherTests method expectedDefaultMappingsAreSatisfied.

@Test
public void expectedDefaultMappingsAreSatisfied() throws Exception {
    publisher = new DefaultAuthenticationEventPublisher();
    ApplicationEventPublisher appPublisher = mock(ApplicationEventPublisher.class);
    publisher.setApplicationEventPublisher(appPublisher);
    Authentication a = mock(Authentication.class);
    Exception cause = new Exception();
    Object extraInfo = new Object();
    publisher.publishAuthenticationFailure(new BadCredentialsException(""), a);
    publisher.publishAuthenticationFailure(new BadCredentialsException("", cause), a);
    verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureBadCredentialsEvent.class));
    reset(appPublisher);
    publisher.publishAuthenticationFailure(new UsernameNotFoundException(""), a);
    publisher.publishAuthenticationFailure(new UsernameNotFoundException("", cause), a);
    publisher.publishAuthenticationFailure(new AccountExpiredException(""), a);
    publisher.publishAuthenticationFailure(new AccountExpiredException("", cause), a);
    publisher.publishAuthenticationFailure(new ProviderNotFoundException(""), a);
    publisher.publishAuthenticationFailure(new DisabledException(""), a);
    publisher.publishAuthenticationFailure(new DisabledException("", cause), a);
    publisher.publishAuthenticationFailure(new LockedException(""), a);
    publisher.publishAuthenticationFailure(new LockedException("", cause), a);
    publisher.publishAuthenticationFailure(new AuthenticationServiceException(""), a);
    publisher.publishAuthenticationFailure(new AuthenticationServiceException("", cause), a);
    publisher.publishAuthenticationFailure(new CredentialsExpiredException(""), a);
    publisher.publishAuthenticationFailure(new CredentialsExpiredException("", cause), a);
    verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureBadCredentialsEvent.class));
    verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureExpiredEvent.class));
    verify(appPublisher).publishEvent(isA(AuthenticationFailureProviderNotFoundEvent.class));
    verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureDisabledEvent.class));
    verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureLockedEvent.class));
    verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureServiceExceptionEvent.class));
    verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureCredentialsExpiredEvent.class));
    verifyNoMoreInteractions(appPublisher);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AuthenticationFailureProviderNotFoundEvent(org.springframework.security.authentication.event.AuthenticationFailureProviderNotFoundEvent) AuthenticationFailureBadCredentialsEvent(org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AuthenticationException(org.springframework.security.core.AuthenticationException) AuthenticationFailureServiceExceptionEvent(org.springframework.security.authentication.event.AuthenticationFailureServiceExceptionEvent) AuthenticationFailureLockedEvent(org.springframework.security.authentication.event.AuthenticationFailureLockedEvent) Authentication(org.springframework.security.core.Authentication) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) AuthenticationFailureCredentialsExpiredEvent(org.springframework.security.authentication.event.AuthenticationFailureCredentialsExpiredEvent) AuthenticationFailureDisabledEvent(org.springframework.security.authentication.event.AuthenticationFailureDisabledEvent) AuthenticationFailureExpiredEvent(org.springframework.security.authentication.event.AuthenticationFailureExpiredEvent)

Example 77 with Authentication

use of org.springframework.security.core.Authentication in project spring-security by spring-projects.

the class RunAsManagerImplTests method testReturnsAdditionalGrantedAuthorities.

@Test
public void testReturnsAdditionalGrantedAuthorities() throws Exception {
    UsernamePasswordAuthenticationToken inputToken = new UsernamePasswordAuthenticationToken("Test", "Password", AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
    RunAsManagerImpl runAs = new RunAsManagerImpl();
    runAs.setKey("my_password");
    Authentication result = runAs.buildRunAs(inputToken, new Object(), SecurityConfig.createList("RUN_AS_SOMETHING"));
    if (!(result instanceof RunAsUserToken)) {
        fail("Should have returned a RunAsUserToken");
    }
    assertThat(result.getPrincipal()).isEqualTo(inputToken.getPrincipal());
    assertThat(result.getCredentials()).isEqualTo(inputToken.getCredentials());
    Set<String> authorities = AuthorityUtils.authorityListToSet(result.getAuthorities());
    assertThat(authorities.contains("ROLE_RUN_AS_SOMETHING")).isTrue();
    assertThat(authorities.contains("ROLE_ONE")).isTrue();
    assertThat(authorities.contains("ROLE_TWO")).isTrue();
    RunAsUserToken resultCast = (RunAsUserToken) result;
    assertThat(resultCast.getKeyHash()).isEqualTo("my_password".hashCode());
}
Also used : Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 78 with Authentication

use of org.springframework.security.core.Authentication in project spring-security by spring-projects.

the class RunAsManagerImplTests method testRespectsRolePrefix.

@Test
public void testRespectsRolePrefix() throws Exception {
    UsernamePasswordAuthenticationToken inputToken = new UsernamePasswordAuthenticationToken("Test", "Password", AuthorityUtils.createAuthorityList("ONE", "TWO"));
    RunAsManagerImpl runAs = new RunAsManagerImpl();
    runAs.setKey("my_password");
    runAs.setRolePrefix("FOOBAR_");
    Authentication result = runAs.buildRunAs(inputToken, new Object(), SecurityConfig.createList("RUN_AS_SOMETHING"));
    assertThat(result instanceof RunAsUserToken).withFailMessage("Should have returned a RunAsUserToken").isTrue();
    assertThat(result.getPrincipal()).isEqualTo(inputToken.getPrincipal());
    assertThat(result.getCredentials()).isEqualTo(inputToken.getCredentials());
    Set<String> authorities = AuthorityUtils.authorityListToSet(result.getAuthorities());
    assertThat(authorities.contains("FOOBAR_RUN_AS_SOMETHING")).isTrue();
    assertThat(authorities.contains("ONE")).isTrue();
    assertThat(authorities.contains("TWO")).isTrue();
    RunAsUserToken resultCast = (RunAsUserToken) result;
    assertThat(resultCast.getKeyHash()).isEqualTo("my_password".hashCode());
}
Also used : Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 79 with Authentication

use of org.springframework.security.core.Authentication in project spring-security by spring-projects.

the class DaoAuthenticationProviderTests method testAuthenticatesWithForcePrincipalAsString.

@Test
public void testAuthenticatesWithForcePrincipalAsString() {
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("rod", "koala");
    DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    provider.setUserDetailsService(new MockAuthenticationDaoUserrod());
    provider.setUserCache(new MockUserCache());
    provider.setForcePrincipalAsString(true);
    Authentication result = provider.authenticate(token);
    if (!(result instanceof UsernamePasswordAuthenticationToken)) {
        fail("Should have returned instance of UsernamePasswordAuthenticationToken");
    }
    UsernamePasswordAuthenticationToken castResult = (UsernamePasswordAuthenticationToken) result;
    assertThat(castResult.getPrincipal().getClass()).isEqualTo(String.class);
    assertThat(castResult.getPrincipal()).isEqualTo("rod");
}
Also used : Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 80 with Authentication

use of org.springframework.security.core.Authentication in project spring-security by spring-projects.

the class ProviderManagerTests method authenticationSucceedsWhenFirstProviderReturnsNullButSecondAuthenticates.

@Test
public void authenticationSucceedsWhenFirstProviderReturnsNullButSecondAuthenticates() {
    final Authentication a = mock(Authentication.class);
    ProviderManager mgr = new ProviderManager(Arrays.asList(createProviderWhichReturns(null), createProviderWhichReturns(a)));
    AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
    mgr.setAuthenticationEventPublisher(publisher);
    Authentication result = mgr.authenticate(a);
    assertThat(result).isSameAs(a);
    verify(publisher).publishAuthenticationSuccess(result);
}
Also used : Authentication(org.springframework.security.core.Authentication) Test(org.junit.Test)

Aggregations

Authentication (org.springframework.security.core.Authentication)498 Test (org.junit.Test)192 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)114 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)98 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)75 SecurityContext (org.springframework.security.core.context.SecurityContext)63 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)57 GrantedAuthority (org.springframework.security.core.GrantedAuthority)50 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)47 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)42 MifosUser (org.mifos.security.MifosUser)38 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)34 HttpServletRequest (javax.servlet.http.HttpServletRequest)32 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)32 AuthenticationException (org.springframework.security.core.AuthenticationException)31 UserDetails (org.springframework.security.core.userdetails.UserDetails)31 MifosUserBuilder (org.mifos.builders.MifosUserBuilder)29 HashMap (java.util.HashMap)27 HttpServletResponse (javax.servlet.http.HttpServletResponse)27 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)25