package org.apereo.cas.authentication.event;

import org.apereo.cas.authentication.principal.Principal;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.configuration.model.support.email.EmailProperties;
import org.apereo.cas.configuration.model.support.sms.SmsProperties;
import org.apereo.cas.support.events.AbstractCasEvent;
import org.apereo.cas.support.events.authentication.surrogate.CasSurrogateAuthenticationFailureEvent;
import org.apereo.cas.support.events.authentication.surrogate.CasSurrogateAuthenticationSuccessfulEvent;
import org.apereo.cas.util.io.CommunicationsManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.EventListener;

/**
 * This is {@link SurrogateAuthenticationEventListener}.
 *
 * @author Misagh Moayyed
 * @since 5.2.0
 */
public class SurrogateAuthenticationEventListener {
    private static final Logger LOGGER = LoggerFactory.getLogger(SurrogateAuthenticationEventListener.class);

    private final CommunicationsManager communicationsManager;
    private final CasConfigurationProperties casProperties;

    public SurrogateAuthenticationEventListener(final CommunicationsManager communicationsManager,
                                                final CasConfigurationProperties casProperties) {
        this.communicationsManager = communicationsManager;
        this.casProperties = casProperties;
    }

    /**
     * Handle failure event.
     *
     * @param event the event
     */
    @EventListener
    public void handleSurrogateAuthenticationFailureEvent(final CasSurrogateAuthenticationFailureEvent event) {
        notify(event.getPrincipal(), event);
    }

    /**
     * Handle success event.
     *
     * @param event the event
     */
    @EventListener
    public void handleSurrogateAuthenticationSuccessEvent(final CasSurrogateAuthenticationSuccessfulEvent event) {
        notify(event.getPrincipal(), event);
    }

    private void notify(final Principal principal, final AbstractCasEvent event) {
        final String eventDetails = event.toString();
        if (communicationsManager.isSmsSenderDefined()) {
            final SmsProperties sms = casProperties.getAuthn().getSurrogate().getSms();
            final String text = sms.getText().concat("\n").concat(eventDetails);
            communicationsManager.sms(sms.getFrom(), principal.getAttributes().get(sms.getAttributeName()).toString(), text);
        } else {
            LOGGER.trace("CAS is unable to send surrogate-authentication SMS messages given no settings are defined to account for servers, etc");
        }
        if (communicationsManager.isMailSenderDefined()) {
            final EmailProperties mail = casProperties.getAuthn().getSurrogate().getMail();
            final String to = principal.getAttributes().get(mail.getAttributeName()).toString();
            final String text = mail.getText().concat("\n").concat(eventDetails);
            this.communicationsManager.email(text, mail.getFrom(), mail.getSubject(), to, mail.getCc(), mail.getBcc());
        } else {
            LOGGER.trace("CAS is unable to send surrogate-authentication email messages given no settings are defined to account for servers, etc");
        }
    }
}

@@ -2810,16 +2810,83 @@
  String 
+emailAttribute = mail.getAttributeName();%0A            final Object 
 to = pri
@@ -2912,24 +2912,21 @@
 s().get(
+e
 mail
-.get
 Attribut
@@ -2930,28 +2930,45 @@
 bute
-Name()).toString();%0A
+);%0A            if (to != null) %7B%0A    
     
@@ -3049,32 +3049,36 @@
 s);%0A            
+    
 this.communicati
@@ -3137,16 +3137,27 @@
 ct(), to
+.toString()
 , mail.g
@@ -3180,16 +3180,166 @@
 Bcc());%0A
+            %7D else %7B%0A                LOGGER.trace(%22The principal has no %7B%7D attribute, cannot send email notification%22, emailAttribute);%0A            %7D%0A
         

Fix NPE on surrogate mail notification (#3133)