Search in sources :

Example 6 with UaaUser

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

the class TotpMfaEndpointTest method testNonNumericOTP.

@Test
public void testNonNumericOTP() throws Exception {
    when(uaaAuthentication.getPrincipal()).thenReturn(new UaaPrincipal(userId, "Marissa", null, "uaa", null, null), null, null);
    when(mfaProviderProvisioning.retrieveByName(mfaProvider.getName(), IdentityZoneHolder.get().getId())).thenReturn(mfaProvider);
    when(userDb.retrieveUserByName("Marissa", "uaa")).thenReturn(new UaaUser(new UaaUserPrototype().withUsername("Marissa").withOrigin("uaa").withId("1234").withEmail("marissa@example.com")));
    IdentityZoneHolder.get().getConfig().getMfaConfig().setEnabled(true).setProviderName(mfaProvider.getName());
    SessionStatus sessionStatus = mock(SessionStatus.class);
    ModelAndView returnView = endpoint.validateCode(mock(Model.class), "asdf123", mock(UserGoogleMfaCredentials.class), new MockHttpServletRequest(), sessionStatus);
    assertEquals("mfa/enter_code", returnView.getViewName());
    verifyZeroInteractions(sessionStatus);
    verifyMfaEvent(MfaAuthenticationFailureEvent.class);
}
Also used : UaaPrincipal(org.cloudfoundry.identity.uaa.authentication.UaaPrincipal) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) SessionStatus(org.springframework.web.bind.support.SessionStatus) UserGoogleMfaCredentials(org.cloudfoundry.identity.uaa.mfa.UserGoogleMfaCredentials) ModelAndView(org.springframework.web.servlet.ModelAndView) Model(org.springframework.ui.Model) UaaUserPrototype(org.cloudfoundry.identity.uaa.user.UaaUserPrototype) Test(org.junit.Test)

Example 7 with UaaUser

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

the class UaaChangePasswordService method changePassword.

@Override
public void changePassword(String username, String currentPassword, String newPassword) {
    if (username == null || currentPassword == null) {
        throw new BadCredentialsException(username);
    }
    passwordValidator.validate(newPassword);
    List<ScimUser> results = scimUserProvisioning.retrieveByUsernameAndOriginAndZone(username, UAA, IdentityZoneHolder.getCurrentZoneId());
    if (results.isEmpty()) {
        throw new ScimResourceNotFoundException("User not found");
    }
    ScimUser user = results.get(0);
    UaaUser uaaUser = getUaaUser(user);
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    try {
        if (scimUserProvisioning.checkPasswordMatches(user.getId(), newPassword, IdentityZoneHolder.get().getId())) {
            throw new InvalidPasswordException("Your new password cannot be the same as the old password.", UNPROCESSABLE_ENTITY);
        }
        scimUserProvisioning.changePassword(user.getId(), currentPassword, newPassword, IdentityZoneHolder.get().getId());
        publish(new PasswordChangeEvent("Password changed", uaaUser, authentication, IdentityZoneHolder.getCurrentZoneId()));
    } catch (Exception e) {
        publish(new PasswordChangeFailureEvent(e.getMessage(), uaaUser, authentication, IdentityZoneHolder.getCurrentZoneId()));
        throw e;
    }
}
Also used : ScimUser(org.cloudfoundry.identity.uaa.scim.ScimUser) PasswordChangeEvent(org.cloudfoundry.identity.uaa.account.event.PasswordChangeEvent) PasswordChangeFailureEvent(org.cloudfoundry.identity.uaa.account.event.PasswordChangeFailureEvent) Authentication(org.springframework.security.core.Authentication) UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) InvalidPasswordException(org.cloudfoundry.identity.uaa.scim.exception.InvalidPasswordException) ScimResourceNotFoundException(org.cloudfoundry.identity.uaa.scim.exception.ScimResourceNotFoundException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) InvalidPasswordException(org.cloudfoundry.identity.uaa.scim.exception.InvalidPasswordException) ScimResourceNotFoundException(org.cloudfoundry.identity.uaa.scim.exception.ScimResourceNotFoundException)

Example 8 with UaaUser

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

the class PasswordChangeEventPublisher method getUser.

UaaUser getUser(String userId) {
    try {
        // If the request came in for a user by id we should be able to
        // retrieve the username
        ScimUser scimUser = dao.retrieve(userId, identityZoneManager.getCurrentIdentityZoneId());
        Date today = new Date();
        if (scimUser != null) {
            return new UaaUser(scimUser.getId(), scimUser.getUserName(), "N/A", getEmail(scimUser), null, scimUser.getGivenName(), scimUser.getFamilyName(), today, today, scimUser.getOrigin(), scimUser.getExternalId(), scimUser.isVerified(), scimUser.getZoneId(), scimUser.getSalt(), scimUser.getPasswordLastModified());
        }
    } catch (ScimResourceNotFoundException e) {
    // ignore
    }
    return null;
}
Also used : ScimUser(org.cloudfoundry.identity.uaa.scim.ScimUser) UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) ScimResourceNotFoundException(org.cloudfoundry.identity.uaa.scim.exception.ScimResourceNotFoundException) Date(java.util.Date)

Example 9 with UaaUser

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

the class ExternalLoginAuthenticationManagerTest method testAuthenticateInvitedUserWithoutAcceptance.

@Test
public void testAuthenticateInvitedUserWithoutAcceptance() {
    String username = "guyWhoDoesNotAcceptInvites";
    String origin = LDAP;
    String email = "guy@ldap.org";
    UserDetails ldapUserDetails = mock(ExtendedLdapUserDetails.class, withSettings().extraInterfaces(Mailable.class));
    when(ldapUserDetails.getUsername()).thenReturn(username);
    when(ldapUserDetails.getPassword()).thenReturn(password);
    when(ldapUserDetails.getAuthorities()).thenReturn(null);
    when(ldapUserDetails.isAccountNonExpired()).thenReturn(true);
    when(ldapUserDetails.isAccountNonLocked()).thenReturn(true);
    when(ldapUserDetails.isCredentialsNonExpired()).thenReturn(true);
    when(ldapUserDetails.isEnabled()).thenReturn(true);
    when(((Mailable) ldapUserDetails).getEmailAddress()).thenReturn(email);
    // Invited users are created with their email as their username.
    UaaUser invitedUser = addUserToDb(email, userId, origin, email);
    when(invitedUser.modifyAttributes(anyString(), anyString(), anyString(), anyString(), anyBoolean())).thenReturn(invitedUser);
    UaaUser updatedUser = new UaaUser(new UaaUserPrototype().withUsername(username).withId(userId).withOrigin(origin).withEmail(email));
    when(invitedUser.modifyUsername(username)).thenReturn(updatedUser);
    manager = new LdapLoginAuthenticationManager(null);
    setupManager();
    manager.setProviderProvisioning(null);
    manager.setOrigin(origin);
    when(uaaUserDatabase.retrieveUserByName(eq(username), eq(origin))).thenThrow(new UsernameNotFoundException(""));
    when(uaaUserDatabase.retrieveUserByEmail(eq(email), eq(origin))).thenReturn(invitedUser);
    Authentication ldapAuth = mock(Authentication.class);
    when(ldapAuth.getPrincipal()).thenReturn(ldapUserDetails);
    manager.authenticate(ldapAuth);
    userArgumentCaptor = ArgumentCaptor.forClass(ApplicationEvent.class);
    verify(applicationEventPublisher, atLeastOnce()).publishEvent(userArgumentCaptor.capture());
    for (ApplicationEvent event : userArgumentCaptor.getAllValues()) {
        assertNotEquals(event.getClass(), NewUserAuthenticatedEvent.class);
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Mailable(org.cloudfoundry.identity.uaa.user.Mailable) LdapUserDetails(org.springframework.security.ldap.userdetails.LdapUserDetails) UserDetails(org.springframework.security.core.userdetails.UserDetails) ExtendedLdapUserDetails(org.cloudfoundry.identity.uaa.provider.ldap.ExtendedLdapUserDetails) UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) Authentication(org.springframework.security.core.Authentication) UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) ApplicationEvent(org.springframework.context.ApplicationEvent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UaaUserPrototype(org.cloudfoundry.identity.uaa.user.UaaUserPrototype) Test(org.junit.Test)

Example 10 with UaaUser

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

the class LdapLoginAuthenticationManagerTests method testGetUserWithNonLdapInfo.

@Test
void testGetUserWithNonLdapInfo() {
    UserDetails mockNonLdapUserDetails = mockNonLdapUserDetails();
    when(mockNonLdapUserDetails.getUsername()).thenReturn(TEST_EMAIL);
    when(auth.getPrincipal()).thenReturn(mockNonLdapUserDetails);
    UaaUser user = am.getUser(auth, null);
    assertEquals(TEST_EMAIL, user.getExternalId());
    assertEquals(TEST_EMAIL, user.getEmail());
    assertEquals(origin, user.getOrigin());
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) LdapUserDetails(org.springframework.security.ldap.userdetails.LdapUserDetails) 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