Created
October 24, 2018 18:49
-
-
Save eseyden/9fd0eda245bf1595d463f6e7e2590138 to your computer and use it in GitHub Desktop.
Laravel AD Auth
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\Auth; | |
use adLDAP\collections\adLDAPUserCollection; | |
use Illuminate\Contracts\Auth\Authenticatable; | |
class AdUser implements Authenticatable | |
{ | |
/** | |
* @var adLDAPUserCollection | |
*/ | |
private $adLDAPUserCollection; | |
protected $atributes; | |
public function __construct(adLDAPUserCollection $adLDAPUserCollection) | |
{ | |
$this->adLDAPUserCollection = $adLDAPUserCollection; | |
} | |
/** | |
* Get the name of the unique identifier for the user. | |
* | |
* @return string | |
*/ | |
public function getAuthIdentifierName() | |
{ | |
} | |
/** | |
* Get the unique identifier for the user. | |
* | |
* @return mixed | |
*/ | |
public function getAuthIdentifier() | |
{ | |
return $this->adLDAPUserCollection->userprincipalname; | |
} | |
/** | |
* Get the password for the user. | |
* | |
* @return string | |
*/ | |
public function getAuthPassword() | |
{ | |
} | |
/** | |
* Get the token value for the "remember me" session. | |
* | |
* @return string | |
*/ | |
public function getRememberToken() | |
{ | |
} | |
/** | |
* Set the token value for the "remember me" session. | |
* | |
* @param string $value | |
* @return void | |
*/ | |
public function setRememberToken($value) | |
{ | |
} | |
/** | |
* Get the column name for the "remember me" token. | |
* | |
* @return string | |
*/ | |
public function getRememberTokenName() | |
{ | |
} | |
} |
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\Auth; | |
use adLDAP\adLDAP; | |
use Illuminate\Contracts\Auth\Authenticatable; | |
use Illuminate\Contracts\Auth\UserProvider; | |
class AdUserProvider implements UserProvider | |
{ | |
/** | |
* @var adLDAP | |
*/ | |
private $adLDAP; | |
public function __construct(adLDAP $adLDAP) | |
{ | |
$this->adLDAP = $adLDAP; | |
} | |
/** | |
* Retrieve a user by their unique identifier. | |
* | |
* @param mixed $identifier | |
* @return \Illuminate\Contracts\Auth\Authenticatable|null | |
*/ | |
public function retrieveById($identifier) | |
{ | |
$user = $this->adLDAP->user()->infoCollection($identifier,['*']); | |
if($user !== false){ | |
return new AdUser($this->adLDAP->user()->infoCollection($identifier,['*'])); | |
} | |
} | |
/** | |
* Retrieve a user by 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) | |
{ | |
} | |
/** | |
* 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) | |
{ | |
} | |
/** | |
* Retrieve a user by the given credentials. | |
* | |
* @param array $credentials | |
* @return \Illuminate\Contracts\Auth\Authenticatable|null | |
*/ | |
public function retrieveByCredentials(array $credentials) | |
{ | |
$user = $this->adLDAP->user()->infoCollection($credentials['username'],['*']); | |
return new AdUser($user); | |
} | |
/** | |
* 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) | |
{ | |
if($this->adLDAP->authenticate($credentials['username'],$credentials['password'])) | |
{ | |
return $this->checkIfUserInAllowedGroups($credentials['username']); | |
} | |
return false; | |
} | |
private function checkIfUserInAllowedGroups($username) | |
{ | |
$authorizedGroups = config('adldap.authorizedGroups'); | |
/** @var adLDAP $adldap */ | |
foreach($authorizedGroups as $group) | |
{ | |
$authorized = $this->adLDAP->user()->inGroup($username,$group,true); | |
if($authorized) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
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\Providers; | |
use App\Auth\AdUserProvider; | |
use Illuminate\Support\Facades\Gate; | |
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; | |
class AuthServiceProvider extends ServiceProvider | |
{ | |
/** | |
* The policy mappings for the application. | |
* | |
* @var array | |
*/ | |
protected $policies = [ | |
'App\Model' => 'App\Policies\ModelPolicy', | |
]; | |
/** | |
* Register any authentication / authorization services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
$this->registerPolicies(); | |
// | |
} | |
public function register() | |
{ | |
\Auth::provider('custom',function($app){ | |
return $app->make(AdUserProvider::class); | |
}); | |
} | |
} |
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 SAIT\Support\ADLDAP; | |
use adLDAP\adLDAP; | |
use Illuminate\Support\ServiceProvider; | |
class Laravel5AdldapServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Indicates if loading of the provider is deferred. | |
* | |
* @var bool | |
*/ | |
protected $defer = false; | |
/** | |
* Run service provider boot operations. | |
*/ | |
public function boot() | |
{ | |
} | |
/** | |
* Register the service provider. | |
*/ | |
public function register() | |
{ | |
$config = $this->app['config']->get('adldap'); | |
// Bind the Adldap instance to the IoC | |
$this->app->bind('adldap', function () use ($config) { | |
// Verify configuration | |
if (is_null($config)) { | |
$message = 'Adldap configuration could not be found.'; | |
throw new AdldapConfigurationMissingException($message); | |
} | |
return new adLDAP($config['connection_settings']); | |
}); | |
$this->app->bind('adLDAP\adLDAP', 'adldap'); | |
} | |
/** | |
* Get the services provided by the provider. | |
* | |
* @return array | |
*/ | |
public function provides() | |
{ | |
return ['adldap']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment