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);
}
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;
}
}
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;
}
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);
}
}
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());
}
Aggregations