AuthController.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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
  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. * @param string $username
  61. * @return string
  62. */
  63. protected function getLdapRdn(string $username)
  64. {
  65. $bindString = ($this->config['ldap']['rdn_attribute'] ?? 'uid=').addslashes($username);
  66. if ($this->config['ldap']['user_domain'] !== null) {
  67. $bindString .= ','.$this->config['ldap']['user_domain'];
  68. }
  69. if ($this->config['ldap']['base_domain'] !== null) {
  70. $bindString .= ','.$this->config['ldap']['base_domain'];
  71. }
  72. return $bindString;
  73. }
  74. }