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