-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated laravel to 5.2 and started ldap implementation
- Loading branch information
1 parent
e27a630
commit 14ca317
Showing
20 changed files
with
907 additions
and
474 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php namespace BookStack\Exceptions; | ||
|
||
|
||
use Exception; | ||
|
||
class LdapException extends Exception | ||
{ | ||
|
||
} |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace BookStack\Providers; | ||
|
||
use Auth; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class AuthServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Register the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
Auth::provider('ldap', function($app, array $config) { | ||
return new LdapUserProvider($config['model']); | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace BookStack\Providers; | ||
|
||
|
||
use BookStack\User; | ||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use Illuminate\Contracts\Auth\UserProvider; | ||
|
||
class LdapUserProvider implements UserProvider | ||
{ | ||
|
||
/** | ||
* The user model. | ||
* | ||
* @var string | ||
*/ | ||
protected $model; | ||
|
||
|
||
/** | ||
* LdapUserProvider constructor. | ||
* @param $model | ||
*/ | ||
public function __construct($model) | ||
{ | ||
$this->model = $model; | ||
} | ||
|
||
/** | ||
* Create a new instance of the model. | ||
* | ||
* @return \Illuminate\Database\Eloquent\Model | ||
*/ | ||
public function createModel() | ||
{ | ||
$class = '\\'.ltrim($this->model, '\\'); | ||
|
||
return new $class; | ||
} | ||
|
||
|
||
/** | ||
* Retrieve a user by their unique identifier. | ||
* | ||
* @param mixed $identifier | ||
* @return \Illuminate\Contracts\Auth\Authenticatable|null | ||
*/ | ||
public function retrieveById($identifier) | ||
{ | ||
return $this->createModel()->newQuery()->find($identifier); | ||
} | ||
|
||
/** | ||
* Retrieve a user by their unique identifier and "remember me" token. | ||
* | ||
* @param mixed $identifier | ||
* @param string $token | ||
* @return \Illuminate\Contracts\Auth\Authenticatable|null | ||
*/ | ||
public function retrieveByToken($identifier, $token) | ||
{ | ||
$model = $this->createModel(); | ||
|
||
return $model->newQuery() | ||
->where($model->getAuthIdentifierName(), $identifier) | ||
->where($model->getRememberTokenName(), $token) | ||
->first(); | ||
} | ||
|
||
|
||
/** | ||
* Update the "remember me" token for the given user in storage. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Authenticatable $user | ||
* @param string $token | ||
* @return void | ||
*/ | ||
public function updateRememberToken(Authenticatable $user, $token) | ||
{ | ||
$user->setRememberToken($token); | ||
|
||
$user->save(); | ||
} | ||
|
||
/** | ||
* Retrieve a user by the given credentials. | ||
* | ||
* @param array $credentials | ||
* @return \Illuminate\Contracts\Auth\Authenticatable|null | ||
*/ | ||
public function retrieveByCredentials(array $credentials) | ||
{ | ||
// TODO: Implement retrieveByCredentials() method. | ||
|
||
// Get user via LDAP | ||
|
||
// Search current user base by looking up a uid | ||
|
||
// If not exists create a new user instance with attached role | ||
// but do not store it in the database yet | ||
|
||
// | ||
} | ||
|
||
/** | ||
* Validate a user against the given credentials. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Authenticatable $user | ||
* @param array $credentials | ||
* @return bool | ||
*/ | ||
public function validateCredentials(Authenticatable $user, array $credentials) | ||
{ | ||
// TODO: Implement validateCredentials() method. | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php namespace BookStack\Services; | ||
|
||
|
||
use BookStack\Exceptions\LdapException; | ||
|
||
class LdapService | ||
{ | ||
|
||
public function getUserDetails($userName) | ||
{ | ||
|
||
if(!function_exists('ldap_connect')) { | ||
throw new LdapException('LDAP PHP extension not installed'); | ||
} | ||
|
||
|
||
$ldapServer = explode(':', config('services.ldap.server')); | ||
$ldapConnection = ldap_connect($ldapServer[0], count($ldapServer) > 1 ? $ldapServer[1] : 389); | ||
|
||
if ($ldapConnection === false) { | ||
throw new LdapException('Cannot connect to ldap server, Initial connection failed'); | ||
} | ||
|
||
// Options | ||
|
||
ldap_set_option($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, 3); // TODO - make configurable | ||
|
||
$ldapDn = config('services.ldap.dn'); | ||
$ldapPass = config('services.ldap.pass'); | ||
$isAnonymous = ($ldapDn === false || $ldapPass === false); | ||
if ($isAnonymous) { | ||
$ldapBind = ldap_bind($ldapConnection); | ||
} else { | ||
$ldapBind = ldap_bind($ldapConnection, $ldapDn, $ldapPass); | ||
} | ||
|
||
if (!$ldapBind) throw new LdapException('LDAP access failed using ' . $isAnonymous ? ' anonymous bind.' : ' given dn & pass details'); | ||
|
||
// Find user | ||
$userFilter = $this->buildFilter(config('services.ldap.user_filter'), ['user' => $userName]); | ||
//dd($userFilter); | ||
$baseDn = config('services.ldap.base_dn'); | ||
$ldapSearch = ldap_search($ldapConnection, $baseDn, $userFilter); | ||
$users = ldap_get_entries($ldapConnection, $ldapSearch); | ||
|
||
dd($users); | ||
} | ||
|
||
|
||
private function buildFilter($filterString, $attrs) | ||
{ | ||
$newAttrs = []; | ||
foreach ($attrs as $key => $attrText) { | ||
$newKey = '${'.$key.'}'; | ||
$newAttrs[$newKey] = $attrText; | ||
} | ||
return strtr($filterString, $newAttrs); | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.