Created
October 16, 2015 21:18
-
-
Save michaelachrisco/7f418abfe41c28d666a8 to your computer and use it in GitHub Desktop.
Open LDAP Login Object for Laravel 5.1
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 | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use App\Http\Requests; | |
use App\Http\Controllers\Controller; | |
// Simple Open LDAP Object for Auth | |
class OpenLDAPAuth { | |
public function authenticate($username, $password) | |
{ | |
if(empty($username) or empty($password)){ | |
return false; | |
} | |
$ldapRdn = $this->getLdapRdn($username); | |
$ldapconn=ldap_connect(Config::get('app.open_ldap_server');) or die("Could not connect to LDAP server."); | |
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3); | |
$result = false; | |
if ($ldapconn) | |
{ | |
try{ | |
// bind the connection | |
$bind = @ldap_bind($ldapconn, $ldapRdn, $password); | |
if($bind){ | |
// valid credentials | |
ldap_unbind($ldapconn); | |
return true; | |
} | |
else{ | |
// invalid credentials | |
ldap_unbind($ldapconn); | |
return false; | |
} | |
} | |
//If username/password are not correct, dont login | |
catch(Exception $e){ | |
return false; | |
} | |
} | |
else { | |
Log::error('Error connecting to LDAP.'); | |
} | |
ldap_unbind($ldapconn); | |
} | |
private function getLdapRdn($username) | |
{ | |
return "cn={$username},dc=openldap,dc=local"; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment