AuthController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * @return bool|false|resource
  23. */
  24. public function ldapConnect()
  25. {
  26. if (!extension_loaded('ldap')) {
  27. $this->logger->error('The LDAP extension is not loaded.');
  28. return false;
  29. }
  30. // Building LDAP URI
  31. $ldapSchema=array_key_exists('schema', $this->config['ldap']) ?
  32. strtolower($this->config['ldap']['schema']) : 'ldap';
  33. $ldapURI="$ldapSchema://".$this->config['ldap']['host'].':'.$this->config['ldap']['port'];
  34. // Connecting to LDAP server
  35. $server = ldap_connect($ldapURI);
  36. if ($server) {
  37. ldap_set_option($server, LDAP_OPT_PROTOCOL_VERSION, 3);
  38. ldap_set_option($server, LDAP_OPT_REFERRALS, 0);
  39. ldap_set_option($server, LDAP_OPT_NETWORK_TIMEOUT, 10);
  40. }
  41. // Upgrade to StartTLS
  42. if ($this->config['ldap']['useStartTLS'] === true) {
  43. if (ldap_start_tls($server) === false) {
  44. $this->logger-error("Failed to establish secure LDAP swith StartTLS");
  45. return false;
  46. }
  47. }
  48. // Authenticating LDAP service account (if configured)
  49. $serviceAccountFQDN= (array_key_exists('service_account_dn', $this->config['ldap'])) ?
  50. $this->config['ldap']['service_account_dn'] : null;
  51. if (is_string($serviceAccountFQDN)) {
  52. if (ldap_bind($server,$serviceAccountFQDN,$this->config['ldap']['service_account_password']) === false) {
  53. $this->logger->error("Bind with service account ($serviceAccountFQDN) failed.");
  54. return false;
  55. }
  56. }
  57. return $server;
  58. }
  59. /**
  60. * Returns User's LDAP DN
  61. * @param string $username
  62. * @package resource $server LDAP Server Resource
  63. * @return string|null
  64. */
  65. protected function getLdapRdn(string $username, $server)
  66. {
  67. //Dynamic LDAP User Binding
  68. if (@is_string($this->config['ldap']['search_filter'])) {
  69. //Replace ???? with username
  70. $searchFilter=str_replace('????', ldap_escape($username,null,LDAP_ESCAPE_FILTER), $this->config['ldap']['search_filter']);
  71. $ldapAddributes=array ('dn');
  72. $ldapSearchResp=ldap_search(
  73. $server,
  74. $this->config['ldap']['base_domain'],
  75. $searchFilter,
  76. $ldapAddributes
  77. );
  78. if (ldap_count_entries($server, $ldapSearchResp) !== 1 ) {
  79. $this->logger->warn("$username not found or had multiple entries");
  80. return null;
  81. }
  82. $ldapEntry = ldap_first_entry($server, $$ldapSearchResp);
  83. $bindString=@ldap_get_values($server, $ldapEntry, 'dn');
  84. } else {
  85. // Static LDAP Binding
  86. $bindString = ($this->config['ldap']['rdn_attribute'] ?? 'uid=').addslashes($username);
  87. if ($this->config['ldap']['user_domain'] !== null) {
  88. $bindString .= ','.$this->config['ldap']['user_domain'];
  89. }
  90. if ($this->config['ldap']['base_domain'] !== null) {
  91. $bindString .= ','.$this->config['ldap']['base_domain'];
  92. }
  93. }
  94. return $bindString;
  95. }
  96. }