AuthController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Controllers\Auth;
  3. use App\Controllers\Controller;
  4. use App\Web\Session;
  5. use App\Web\ValidationHelper;
  6. use Psr\Http\Message\ServerRequestInterface as Request;
  7. abstract class AuthController extends Controller
  8. {
  9. protected function checkRecaptcha(ValidationHelper $validator, Request $request)
  10. {
  11. $validator->callIf($this->getSetting('recaptcha_enabled') === 'on', function (Session $session) use (&$request) {
  12. $recaptcha = json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$this->getSetting('recaptcha_secret_key').'&response='.param($request, 'recaptcha_token')));
  13. if ($recaptcha->success && $recaptcha->score < 0.5) {
  14. $session->alert(lang('recaptcha_failed'), 'danger');
  15. return false;
  16. }
  17. return true;
  18. });
  19. return $validator;
  20. }
  21. /**
  22. * Connects to LDAP server and logs in with service account (if configured)
  23. * @return resource|false
  24. */
  25. public function ldapConnect()
  26. {
  27. if (!extension_loaded('ldap')) {
  28. $this->logger->error('The LDAP extension is not loaded.');
  29. return false;
  30. }
  31. // Building LDAP URI
  32. $ldapSchema=(@is_string($this->config['ldap']['schema'])) ?
  33. strtolower($this->config['ldap']['schema']) : 'ldap';
  34. $ldapURI="$ldapSchema://".$this->config['ldap']['host'].':'.$this->config['ldap']['port'];
  35. // Connecting to LDAP server
  36. $this->logger->debug("Connecting to $ldapURI");
  37. $server = ldap_connect($ldapURI);
  38. if ($server) {
  39. ldap_set_option($server, LDAP_OPT_PROTOCOL_VERSION, 3);
  40. ldap_set_option($server, LDAP_OPT_REFERRALS, 0);
  41. ldap_set_option($server, LDAP_OPT_NETWORK_TIMEOUT, 10);
  42. } else {
  43. $this->logger->error(ldap_error($server));
  44. return false;
  45. }
  46. // Upgrade to StartTLS
  47. $useStartTLS = @is_bool($this->config['ldap']['useStartTLS']) ? $this->config['ldap']['useStartTLS'] : false;
  48. if ( $useStartTLS === true) {
  49. if (ldap_start_tls($server) === false) {
  50. $this->logger-debug(ldap_error($server));
  51. $this->logger->error("Failed to establish secure LDAP swith StartTLS");
  52. return false;
  53. }
  54. }
  55. // Authenticating LDAP service account (if configured)
  56. $serviceAccountFQDN= (@is_string($this->config['ldap']['service_account_dn'])) ?
  57. $this->config['ldap']['service_account_dn'] : null;
  58. if (is_string($serviceAccountFQDN)) {
  59. if (ldap_bind($server,$serviceAccountFQDN,$this->config['ldap']['service_account_password']) === false) {
  60. $this->logger->debug(ldap_error($server));
  61. $this->logger->error("Bind with service account ($serviceAccountFQDN) failed.");
  62. return false;
  63. }
  64. }
  65. return $server;
  66. }
  67. /**
  68. * Returns User's LDAP DN
  69. * @param string $username
  70. * @package resource $server LDAP Server Resource
  71. * @return string|null
  72. */
  73. protected function getLdapRdn(string $username, $server)
  74. {
  75. //Dynamic LDAP User Binding
  76. if (@is_string($this->config['ldap']['search_filter'])) {
  77. //Replace ???? with username
  78. $searchFilter = str_replace('????', ldap_escape($username,null,LDAP_ESCAPE_FILTER), $this->config['ldap']['search_filter']);
  79. $ldapAddributes = array ('dn');
  80. $this->logger->debug("LDAP Search filter: $searchFilter");
  81. $ldapSearchResp = ldap_search(
  82. $server,
  83. $this->config['ldap']['base_domain'],
  84. $searchFilter,
  85. $ldapAddributes
  86. );
  87. if (!is_resource($ldapSearchResp) ) {
  88. $this->logger->debug(ldap_error($server));
  89. $this->logger->error("User LDAP search for user $username failed");
  90. return null;
  91. }
  92. if (ldap_count_entries($server, $ldapSearchResp) !== 1 ) {
  93. $this->logger->notice("LDAP search for $username not found or had multiple entries");
  94. return null;
  95. }
  96. $ldapEntry = ldap_first_entry($server, $ldapSearchResp);
  97. //Returns full DN
  98. $bindString = ldap_get_dn($server, $ldapEntry);
  99. } else {
  100. // Static LDAP Binding
  101. $bindString = ($this->config['ldap']['rdn_attribute'] ?? 'uid=').addslashes($username);
  102. if ($this->config['ldap']['user_domain'] !== null) {
  103. $bindString .= ','.$this->config['ldap']['user_domain'];
  104. }
  105. if ($this->config['ldap']['base_domain'] !== null) {
  106. $bindString .= ','.$this->config['ldap']['base_domain'];
  107. }
  108. //returns partial DN
  109. }
  110. return $bindString;
  111. }
  112. }