| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\ActiveDirectory\Authenticators; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\ActiveDirectory\Services\LDAPService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Control\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Control\Email\Email; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Control\HTTPRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Control\Session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Core\Config\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Core\Injector\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\ORM\ValidationResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\Security\Authenticator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SilverStripe\Security\Member; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use SilverStripe\Security\MemberAuthenticator\LogoutHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Class LDAPAuthenticator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * Authenticate a user against LDAP, without the single sign-on component. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * See SAMLAuthenticator for further information. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @package activedirectory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | class LDAPAuthenticator extends MemberAuthenticator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $name = 'LDAP'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * Set to 'yes' to indicate if this module should look up usernames in LDAP by matching the email addresses. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * CAVEAT #1: only set to 'yes' for systems that enforce email uniqueness. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * Otherwise only the first LDAP user with matching email will be accessible. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * CAVEAT #2: this is untested for systems that use LDAP with principal style usernames (i.e. [email protected]). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * The system will misunderstand emails for usernames with uncertain outcome. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @var string 'no' or 'yes' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     private static $allow_email_login = 'no'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * Set to 'yes' to fallback login attempts to {@link $fallback_authenticator}. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * This will occur if LDAP fails to authenticate the user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @var string 'no' or 'yes' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     private static $fallback_authenticator = 'no'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * The class of {@link Authenticator} to use as the fallback authenticator. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     private static $fallback_authenticator_class = MemberAuthenticator::class; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public static function get_name() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return Config::inst()->get(self::class, 'name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param Controller $controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return LDAPLoginForm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public static function get_login_form(Controller $controller) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return new LDAPLoginForm($controller, 'LoginForm'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * Performs the login, but will also create and sync the Member record on-the-fly, if not found. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param ValidationResult|null $result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @return bool|Member | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @internal param Form $form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function authenticate(array $data, HTTPRequest $request, ValidationResult &$result = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         /** @var LDAPService $service */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $service = Injector::inst()->get(LDAPService::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $login = trim($data['Login']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         if (Email::is_valid_address($login)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             if (Config::inst()->get(self::class, 'allow_email_login') != 'yes') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $result->addError( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                     _t( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                         'LDAPAuthenticator.PLEASEUSEUSERNAME', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                         'Please enter your username instead of your email to log in.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $username = $service->getUsernameByEmail($login); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             // No user found with this email. | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 107 |  | View Code Duplication |             if (!$username) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 if (Config::inst()->get(self::class, 'fallback_authenticator') === 'yes') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                     if ($fallbackMember = $this->fallbackAuthenticate($data, $request)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                             return $fallbackMember; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 $result->addError(_t('LDAPAuthenticator.INVALIDCREDENTIALS', 'Invalid credentials')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $username = $login; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $serviceAuthenticationResult = $service->authenticate($username, $data['Password']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $success = $serviceAuthenticationResult['success'] === true; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 125 |  | View Code Duplication |         if (!$success) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             if (Config::inst()->get(self::class, 'fallback_authenticator') === 'yes') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 $fallbackMember = $this->fallbackAuthenticate($data, $request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 if ($fallbackMember) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     return $fallbackMember; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             $result->addError($serviceAuthenticationResult['message']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $data = $service->getUserByUsername($serviceAuthenticationResult['identity']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         if (!$data) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             $result->addError( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 _t( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                     'LDAPAuthenticator.PROBLEMFINDINGDATA', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                     'There was a problem retrieving your user data' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         // LDAPMemberExtension::memberLoggedIn() will update any other AD attributes mapped to Member fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $member = Member::get()->filter('GUID', $data['objectguid'])->limit(1)->first(); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 151 |  | View Code Duplication |         if (!($member && $member->exists())) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             $member = new Member(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             $member->GUID = $data['objectguid']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         // Update the users from LDAP so we are sure that the email is correct. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         // This will also write the Member record. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $service->updateMemberFromLDAP($member); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $request->getSession()->clear('BackURL'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         return $member; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * Try to authenticate using the fallback authenticator. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * @param HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @return null|Member | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @internal param null|Form $form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     protected function fallbackAuthenticate($data, HTTPRequest $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         $authenticatorClass = Config::inst()->get(self::class, 'fallback_authenticator_class'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         if ($authenticator = Injector::inst()->get($authenticatorClass)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             $result = call_user_func( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |                     $authenticator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |                     'authenticate' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |                 $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |                 $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     public function getLoginHandler($link) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         return LDAPLoginHandler::create($link, $this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |      * @inheritdoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 199 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 200 |  |  |     public function supportedServices() | 
            
                                                        
            
                                    
            
            
                | 201 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 202 |  |  |         return Authenticator::LOGIN | Authenticator::LOGOUT; | 
            
                                                        
            
                                    
            
            
                | 203 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 204 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 205 |  |  |  | 
            
                        
This check marks private properties in classes that are never used. Those properties can be removed.