PageRenderTime 31ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Auth/Adapter/Ldap.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 528 lines | 260 code | 50 blank | 218 comment | 39 complexity | 33c58608d52227b2843bb4eb5b82c062 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Auth
  17. * @subpackage Zend_Auth_Adapter
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Ldap.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @see Zend_Auth_Adapter_Interface
  24. */
  25. require_once 'Zend/Auth/Adapter/Interface.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Auth
  29. * @subpackage Zend_Auth_Adapter
  30. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Auth_Adapter_Ldap implements Zend_Auth_Adapter_Interface
  34. {
  35. /**
  36. * The Zend_Ldap context.
  37. *
  38. * @var Zend_Ldap
  39. */
  40. protected $_ldap = null;
  41. /**
  42. * The array of arrays of Zend_Ldap options passed to the constructor.
  43. *
  44. * @var array
  45. */
  46. protected $_options = null;
  47. /**
  48. * The username of the account being authenticated.
  49. *
  50. * @var string
  51. */
  52. protected $_username = null;
  53. /**
  54. * The password of the account being authenticated.
  55. *
  56. * @var string
  57. */
  58. protected $_password = null;
  59. /**
  60. * The DN of the authenticated account. Used to retrieve the account entry on request.
  61. *
  62. * @var string
  63. */
  64. protected $_authenticatedDn = null;
  65. /**
  66. * Constructor
  67. *
  68. * @param array $options An array of arrays of Zend_Ldap options
  69. * @param string $username The username of the account being authenticated
  70. * @param string $password The password of the account being authenticated
  71. * @return void
  72. */
  73. public function __construct(array $options = array(), $username = null, $password = null)
  74. {
  75. $this->setOptions($options);
  76. if ($username !== null) {
  77. $this->setUsername($username);
  78. }
  79. if ($password !== null) {
  80. $this->setPassword($password);
  81. }
  82. }
  83. /**
  84. * Returns the array of arrays of Zend_Ldap options of this adapter.
  85. *
  86. * @return array|null
  87. */
  88. public function getOptions()
  89. {
  90. return $this->_options;
  91. }
  92. /**
  93. * Sets the array of arrays of Zend_Ldap options to be used by
  94. * this adapter.
  95. *
  96. * @param array $options The array of arrays of Zend_Ldap options
  97. * @return Zend_Auth_Adapter_Ldap Provides a fluent interface
  98. */
  99. public function setOptions($options)
  100. {
  101. $this->_options = is_array($options) ? $options : array();
  102. return $this;
  103. }
  104. /**
  105. * Returns the username of the account being authenticated, or
  106. * NULL if none is set.
  107. *
  108. * @return string|null
  109. */
  110. public function getUsername()
  111. {
  112. return $this->_username;
  113. }
  114. /**
  115. * Sets the username for binding
  116. *
  117. * @param string $username The username for binding
  118. * @return Zend_Auth_Adapter_Ldap Provides a fluent interface
  119. */
  120. public function setUsername($username)
  121. {
  122. $this->_username = (string) $username;
  123. return $this;
  124. }
  125. /**
  126. * Returns the password of the account being authenticated, or
  127. * NULL if none is set.
  128. *
  129. * @return string|null
  130. */
  131. public function getPassword()
  132. {
  133. return $this->_password;
  134. }
  135. /**
  136. * Sets the passwort for the account
  137. *
  138. * @param string $password The password of the account being authenticated
  139. * @return Zend_Auth_Adapter_Ldap Provides a fluent interface
  140. */
  141. public function setPassword($password)
  142. {
  143. $this->_password = (string) $password;
  144. return $this;
  145. }
  146. /**
  147. * setIdentity() - set the identity (username) to be used
  148. *
  149. * Proxies to {@see setUsername()}
  150. *
  151. * Closes ZF-6813
  152. *
  153. * @param string $identity
  154. * @return Zend_Auth_Adapter_Ldap Provides a fluent interface
  155. */
  156. public function setIdentity($identity)
  157. {
  158. return $this->setUsername($identity);
  159. }
  160. /**
  161. * setCredential() - set the credential (password) value to be used
  162. *
  163. * Proxies to {@see setPassword()}
  164. *
  165. * Closes ZF-6813
  166. *
  167. * @param string $credential
  168. * @return Zend_Auth_Adapter_Ldap Provides a fluent interface
  169. */
  170. public function setCredential($credential)
  171. {
  172. return $this->setPassword($credential);
  173. }
  174. /**
  175. * Returns the LDAP Object
  176. *
  177. * @return Zend_Ldap The Zend_Ldap object used to authenticate the credentials
  178. */
  179. public function getLdap()
  180. {
  181. if ($this->_ldap === null) {
  182. /**
  183. * @see Zend_Ldap
  184. */
  185. require_once 'Zend/Ldap.php';
  186. $this->_ldap = new Zend_Ldap();
  187. }
  188. return $this->_ldap;
  189. }
  190. /**
  191. * Set an Ldap connection
  192. *
  193. * @param Zend_Ldap $ldap An existing Ldap object
  194. * @return Zend_Auth_Adapter_Ldap Provides a fluent interface
  195. */
  196. public function setLdap(Zend_Ldap $ldap)
  197. {
  198. $this->_ldap = $ldap;
  199. $this->setOptions(array($ldap->getOptions()));
  200. return $this;
  201. }
  202. /**
  203. * Returns a domain name for the current LDAP options. This is used
  204. * for skipping redundant operations (e.g. authentications).
  205. *
  206. * @return string
  207. */
  208. protected function _getAuthorityName()
  209. {
  210. $options = $this->getLdap()->getOptions();
  211. $name = $options['accountDomainName'];
  212. if (!$name)
  213. $name = $options['accountDomainNameShort'];
  214. return $name ? $name : '';
  215. }
  216. /**
  217. * Authenticate the user
  218. *
  219. * @throws Zend_Auth_Adapter_Exception
  220. * @return Zend_Auth_Result
  221. */
  222. public function authenticate()
  223. {
  224. /**
  225. * @see Zend_Ldap_Exception
  226. */
  227. require_once 'Zend/Ldap/Exception.php';
  228. $messages = array();
  229. $messages[0] = ''; // reserved
  230. $messages[1] = ''; // reserved
  231. $username = $this->_username;
  232. $password = $this->_password;
  233. if (!$username) {
  234. $code = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
  235. $messages[0] = 'A username is required';
  236. return new Zend_Auth_Result($code, '', $messages);
  237. }
  238. if (!$password) {
  239. /* A password is required because some servers will
  240. * treat an empty password as an anonymous bind.
  241. */
  242. $code = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
  243. $messages[0] = 'A password is required';
  244. return new Zend_Auth_Result($code, '', $messages);
  245. }
  246. $ldap = $this->getLdap();
  247. $code = Zend_Auth_Result::FAILURE;
  248. $messages[0] = "Authority not found: $username";
  249. $failedAuthorities = array();
  250. /* Iterate through each server and try to authenticate the supplied
  251. * credentials against it.
  252. */
  253. foreach ($this->_options as $name => $options) {
  254. if (!is_array($options)) {
  255. /**
  256. * @see Zend_Auth_Adapter_Exception
  257. */
  258. require_once 'Zend/Auth/Adapter/Exception.php';
  259. throw new Zend_Auth_Adapter_Exception('Adapter options array not an array');
  260. }
  261. $adapterOptions = $this->_prepareOptions($ldap, $options);
  262. $dname = '';
  263. try {
  264. if ($messages[1])
  265. $messages[] = $messages[1];
  266. $messages[1] = '';
  267. $messages[] = $this->_optionsToString($options);
  268. $dname = $this->_getAuthorityName();
  269. if (isset($failedAuthorities[$dname])) {
  270. /* If multiple sets of server options for the same domain
  271. * are supplied, we want to skip redundant authentications
  272. * where the identity or credentials where found to be
  273. * invalid with another server for the same domain. The
  274. * $failedAuthorities array tracks this condition (and also
  275. * serves to supply the original error message).
  276. * This fixes issue ZF-4093.
  277. */
  278. $messages[1] = $failedAuthorities[$dname];
  279. $messages[] = "Skipping previously failed authority: $dname";
  280. continue;
  281. }
  282. $canonicalName = $ldap->getCanonicalAccountName($username);
  283. $ldap->bind($canonicalName, $password);
  284. /*
  285. * Fixes problem when authenticated user is not allowed to retrieve
  286. * group-membership information or own account.
  287. * This requires that the user specified with "username" and optionally
  288. * "password" in the Zend_Ldap options is able to retrieve the required
  289. * information.
  290. */
  291. $requireRebind = false;
  292. if (isset($options['username'])) {
  293. $ldap->bind();
  294. $requireRebind = true;
  295. }
  296. $dn = $ldap->getCanonicalAccountName($canonicalName, Zend_Ldap::ACCTNAME_FORM_DN);
  297. $groupResult = $this->_checkGroupMembership($ldap, $canonicalName, $dn, $adapterOptions);
  298. if ($groupResult === true) {
  299. $this->_authenticatedDn = $dn;
  300. $messages[0] = '';
  301. $messages[1] = '';
  302. $messages[] = "$canonicalName authentication successful";
  303. if ($requireRebind === true) {
  304. // rebinding with authenticated user
  305. $ldap->bind($dn, $password);
  306. }
  307. return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $canonicalName, $messages);
  308. } else {
  309. $messages[0] = 'Account is not a member of the specified group';
  310. $messages[1] = $groupResult;
  311. $failedAuthorities[$dname] = $groupResult;
  312. }
  313. } catch (Zend_Ldap_Exception $zle) {
  314. /* LDAP based authentication is notoriously difficult to diagnose. Therefore
  315. * we bend over backwards to capture and record every possible bit of
  316. * information when something goes wrong.
  317. */
  318. $err = $zle->getCode();
  319. if ($err == Zend_Ldap_Exception::LDAP_X_DOMAIN_MISMATCH) {
  320. /* This error indicates that the domain supplied in the
  321. * username did not match the domains in the server options
  322. * and therefore we should just skip to the next set of
  323. * server options.
  324. */
  325. continue;
  326. } else if ($err == Zend_Ldap_Exception::LDAP_NO_SUCH_OBJECT) {
  327. $code = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
  328. $messages[0] = "Account not found: $username";
  329. $failedAuthorities[$dname] = $zle->getMessage();
  330. } else if ($err == Zend_Ldap_Exception::LDAP_INVALID_CREDENTIALS) {
  331. $code = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
  332. $messages[0] = 'Invalid credentials';
  333. $failedAuthorities[$dname] = $zle->getMessage();
  334. } else {
  335. $line = $zle->getLine();
  336. $messages[] = $zle->getFile() . "($line): " . $zle->getMessage();
  337. $messages[] = str_replace($password, '*****', $zle->getTraceAsString());
  338. $messages[0] = 'An unexpected failure occurred';
  339. }
  340. $messages[1] = $zle->getMessage();
  341. }
  342. }
  343. $msg = isset($messages[1]) ? $messages[1] : $messages[0];
  344. $messages[] = "$username authentication failed: $msg";
  345. return new Zend_Auth_Result($code, $username, $messages);
  346. }
  347. /**
  348. * Sets the LDAP specific options on the Zend_Ldap instance
  349. *
  350. * @param Zend_Ldap $ldap
  351. * @param array $options
  352. * @return array of auth-adapter specific options
  353. */
  354. protected function _prepareOptions(Zend_Ldap $ldap, array $options)
  355. {
  356. $adapterOptions = array(
  357. 'group' => null,
  358. 'groupDn' => $ldap->getBaseDn(),
  359. 'groupScope' => Zend_Ldap::SEARCH_SCOPE_SUB,
  360. 'groupAttr' => 'cn',
  361. 'groupFilter' => 'objectClass=groupOfUniqueNames',
  362. 'memberAttr' => 'uniqueMember',
  363. 'memberIsDn' => true
  364. );
  365. foreach ($adapterOptions as $key => $value) {
  366. if (array_key_exists($key, $options)) {
  367. $value = $options[$key];
  368. unset($options[$key]);
  369. switch ($key) {
  370. case 'groupScope':
  371. $value = (int)$value;
  372. if (in_array($value, array(Zend_Ldap::SEARCH_SCOPE_BASE,
  373. Zend_Ldap::SEARCH_SCOPE_ONE, Zend_Ldap::SEARCH_SCOPE_SUB), true)) {
  374. $adapterOptions[$key] = $value;
  375. }
  376. break;
  377. case 'memberIsDn':
  378. $adapterOptions[$key] = ($value === true ||
  379. $value === '1' || strcasecmp($value, 'true') == 0);
  380. break;
  381. default:
  382. $adapterOptions[$key] = trim($value);
  383. break;
  384. }
  385. }
  386. }
  387. $ldap->setOptions($options);
  388. return $adapterOptions;
  389. }
  390. /**
  391. * Checks the group membership of the bound user
  392. *
  393. * @param Zend_Ldap $ldap
  394. * @param string $canonicalName
  395. * @param string $dn
  396. * @param array $adapterOptions
  397. * @return string|true
  398. */
  399. protected function _checkGroupMembership(Zend_Ldap $ldap, $canonicalName, $dn, array $adapterOptions)
  400. {
  401. if ($adapterOptions['group'] === null) {
  402. return true;
  403. }
  404. if ($adapterOptions['memberIsDn'] === false) {
  405. $user = $canonicalName;
  406. } else {
  407. $user = $dn;
  408. }
  409. /**
  410. * @see Zend_Ldap_Filter
  411. */
  412. require_once 'Zend/Ldap/Filter.php';
  413. $groupName = Zend_Ldap_Filter::equals($adapterOptions['groupAttr'], $adapterOptions['group']);
  414. $membership = Zend_Ldap_Filter::equals($adapterOptions['memberAttr'], $user);
  415. $group = Zend_Ldap_Filter::andFilter($groupName, $membership);
  416. $groupFilter = $adapterOptions['groupFilter'];
  417. if (!empty($groupFilter)) {
  418. $group = $group->addAnd($groupFilter);
  419. }
  420. $result = $ldap->count($group, $adapterOptions['groupDn'], $adapterOptions['groupScope']);
  421. if ($result === 1) {
  422. return true;
  423. } else {
  424. return 'Failed to verify group membership with ' . $group->toString();
  425. }
  426. }
  427. /**
  428. * getAccountObject() - Returns the result entry as a stdClass object
  429. *
  430. * This resembles the feature {@see Zend_Auth_Adapter_DbTable::getResultRowObject()}.
  431. * Closes ZF-6813
  432. *
  433. * @param array $returnAttribs
  434. * @param array $omitAttribs
  435. * @return stdClass|boolean
  436. */
  437. public function getAccountObject(array $returnAttribs = array(), array $omitAttribs = array())
  438. {
  439. if (!$this->_authenticatedDn) {
  440. return false;
  441. }
  442. $returnObject = new stdClass();
  443. $returnAttribs = array_map('strtolower', $returnAttribs);
  444. $omitAttribs = array_map('strtolower', $omitAttribs);
  445. $returnAttribs = array_diff($returnAttribs, $omitAttribs);
  446. $entry = $this->getLdap()->getEntry($this->_authenticatedDn, $returnAttribs, true);
  447. foreach ($entry as $attr => $value) {
  448. if (in_array($attr, $omitAttribs)) {
  449. // skip attributes marked to be omitted
  450. continue;
  451. }
  452. if (is_array($value)) {
  453. $returnObject->$attr = (count($value) > 1) ? $value : $value[0];
  454. } else {
  455. $returnObject->$attr = $value;
  456. }
  457. }
  458. return $returnObject;
  459. }
  460. /**
  461. * Converts options to string
  462. *
  463. * @param array $options
  464. * @return string
  465. */
  466. private function _optionsToString(array $options)
  467. {
  468. $str = '';
  469. foreach ($options as $key => $val) {
  470. if ($key === 'password')
  471. $val = '*****';
  472. if ($str)
  473. $str .= ',';
  474. $str .= $key . '=' . $val;
  475. }
  476. return $str;
  477. }
  478. }