Last active
April 10, 2023 12:32
-
-
Save JBlond/accd7b81a4ae3470daa1018f87b1f2b6 to your computer and use it in GitHub Desktop.
LdapAuth php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$ldap = new LdapAuth('ldap://10.10.10.10'); | |
if($ldap->auth($_POST['username'],$_POST['password'], 'example.local') === true){ | |
echo 'Access granted'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* LDAP Auth Handler | |
*/ | |
class LdapAuth | |
{ | |
/** | |
* @var false|resource | |
*/ | |
private $handle; | |
/** | |
* @param string $server | |
*/ | |
public function __construct(string $server='') | |
{ | |
$this->handle = ldap_connect($server); | |
ldap_set_option($this->handle, LDAP_OPT_PROTOCOL_VERSION, 3); | |
ldap_set_option($this->handle, LDAP_OPT_REFERRALS, 0); | |
} | |
/** | |
* @param string $userName | |
* @param string $password | |
* @param string $domainSuffix e.g. example.local | |
* @return bool | |
*/ | |
public function auth(string $userName, string $password, string $domainSuffix): bool | |
{ | |
$bind = ldap_bind($this->handle, $userName . '@' . $domainSuffix, $password); | |
return $bind === true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment