Search in sources :

Example 66 with UsernamePasswordCredential

use of org.apereo.cas.authentication.credential.UsernamePasswordCredential in project cas by apereo.

the class PasswordChangeAction method doExecute.

@Override
protected Event doExecute(final RequestContext requestContext) {
    try {
        val creds = Objects.requireNonNull(WebUtils.getCredential(requestContext, UsernamePasswordCredential.class));
        val bean = getPasswordChangeRequest(requestContext);
        LOGGER.debug("Attempting to validate the password change bean for username [{}]", creds.getUsername());
        if (!passwordValidationService.isValid(creds, bean)) {
            LOGGER.error("Failed to validate the provided password");
            return getErrorEvent(requestContext, PASSWORD_VALIDATION_FAILURE_CODE, DEFAULT_MESSAGE);
        }
        if (passwordManagementService.change(creds, bean)) {
            WebUtils.putCredential(requestContext, new UsernamePasswordCredential(creds.getUsername(), bean.getPassword()));
            LOGGER.info("Password successfully changed for [{}]", bean.getUsername());
            return getSuccessEvent(requestContext, bean);
        }
    } catch (final InvalidPasswordException e) {
        return getErrorEvent(requestContext, PASSWORD_VALIDATION_FAILURE_CODE + StringUtils.defaultIfBlank(e.getCode(), StringUtils.EMPTY), StringUtils.defaultIfBlank(e.getValidationMessage(), DEFAULT_MESSAGE), e.getParams());
    } catch (final Exception e) {
        LoggingUtils.error(LOGGER, e);
    }
    return getErrorEvent(requestContext, "pm.updateFailure", DEFAULT_MESSAGE);
}
Also used : lombok.val(lombok.val) InvalidPasswordException(org.apereo.cas.pm.InvalidPasswordException) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) InvalidPasswordException(org.apereo.cas.pm.InvalidPasswordException)

Example 67 with UsernamePasswordCredential

use of org.apereo.cas.authentication.credential.UsernamePasswordCredential in project cas by apereo.

the class InitPasswordResetAction method doExecute.

@Override
protected Event doExecute(final RequestContext requestContext) {
    val token = PasswordManagementWebflowUtils.getPasswordResetToken(requestContext);
    if (StringUtils.isBlank(token)) {
        LOGGER.error("Password reset token is missing");
        return error();
    }
    val username = passwordManagementService.parseToken(token);
    if (StringUtils.isBlank(username)) {
        LOGGER.error("Password reset token could not be verified to determine username");
        return error();
    }
    val c = new UsernamePasswordCredential();
    c.setUsername(username);
    WebUtils.putCredential(requestContext, c);
    return success();
}
Also used : lombok.val(lombok.val) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential)

Example 68 with UsernamePasswordCredential

use of org.apereo.cas.authentication.credential.UsernamePasswordCredential in project cas by apereo.

the class ADPasswordManagementServiceTests method verifyPasswordReset.

@Test
public void verifyPasswordReset() {
    val credential = new UsernamePasswordCredential("changepassword", StringUtils.EMPTY);
    val bean = new PasswordChangeRequest();
    bean.setConfirmedPassword("P@ssw0rdMellon");
    bean.setPassword("P@ssw0rdMellon");
    bean.setUsername(credential.getUsername());
    assertTrue(passwordChangeService.change(credential, bean));
}
Also used : lombok.val(lombok.val) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) Test(org.junit.jupiter.api.Test)

Example 69 with UsernamePasswordCredential

use of org.apereo.cas.authentication.credential.UsernamePasswordCredential in project cas by apereo.

the class ADPasswordManagementServiceTests method verifyPasswordChange.

@Test
public void verifyPasswordChange() {
    val credential = new UsernamePasswordCredential("changepasswordnoreset", "P@ssw0rd");
    val bean = new PasswordChangeRequest();
    bean.setConfirmedPassword("P@ssw0rd2");
    bean.setPassword("P@ssw0rd2");
    bean.setUsername(credential.getUsername());
    assertTrue(passwordChangeService.change(credential, bean));
}
Also used : lombok.val(lombok.val) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) Test(org.junit.jupiter.api.Test)

Example 70 with UsernamePasswordCredential

use of org.apereo.cas.authentication.credential.UsernamePasswordCredential in project cas by apereo.

the class JsonResourcePasswordManagementService method changeInternal.

@Override
public boolean changeInternal(@NonNull final Credential credential, @NonNull final PasswordChangeRequest bean) {
    val c = (UsernamePasswordCredential) credential;
    if (StringUtils.isBlank(bean.getPassword())) {
        LOGGER.error("Password cannot be blank");
        return false;
    }
    if (!StringUtils.equals(bean.getPassword(), bean.getConfirmedPassword())) {
        LOGGER.error("Password does not match and cannot be confirmed");
        return false;
    }
    val account = this.jsonBackedAccounts.getOrDefault(c.getId(), null);
    if (account == null) {
        LOGGER.error("User account [{}] cannot be found", c.getId());
        return false;
    }
    account.setPassword(bean.getPassword());
    this.jsonBackedAccounts.put(c.getId(), account);
    return writeAccountToJsonResource();
}
Also used : lombok.val(lombok.val) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential)

Aggregations

lombok.val (lombok.val)111 UsernamePasswordCredential (org.apereo.cas.authentication.credential.UsernamePasswordCredential)111 Test (org.junit.jupiter.api.Test)74 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)30 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)14 SimpleTestUsernamePasswordAuthenticationHandler (org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler)12 HashMap (java.util.HashMap)8 Map (java.util.Map)8 BasicCredentialMetaData (org.apereo.cas.authentication.metadata.BasicCredentialMetaData)8 LinkedHashMap (java.util.LinkedHashMap)7 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)7 Executable (org.junit.jupiter.api.function.Executable)7 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)6 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)6 MockRequestContext (org.springframework.webflow.test.MockRequestContext)6 ArrayList (java.util.ArrayList)5 ClassPathResource (org.springframework.core.io.ClassPathResource)5 MockServletContext (org.springframework.mock.web.MockServletContext)5 FailedLoginException (javax.security.auth.login.FailedLoginException)4 SurrogateUsernamePasswordCredential (org.apereo.cas.authentication.SurrogateUsernamePasswordCredential)4