Search in sources :

Example 11 with UaaAuthentication

use of org.cloudfoundry.identity.uaa.authentication.UaaAuthentication in project uaa by cloudfoundry.

the class ExternalLoginAuthenticationManagerTest method testAuthenticateCreateUserWithLdapUserDetailsPrincipal.

@Test
public void testAuthenticateCreateUserWithLdapUserDetailsPrincipal() {
    String dn = "cn=" + userName + ",ou=Users,dc=test,dc=com";
    String origin = LDAP;
    String email = "joe@test.org";
    LdapUserDetails baseLdapUserDetails = mock(LdapUserDetails.class);
    mockUserDetails(baseLdapUserDetails);
    when(baseLdapUserDetails.getDn()).thenReturn(dn);
    HashMap<String, String[]> ldapAttrs = new HashMap<>();
    String ldapMailAttrName = "email";
    ldapAttrs.put(ldapMailAttrName, new String[] { email });
    ExtendedLdapUserImpl ldapUserDetails = new ExtendedLdapUserImpl(baseLdapUserDetails, ldapAttrs);
    ldapUserDetails.setMailAttributeName(ldapMailAttrName);
    manager = new LdapLoginAuthenticationManager(null);
    setupManager();
    manager.setProviderProvisioning(null);
    manager.setOrigin(origin);
    when(user.getEmail()).thenReturn(email);
    when(user.getOrigin()).thenReturn(origin);
    when(uaaUserDatabase.retrieveUserByName(eq(userName), eq(origin))).thenReturn(null).thenReturn(user);
    when(inputAuth.getPrincipal()).thenReturn(ldapUserDetails);
    Authentication result = manager.authenticate(inputAuth);
    assertNotNull(result);
    assertEquals(UaaAuthentication.class, result.getClass());
    UaaAuthentication uaaAuthentication = (UaaAuthentication) result;
    assertEquals(userName, uaaAuthentication.getPrincipal().getName());
    assertEquals(origin, uaaAuthentication.getPrincipal().getOrigin());
    assertEquals(userId, uaaAuthentication.getPrincipal().getId());
    userArgumentCaptor = ArgumentCaptor.forClass(ApplicationEvent.class);
    verify(applicationEventPublisher, times(3)).publishEvent(userArgumentCaptor.capture());
    assertEquals(3, userArgumentCaptor.getAllValues().size());
    NewUserAuthenticatedEvent event = (NewUserAuthenticatedEvent) userArgumentCaptor.getAllValues().get(0);
    assertEquals(origin, event.getUser().getOrigin());
    assertEquals(dn, event.getUser().getExternalId());
}
Also used : UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) ExtendedLdapUserImpl(org.cloudfoundry.identity.uaa.provider.ldap.extension.ExtendedLdapUserImpl) UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) Authentication(org.springframework.security.core.Authentication) LdapUserDetails(org.springframework.security.ldap.userdetails.LdapUserDetails) ExtendedLdapUserDetails(org.cloudfoundry.identity.uaa.provider.ldap.ExtendedLdapUserDetails) ApplicationEvent(org.springframework.context.ApplicationEvent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 12 with UaaAuthentication

use of org.cloudfoundry.identity.uaa.authentication.UaaAuthentication in project uaa by cloudfoundry.

the class ExternalLoginAuthenticationManagerTest method testAuthenticateWithAuthDetails.

@Test
public void testAuthenticateWithAuthDetails() {
    UaaAuthenticationDetails uaaAuthenticationDetails = mock(UaaAuthenticationDetails.class);
    when(uaaAuthenticationDetails.getOrigin()).thenReturn(origin);
    when(uaaAuthenticationDetails.getClientId()).thenReturn(null);
    when(uaaAuthenticationDetails.getSessionId()).thenReturn(new RandomValueStringGenerator().generate());
    when(inputAuth.getDetails()).thenReturn(uaaAuthenticationDetails);
    Authentication result = manager.authenticate(inputAuth);
    assertNotNull(result);
    assertEquals(UaaAuthentication.class, result.getClass());
    UaaAuthentication uaaAuthentication = (UaaAuthentication) result;
    assertEquals(userName, uaaAuthentication.getPrincipal().getName());
    assertEquals(origin, uaaAuthentication.getPrincipal().getOrigin());
    assertEquals(userId, uaaAuthentication.getPrincipal().getId());
}
Also used : UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) UaaAuthenticationDetails(org.cloudfoundry.identity.uaa.authentication.UaaAuthenticationDetails) UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) Authentication(org.springframework.security.core.Authentication) RandomValueStringGenerator(org.springframework.security.oauth2.common.util.RandomValueStringGenerator) Test(org.junit.Test)

Example 13 with UaaAuthentication

use of org.cloudfoundry.identity.uaa.authentication.UaaAuthentication in project uaa by cloudfoundry.

the class ExternalLoginAuthenticationManagerTest method testAmpersandInEndOfName.

@Test
public void testAmpersandInEndOfName() {
    String name = "filip@hanik@";
    String actual = name.replaceAll("@", "") + "@user.from." + origin + ".cf";
    when(userDetails.getUsername()).thenReturn(name);
    when(user.getUsername()).thenReturn(name);
    when(uaaUserDatabase.retrieveUserByName(eq(name), eq(origin))).thenReturn(null).thenReturn(user);
    Authentication result = manager.authenticate(inputAuth);
    assertNotNull(result);
    assertEquals(UaaAuthentication.class, result.getClass());
    UaaAuthentication uaaAuthentication = (UaaAuthentication) result;
    assertEquals(name, uaaAuthentication.getPrincipal().getName());
    assertEquals(origin, uaaAuthentication.getPrincipal().getOrigin());
    assertEquals(userId, uaaAuthentication.getPrincipal().getId());
    userArgumentCaptor = ArgumentCaptor.forClass(ApplicationEvent.class);
    verify(applicationEventPublisher, times(2)).publishEvent(userArgumentCaptor.capture());
    assertEquals(2, userArgumentCaptor.getAllValues().size());
    NewUserAuthenticatedEvent event = (NewUserAuthenticatedEvent) userArgumentCaptor.getAllValues().get(0);
    assertEquals(origin, event.getUser().getOrigin());
    assertEquals(actual, event.getUser().getEmail());
}
Also used : UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) Authentication(org.springframework.security.core.Authentication) ApplicationEvent(org.springframework.context.ApplicationEvent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 14 with UaaAuthentication

use of org.cloudfoundry.identity.uaa.authentication.UaaAuthentication in project uaa by cloudfoundry.

the class DefaultSecurityContextAccessorTests method zoneAdminUserIsNotAdmin_BecauseOriginIsNotUaa.

@Test
void zoneAdminUserIsNotAdmin_BecauseOriginIsNotUaa() {
    BaseClientDetails client = new BaseClientDetails();
    List<SimpleGrantedAuthority> authorities = new LinkedList<>();
    authorities.add(new SimpleGrantedAuthority("zones." + IdentityZoneHolder.get().getId() + ".admin"));
    client.setAuthorities(authorities);
    UaaPrincipal principal = new UaaPrincipal("id", "username", "email", OriginKeys.UAA, null, MultitenancyFixture.identityZone("test", "test").getId());
    UaaAuthentication userAuthentication = new UaaAuthentication(principal, authorities, new UaaAuthenticationDetails(new MockHttpServletRequest()));
    AuthorizationRequest authorizationRequest = new AuthorizationRequest("admin", UaaStringUtils.getStringsFromAuthorities(authorities));
    authorizationRequest.setResourceIdsAndAuthoritiesFromClientDetails(client);
    SecurityContextHolder.getContext().setAuthentication(new OAuth2Authentication(authorizationRequest.createOAuth2Request(), userAuthentication));
    assertFalse(defaultSecurityContextAccessor.isAdmin());
}
Also used : BaseClientDetails(org.springframework.security.oauth2.provider.client.BaseClientDetails) UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UaaPrincipal(org.cloudfoundry.identity.uaa.authentication.UaaPrincipal) AuthorizationRequest(org.springframework.security.oauth2.provider.AuthorizationRequest) UaaAuthenticationDetails(org.cloudfoundry.identity.uaa.authentication.UaaAuthenticationDetails) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 15 with UaaAuthentication

use of org.cloudfoundry.identity.uaa.authentication.UaaAuthentication in project uaa by cloudfoundry.

the class TokenMvcMockTests method setAuthentication.

private void setAuthentication(MockHttpSession session, ScimUser developer, boolean forcePasswordChange, String... authMethods) {
    UaaPrincipal p = new UaaPrincipal(developer.getId(), developer.getUserName(), developer.getPrimaryEmail(), OriginKeys.UAA, "", IdentityZoneHolder.get().getId());
    UaaAuthentication auth = new UaaAuthentication(p, UaaAuthority.USER_AUTHORITIES, new UaaAuthenticationDetails(false, "clientId", OriginKeys.ORIGIN, "sessionId"));
    SessionUtils.setPasswordChangeRequired(session, forcePasswordChange);
    auth.setAuthenticationMethods(new HashSet<>(Arrays.asList(authMethods)));
    assertTrue(auth.isAuthenticated());
    SecurityContextHolder.getContext().setAuthentication(auth);
    session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, new MockSecurityContext(auth));
}
Also used : UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) UaaPrincipal(org.cloudfoundry.identity.uaa.authentication.UaaPrincipal) UaaAuthenticationDetails(org.cloudfoundry.identity.uaa.authentication.UaaAuthenticationDetails)

Aggregations

UaaAuthentication (org.cloudfoundry.identity.uaa.authentication.UaaAuthentication)117 Test (org.junit.jupiter.api.Test)51 UaaPrincipal (org.cloudfoundry.identity.uaa.authentication.UaaPrincipal)45 Authentication (org.springframework.security.core.Authentication)35 UaaUser (org.cloudfoundry.identity.uaa.user.UaaUser)29 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)25 UaaAuthenticationDetails (org.cloudfoundry.identity.uaa.authentication.UaaAuthenticationDetails)23 Test (org.junit.Test)18 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)14 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)13 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)12 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)11 HashMap (java.util.HashMap)10 MockHttpSession (org.springframework.mock.web.MockHttpSession)10 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)9 BaseClientDetails (org.springframework.security.oauth2.provider.client.BaseClientDetails)9 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)8 ScimUser (org.cloudfoundry.identity.uaa.scim.ScimUser)7