Search in sources :

Example 11 with PasswordRecovery

use of org.motechproject.security.domain.PasswordRecovery in project motech by motech.

the class PasswordRecoveryServiceTest method shouldSendRecoveryEmail.

@Test
public void shouldSendRecoveryEmail() {
    motechSettings = mock(MotechSettings.class);
    when(settingsFacade.getPlatformSettings()).thenReturn(motechSettings);
    when(motechSettings.getServerHost()).thenReturn("serverurl");
    when(motechSettings.getServerUrl()).thenReturn("http://serverurl");
    when(motechSettings.getLoginMode()).thenReturn(LoginMode.REPOSITORY);
    PasswordRecovery newRecovery = new PasswordRecovery();
    newRecovery.setUsername(USERNAME);
    newRecovery.setEmail(EMAIL);
    newRecovery.setToken(TOKEN);
    newRecovery.setExpirationDate(DateTime.now().plusHours(1));
    newRecovery.setLocale(Locale.ENGLISH);
    emailSenderInjected.sendRecoveryEmail(newRecovery);
    verify(eventRelay).sendEventMessage(any(emailEvent.getClass()));
}
Also used : MotechSettings(org.motechproject.config.domain.MotechSettings) PasswordRecovery(org.motechproject.security.domain.PasswordRecovery) Test(org.junit.Test)

Example 12 with PasswordRecovery

use of org.motechproject.security.domain.PasswordRecovery in project motech by motech.

the class PasswordRecoveryServiceTest method shouldCreatePasswordRecoveryForOpenID.

@Test
public void shouldCreatePasswordRecoveryForOpenID() throws UserNotFoundException, NonAdminUserException {
    final DateTime now = DateTime.now();
    final int expiration = EXPIRATION_HOURS;
    testCreateOpenIDRecoveryTemplate(now, EMAIL, now.plusHours(expiration));
    ArgumentCaptor<PasswordRecovery> captor = ArgumentCaptor.forClass(PasswordRecovery.class);
    verify(passwordRecoveriesDataService).create(captor.capture());
    PasswordRecovery createdRecovery = captor.getValue();
    assertEquals(USERNAME, createdRecovery.getUsername());
    assertEquals(EMAIL, createdRecovery.getEmail());
    assertEquals(now.plusHours(expiration), createdRecovery.getExpirationDate());
    assertEquals(Locale.ENGLISH, createdRecovery.getLocale());
    assertEquals(60, createdRecovery.getToken().length());
}
Also used : PasswordRecovery(org.motechproject.security.domain.PasswordRecovery) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 13 with PasswordRecovery

use of org.motechproject.security.domain.PasswordRecovery in project motech by motech.

the class PasswordRecoveryServiceImpl method createRecovery.

private PasswordRecovery createRecovery(String username, String email, String token, DateTime expirationDate, Locale locale) {
    PasswordRecovery oldRecovery = findForUser(username);
    if (oldRecovery != null) {
        passwordRecoveriesDataService.delete(oldRecovery);
    }
    PasswordRecovery recovery = new PasswordRecovery();
    recovery.setUsername(username);
    recovery.setEmail(email);
    recovery.setToken(token);
    recovery.setExpirationDate(expirationDate);
    recovery.setLocale(locale);
    add(recovery);
    return recovery;
}
Also used : PasswordRecovery(org.motechproject.security.domain.PasswordRecovery)

Example 14 with PasswordRecovery

use of org.motechproject.security.domain.PasswordRecovery in project motech by motech.

the class PasswordRecoveryServiceImpl method passwordRecoveryRequest.

@Override
@Transactional
public String passwordRecoveryRequest(String email, DateTime expiration, boolean notify) throws UserNotFoundException {
    MotechUser user = motechUsersDao.findUserByEmail(email);
    DateTime expirationDate = expiration;
    if (expirationDate == null) {
        expirationDate = DateTime.now().plusHours(DEFAULT_EXPIRATION_HOURS);
    } else if (expirationDate.isBefore(DateTime.now())) {
        throw new IllegalArgumentException("The expiration date shouldn't be a past date!");
    }
    if (user == null) {
        throw new UserNotFoundException("User with email not found: " + email);
    }
    String token = RandomStringUtils.randomAlphanumeric(TOKEN_LENGTH);
    PasswordRecovery recovery = createRecovery(user.getUserName(), user.getEmail(), token, expirationDate, user.getLocale());
    if (notify) {
        emailSender.sendRecoveryEmail(recovery);
    }
    LOGGER.info("Created a password recovery for user " + user.getUserName());
    return token;
}
Also used : UserNotFoundException(org.motechproject.security.exception.UserNotFoundException) MotechUser(org.motechproject.security.domain.MotechUser) PasswordRecovery(org.motechproject.security.domain.PasswordRecovery) DateTime(org.joda.time.DateTime) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

PasswordRecovery (org.motechproject.security.domain.PasswordRecovery)14 DateTime (org.joda.time.DateTime)8 Test (org.junit.Test)8 Transactional (org.springframework.transaction.annotation.Transactional)5 MotechUser (org.motechproject.security.domain.MotechUser)4 UserNotFoundException (org.motechproject.security.exception.UserNotFoundException)2 Range (org.motechproject.commons.api.Range)1 MotechSettings (org.motechproject.config.domain.MotechSettings)1 InvalidTokenException (org.motechproject.security.exception.InvalidTokenException)1 NonAdminUserException (org.motechproject.security.exception.NonAdminUserException)1 Authentication (org.springframework.security.core.Authentication)1 OpenIDAuthenticationToken (org.springframework.security.openid.OpenIDAuthenticationToken)1