| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @package     Mautic | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright   2019 Monogramm. All rights reserved | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author      Monogramm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @contributor      enguerr | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @link        https://www.monogramm.io | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @link        https://www.septeo.fr | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @license     GNU/AGPLv3 http://www.gnu.org/licenses/agpl.html | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace MauticPlugin\MauticLdapAuthBundle\EventListener; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Mautic\CoreBundle\Helper\CoreParametersHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Mautic\PluginBundle\Integration\AbstractSsoFormIntegration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Mautic\UserBundle\Entity\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Mautic\UserBundle\Event\AuthenticationEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Mautic\UserBundle\UserEvents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\HttpFoundation\RedirectResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Symfony\Component\Security\Core\AuthenticationEvents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * Class UserSubscriber | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | class UserSubscriber implements EventSubscriberInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var CoreParametersHelper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private $parametersHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $supportedServices = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         'LdapAuth', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function __construct(CoreParametersHelper $parametersHelper) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->parametersHelper = $parametersHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public static function getSubscribedEvents() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             UserEvents::USER_FORM_AUTHENTICATION => array('onUserFormAuthentication', 0), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             UserEvents::USER_PRE_AUTHENTICATION  => array('onUserSsoAuthentication', 0), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Authenticate via the form using users defined in LDAP server(s). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param AuthenticationEvent $event | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return bool|void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     public function onUserSsoAuthentication(AuthenticationEvent $event) | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         $request = Request::createFromGlobals(); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         $username = $request->server->get('PHP_AUTH_USER'); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         $password = $request->server->get('PHP_AUTH_PW'); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         $integration = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         $result = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         if ($authenticatingService = $event->getAuthenticatingService()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             if (in_array($authenticatingService, $this->supportedServices) | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |                 && $integration = $event->getIntegration($authenticatingService)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |                 $result = $this->authenticateService($integration, $username, $password); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             foreach ($this->supportedServices as $supportedService) { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                 if ($integration = $event->getIntegration($supportedService)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                     $authenticatingService = $supportedService; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                     $result = $this->authenticateService($integration, $username, $password); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                     break; | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         if ($integration && $result instanceof User) { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             $event->setIsAuthenticated($authenticatingService, $result, $integration->shouldAutoCreateNewUser()); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         } elseif ($result instanceof Response) { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             $event->setResponse($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } // else do nothing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * Authenticate via the form using users defined in LDAP server(s). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @param AuthenticationEvent $event | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @return bool|void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function onUserFormAuthentication(AuthenticationEvent $event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $username = $event->getUsername(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $password = $event->getToken()->getCredentials(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $integration = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $result = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if ($authenticatingService = $event->getAuthenticatingService()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             if (in_array($authenticatingService, $this->supportedServices) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 && $integration = $event->getIntegration($authenticatingService)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 $result = $this->authenticateService($integration, $username, $password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             foreach ($this->supportedServices as $supportedService) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 if ($integration = $event->getIntegration($supportedService)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                     $authenticatingService = $supportedService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                     $result = $this->authenticateService($integration, $username, $password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         if ($integration && $result instanceof User) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $event->setIsAuthenticated($authenticatingService, $result, $integration->shouldAutoCreateNewUser()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } elseif ($result instanceof Response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $event->setResponse($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } // else do nothing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param AbstractSsoFormIntegration $integration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param string                     $username | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param string                     $password | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @return bool|RedirectResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     private function authenticateService(AbstractSsoFormIntegration $integration, $username, $password) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $settings = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             'hostname'      => $this->parametersHelper->getParameter('ldap_auth_host'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             'port'          => $this->parametersHelper->getParameter('ldap_auth_port', 389), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             'ssl'           => $this->parametersHelper->getParameter('ldap_auth_ssl', false), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             'starttls'      => $this->parametersHelper->getParameter('ldap_auth_starttls', true), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             'version'       => $this->parametersHelper->getParameter('ldap_auth_version', 3), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             // TODO Coming feature: Bind DN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             'bind_dn'       => $this->parametersHelper->getParameter('ldap_auth_bind_dn'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             'bind_passwd'   => $this->parametersHelper->getParameter('ldap_auth_bind_passwd'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             'base_dn'       => $this->parametersHelper->getParameter('ldap_auth_base_dn'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             'user_query'    => $this->parametersHelper->getParameter('ldap_auth_user_query', ''), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             'is_ad'         => $this->parametersHelper->getParameter('ldap_auth_isactivedirectory', false), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             'ad_domain'     => $this->parametersHelper->getParameter('ldap_auth_activedirectory_domain', null), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             'user_key'      => $this->parametersHelper->getParameter('ldap_auth_username_attribute', 'uid'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             'user_email'    => $this->parametersHelper->getParameter('ldap_auth_email_attribute', 'mail'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             'user_firstname'=> $this->parametersHelper->getParameter('ldap_auth_firstname_attribute', 'givenName'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             'user_lastname' => $this->parametersHelper->getParameter('ldap_auth_lastname_attribute', 'sn'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             'user_fullname' => $this->parametersHelper->getParameter('ldap_auth_fullname_attribute', 'displayName'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $parameters = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             'login'     => $username, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             'password'  => $password, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         if ($authenticatedUser = $integration->ssoAuthCallback($settings, $parameters)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             return $authenticatedUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 168 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |  |