Search in sources :

Example 11 with UaaUser

use of org.cloudfoundry.identity.uaa.user.UaaUser in project uaa by cloudfoundry.

the class LdapLoginAuthenticationManagerTests method testUserAuthenticated.

@Test
void testUserAuthenticated() {
    UaaUser user = getUaaUser();
    UaaUser userFromRequest = am.getUser(auth, null);
    definition.setAutoAddGroups(true);
    UaaUser result = am.userAuthenticated(auth, user, userFromRequest);
    assertSame(dbUser, result);
    verify(publisher, times(1)).publishEvent(ArgumentMatchers.any());
    definition.setAutoAddGroups(false);
    result = am.userAuthenticated(auth, userFromRequest, user);
    assertSame(dbUser, result);
    verify(publisher, times(2)).publishEvent(ArgumentMatchers.any());
}
Also used : UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) Test(org.junit.jupiter.api.Test)

Example 12 with UaaUser

use of org.cloudfoundry.identity.uaa.user.UaaUser in project uaa by cloudfoundry.

the class LdapLoginAuthenticationManagerTests method update_existingUser_if_attributes_different.

@Test
void update_existingUser_if_attributes_different() {
    ExtendedLdapUserImpl authDetails = getAuthDetails(LDAP_EMAIL, "MarissaChanged", "BloggsChanged", "8675309");
    when(auth.getPrincipal()).thenReturn(authDetails);
    UaaUser user = getUaaUser();
    UaaUser userFromRequest = am.getUser(auth, null);
    am.userAuthenticated(auth, userFromRequest, user);
    ArgumentCaptor<ExternalGroupAuthorizationEvent> captor = ArgumentCaptor.forClass(ExternalGroupAuthorizationEvent.class);
    verify(publisher, times(1)).publishEvent(captor.capture());
    assertEquals(LDAP_EMAIL, captor.getValue().getUser().getEmail());
    assertEquals("MarissaChanged", captor.getValue().getUser().getGivenName());
    assertEquals("BloggsChanged", captor.getValue().getUser().getFamilyName());
}
Also used : ExtendedLdapUserImpl(org.cloudfoundry.identity.uaa.provider.ldap.extension.ExtendedLdapUserImpl) UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) Test(org.junit.jupiter.api.Test)

Example 13 with UaaUser

use of org.cloudfoundry.identity.uaa.user.UaaUser in project uaa by cloudfoundry.

the class LdapLoginAuthenticationManagerTests method testGetUserWithExtendedLdapInfo.

@Test
void testGetUserWithExtendedLdapInfo() {
    UaaUser user = am.getUser(auth, null);
    assertEquals(DN, user.getExternalId());
    assertEquals(LDAP_EMAIL, user.getEmail());
    assertEquals(origin, user.getOrigin());
    assertFalse(user.isVerified());
}
Also used : UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) Test(org.junit.jupiter.api.Test)

Example 14 with UaaUser

use of org.cloudfoundry.identity.uaa.user.UaaUser in project uaa by cloudfoundry.

the class LdapLoginAuthenticationManagerTests method test_authentication_attributes.

void test_authentication_attributes(boolean storeUserInfo) {
    UaaUser user = getUaaUser();
    ExtendedLdapUserImpl authDetails = getAuthDetails(user.getEmail(), user.getGivenName(), user.getFamilyName(), user.getPhoneNumber(), new AttributeInfo(UAA_MANAGER, new String[] { KARI_THE_ANT_EATER, JOHN_THE_SLOTH }), new AttributeInfo(COST_CENTER, new String[] { DENVER_CO }));
    Map<String, String[]> role1 = new HashMap<>();
    role1.put("cn", new String[] { "ldap.role.1.a", "ldap.role.1.b", "ldap.role.1" });
    Map<String, String[]> role2 = new HashMap<>();
    role2.put("cn", new String[] { "ldap.role.2.a", "ldap.role.2.b", "ldap.role.2" });
    authDetails.setAuthorities(Arrays.asList(new LdapAuthority("role1", "cn=role1,ou=test,ou=com", role1), new LdapAuthority("role2", "cn=role2,ou=test,ou=com", role2)));
    definition.setExternalGroupsWhitelist(Collections.singletonList("*"));
    when(auth.getPrincipal()).thenReturn(authDetails);
    UaaUserDatabase db = mock(UaaUserDatabase.class);
    when(db.retrieveUserByName(anyString(), eq(OriginKeys.LDAP))).thenReturn(user);
    when(db.retrieveUserById(anyString())).thenReturn(user);
    am.setOrigin(OriginKeys.LDAP);
    am.setUserDatabase(db);
    // set the config flag
    definition.setStoreCustomAttributes(storeUserInfo);
    UaaAuthentication authentication = (UaaAuthentication) am.authenticate(auth);
    UserInfo info = new UserInfo().setUserAttributes(authentication.getUserAttributes()).setRoles(Arrays.asList("ldap.role.1.a", "ldap.role.1.b", "ldap.role.1", "ldap.role.2.a", "ldap.role.2.b", "ldap.role.2"));
    if (storeUserInfo) {
        verify(db, times(1)).storeUserInfo(anyString(), eq(info));
    } else {
        verify(db, never()).storeUserInfo(anyString(), eq(info));
    }
    assertEquals("Expected two user attributes", 2, authentication.getUserAttributes().size());
    assertNotNull("Expected cost center attribute", authentication.getUserAttributes().get(COST_CENTERS));
    assertEquals(DENVER_CO, authentication.getUserAttributes().getFirst(COST_CENTERS));
    assertNotNull("Expected manager attribute", authentication.getUserAttributes().get(MANAGERS));
    assertEquals("Expected 2 manager attribute values", 2, authentication.getUserAttributes().get(MANAGERS).size());
    assertThat(authentication.getUserAttributes().get(MANAGERS), containsInAnyOrder(JOHN_THE_SLOTH, KARI_THE_ANT_EATER));
    assertThat(authentication.getAuthenticationMethods(), containsInAnyOrder("ext", "pwd"));
}
Also used : UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) ExtendedLdapUserImpl(org.cloudfoundry.identity.uaa.provider.ldap.extension.ExtendedLdapUserImpl) LdapAuthority(org.cloudfoundry.identity.uaa.provider.ldap.extension.LdapAuthority) UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) UserInfo(org.cloudfoundry.identity.uaa.user.UserInfo) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UaaUserDatabase(org.cloudfoundry.identity.uaa.user.UaaUserDatabase)

Example 15 with UaaUser

use of org.cloudfoundry.identity.uaa.user.UaaUser in project uaa by cloudfoundry.

the class LoginAuthenticationManagerTests method testHappyDayWithAuthorities.

@Test
void testHappyDayWithAuthorities() {
    UaaUser user = UaaUserTestFactory.getAdminUser("FOO", "foo", "fo@test.org", "Foo", "Bar");
    Mockito.when(userDatabase.retrieveUserByName("foo", OriginKeys.LOGIN_SERVER)).thenReturn(user);
    Authentication authentication = manager.authenticate(UaaAuthenticationTestFactory.getAuthenticationRequest("foo"));
    assertEquals(user.getUsername(), ((UaaPrincipal) authentication.getPrincipal()).getName());
    assertEquals(user.getAuthorities(), authentication.getAuthorities());
}
Also used : OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) UaaUserMatcher.aUaaUser(org.cloudfoundry.identity.uaa.user.UaaUserMatcher.aUaaUser) UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) Test(org.junit.jupiter.api.Test)

Aggregations

UaaUser (org.cloudfoundry.identity.uaa.user.UaaUser)148 Test (org.junit.jupiter.api.Test)73 UaaAuthentication (org.cloudfoundry.identity.uaa.authentication.UaaAuthentication)38 UaaPrincipal (org.cloudfoundry.identity.uaa.authentication.UaaPrincipal)29 UaaUserPrototype (org.cloudfoundry.identity.uaa.user.UaaUserPrototype)26 ScimUser (org.cloudfoundry.identity.uaa.scim.ScimUser)24 Test (org.junit.Test)23 HashMap (java.util.HashMap)22 Date (java.util.Date)20 UaaUserMatcher.aUaaUser (org.cloudfoundry.identity.uaa.user.UaaUserMatcher.aUaaUser)17 Authentication (org.springframework.security.core.Authentication)15 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 UaaUserDatabase (org.cloudfoundry.identity.uaa.user.UaaUserDatabase)11 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 BaseClientDetails (org.springframework.security.oauth2.provider.client.BaseClientDetails)11 ModelTestUtils.getResourceAsString (org.cloudfoundry.identity.uaa.test.ModelTestUtils.getResourceAsString)10 Mockito.anyString (org.mockito.Mockito.anyString)9 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)8 ArrayList (java.util.ArrayList)7