package org.apereo.cas.pm.web.flow.actions;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.configuration.model.support.email.EmailProperties;
import org.apereo.cas.configuration.model.support.pm.PasswordManagementProperties;
import org.apereo.cas.pm.PasswordManagementService;
import org.apereo.cas.util.io.CommunicationsManager;
import org.apereo.cas.web.flow.CasWebflowConfigurer;
import org.apereo.cas.web.support.WebUtils;
import org.springframework.webflow.action.AbstractAction;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;

import javax.servlet.http.HttpServletRequest;

/**
 * This is {@link SendPasswordResetInstructionsAction}.
 *
 * @author Misagh Moayyed
 * @since 5.0.0
 */
@Slf4j
@RequiredArgsConstructor
public class SendPasswordResetInstructionsAction extends AbstractAction {
    /**
     * Param name for the token.
     */
    public static final String PARAMETER_NAME_TOKEN = "pswdrst";

    private final CasConfigurationProperties casProperties;
    private final CommunicationsManager communicationsManager;
    private final PasswordManagementService passwordManagementService;

    @Override
    protected Event doExecute(final RequestContext requestContext) {
        communicationsManager.validate();
        if (!communicationsManager.isMailSenderDefined()) {
            return error();
        }
        final PasswordManagementProperties pm = casProperties.getAuthn().getPm();
        final HttpServletRequest request = WebUtils.getHttpServletRequestFromExternalWebflowContext(requestContext);
        final String username = request.getParameter("username");
        if (StringUtils.isBlank(username)) {
            LOGGER.warn("No username is provided");
            return error();
        }

        final String to = passwordManagementService.findEmail(username);
        if (StringUtils.isBlank(to)) {
            LOGGER.warn("No recipient is provided");
            return error();
        }

        final String url = buildPasswordResetUrl(username, passwordManagementService, casProperties);

        LOGGER.debug("Generated password reset URL [{}]; Link is only active for the next [{}] minute(s)", url,
            pm.getReset().getExpirationMinutes());
        if (sendPasswordResetEmailToAccount(to, url)) {
            return success();
        }
        LOGGER.error("Failed to notify account [{}]", to);
        return error();
    }

    /**
     * Utility method to generate a password reset URL.
     *
     * @param username                  username
     * @param passwordManagementService passwordManagementService
     * @param casProperties             casProperties
     * @return URL a user can use to start the password reset process
     */
    public static String buildPasswordResetUrl(final String username,
                                               final PasswordManagementService passwordManagementService,
                                               final CasConfigurationProperties casProperties) {
        final String token = passwordManagementService.createToken(username);
        return casProperties.getServer().getPrefix()
            .concat('/' + CasWebflowConfigurer.FLOW_ID_LOGIN + '?' + PARAMETER_NAME_TOKEN + '=').concat(token);
    }

    /**
     * Send password reset email to account.
     *
     * @param to  the to
     * @param url the url
     * @return true/false
     */
    protected boolean sendPasswordResetEmailToAccount(final String to, final String url) {
        final EmailProperties reset = casProperties.getAuthn().getPm().getReset().getMail();
        final String text = String.format(reset.getText(), url);
        return this.communicationsManager.email(text, reset.getFrom(),
            reset.getSubject(),
            to,
            reset.getCc(),
            reset.getBcc());
    }
}

@@ -1127,31 +1127,90 @@
 drst%22;%0A%0A    
-private
+/**%0A     * The CAS configuration properties.%0A     */%0A    protected
  final CasCo
@@ -1246,27 +1246,95 @@
 erties;%0A
+%0A
     
-private
+/**%0A     * The communication manager for SMS/emails.%0A     */%0A    protected
  final C
@@ -1381,19 +1381,78 @@
 er;%0A
+%0A
     
-private
+/**%0A     * The password management service.%0A     */%0A    protected
  fin

Make classes more easily overridable by defining protected variables (#3372)