├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── config ├── adldap.php └── auth-ldap.php ├── phpunit.xml ├── src ├── AdLDAPLdapServer.php ├── AuthLdapServiceProvider.php ├── BaseLdapUser.php ├── BaseLdapUserProvider.php └── Contracts │ ├── LdapServer.php │ ├── LdapUser.php │ └── LdapUserProvider.php └── tests ├── AdLDAPLdapServerTest.php ├── BaseLdapUserProviderTest.php ├── BaseLdapUserTest.php └── TestCase.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.lock 3 | .DS_Store 4 | Thumbs.db 5 | /.idea 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | - hhvm 8 | 9 | before_script: 10 | - travis_retry composer self-update 11 | - travis_retry composer install --no-interaction --prefer-source --dev 12 | 13 | script: 14 | - vendor/bin/phpunit --verbose 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Adam Particka 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel-auth-ldap 2 | 3 | [![Build Status](https://travis-ci.org/aparticka/laravel-auth-ldap.png)](https://travis-ci.org/aparticka/laravel-auth-ldap) 4 | 5 | LDAP authentication driver for [Laravel 5](http://laravel.com) 6 | 7 | ## Installation 8 | 9 | ### Adding via Composer 10 | 11 | Add to composer.json and install with `composer install` 12 | 13 | { 14 | require: { 15 | "aparticka/laravel-auth-ldap": "dev-master" 16 | } 17 | } 18 | 19 | or use `composer require aparticka/laravel-auth-ldap` 20 | 21 | ### Add to Laravel 22 | 23 | Modify your `config/app.php` file and add the service provider to the providers array. 24 | 25 | 'LaravelAuthLdap\AuthLdapServiceProvider' 26 | 27 | Copy the configuration files to your app. 28 | 29 | php artisan vendor:publish --provider="LaravelAuthLdap\AuthLdapServiceProvider" 30 | 31 | Update your `config/auth.php` to use the `ldap` driver. 32 | 33 | 'driver' => 'ldap' 34 | 35 | ## Configuration 36 | 37 | There are two configuration files included, one for general options - `auth-ldap.php` and one for the included LDAP provider [adLDAP](https://github.com/adldap/adLDAP) - `adldap.php`. 38 | 39 | ### auth-ldap.php 40 | 41 | * `provider` `array` - secondary provider to be used for auth 42 | * `driver` `string` - the driver to use 43 | * `must_exist` `bool` - if the user must exist in the provider to log in 44 | * `convert_fields` `array` - maps dynamic properties on the `Authenticatable` user object 45 | * `credentials_fields` `array` - the field names used for user credentials 46 | * `username` `string` - the authentication field name used for the username 47 | * `password` `string` - the authentication field name used for the password 48 | * `username_field` `string` - the LDAP field used for the username 49 | 50 | ### adldap.php 51 | 52 | Configuration variables used in creation of the adLDAP client. [Documentation](https://github.com/adldap/adLDAP) 53 | 54 | ## Extending 55 | 56 | If you wish to extend any of the classes, just add your own service provider and bind your custom implementations to the provided interfaces. The provided implementations were designed to be extended so you can use them as a base to extend from if you wish. 57 | 58 | ## License 59 | 60 | laravel-auth-ldap is distributed under the terms of the MIT license. 61 | 62 | ## About 63 | 64 | Created by [Adam Particka (aparticka)](https://github.com/aparticka) 65 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aparticka/laravel-auth-ldap", 3 | "description": "Laravel 5 LDAP Authentication Driver", 4 | "require": { 5 | "php": ">=5.4.0", 6 | "strebl/adldap": "~4.0", 7 | "laravel/framework": "~5.0" 8 | }, 9 | "require-dev": { 10 | "mockery/mockery": "0.9.*", 11 | "phpunit/phpunit": "~4.5", 12 | "orchestra/testbench": "~3.0" 13 | }, 14 | "license": "MIT", 15 | "authors": [ 16 | { 17 | "name": "Adam Particka", 18 | "email": "a.particka@gmail.com" 19 | } 20 | ], 21 | "autoload": { 22 | "psr-4": { 23 | "LaravelAuthLdap\\": "src/" 24 | } 25 | }, 26 | "autoload-dev": { 27 | "psr-0": { 28 | "": "tests/" 29 | } 30 | }, 31 | "minimum-stability": "stable" 32 | } 33 | -------------------------------------------------------------------------------- /config/adldap.php: -------------------------------------------------------------------------------- 1 | '@mydomain.local', 7 | 'base_dn' => 'DC=mydomain,DC=local', 8 | 'domain_controllers' => [ 9 | 'dc01.mydomain.local', 10 | 'dc02.mydomain.local', 11 | ], 12 | 'admin_username' => null, 13 | 'admin_password' => null, 14 | 'real_primarygroup' => true, 15 | 'use_ssl' => false, 16 | 'use_tls' => false, 17 | 'recursive_groups' => true, 18 | 'ad_port' => adLDAP::ADLDAP_LDAP_PORT, 19 | ]; 20 | -------------------------------------------------------------------------------- /config/auth-ldap.php: -------------------------------------------------------------------------------- 1 | [ 5 | 'driver' => 'eloquent', 6 | 'must_exist' => false, 7 | ], 8 | 'convert_fields' => [ 9 | 'name' => 'displayname', 10 | ], 11 | 'credentials_fields' => [ 12 | 'username' => 'username', 13 | 'password' => 'password', 14 | ], 15 | 'username_field' => 'samaccountname', 16 | ]; 17 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 13 | ./tests/ 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/AdLDAPLdapServer.php: -------------------------------------------------------------------------------- 1 | adServer = $adServer; 24 | } 25 | 26 | public function retrieveByUsername($username) 27 | { 28 | $user = $this->adServer->user()->infoCollection($username); 29 | if ($user !== false) 30 | { 31 | $ldapUser = app('LaravelAuthLdap\Contracts\LdapUser'); 32 | $ldapUser->setUser($user); 33 | 34 | return $ldapUser; 35 | } 36 | } 37 | 38 | public function authenticate($username, $password) 39 | { 40 | return $this->adServer->authenticate($username, $password); 41 | } 42 | } -------------------------------------------------------------------------------- /src/AuthLdapServiceProvider.php: -------------------------------------------------------------------------------- 1 | app['auth']->extend('ldap', function($app) 11 | { 12 | return $app->make('LaravelAuthLdap\Contracts\LdapUserProvider'); 13 | }); 14 | } 15 | 16 | public function register() 17 | { 18 | $this->publishConfigs(); 19 | $this->registerLdapInterfaces(); 20 | } 21 | 22 | /** 23 | * Publish the config files used in the library. 24 | */ 25 | public function publishConfigs() 26 | { 27 | $configFiles = []; 28 | 29 | foreach (['auth-ldap.php', 'adldap.php'] as $configFile) 30 | { 31 | $configFiles[__DIR__ . '/../config/' . $configFile] = config_path($configFile); 32 | } 33 | 34 | $this->publishes($configFiles, 'config'); 35 | } 36 | 37 | /** 38 | * Bind the interfaces to their implementations in the service container. 39 | */ 40 | public function registerLdapInterfaces() 41 | { 42 | $this->app->bind('LaravelAuthLdap\Contracts\LdapServer', function($app) 43 | { 44 | $server = new AdLDAPLdapServer; 45 | $server->setAdServer(new adLDAP($app->config['adldap'])); 46 | 47 | return $server; 48 | }); 49 | 50 | $this->app->bind('LaravelAuthLdap\Contracts\LdapUser', function($app) 51 | { 52 | $user = new BaseLdapUser; 53 | $user->setConvertFields($app->config['auth-ldap.convert_fields']); 54 | $user->setUsernameField($app->config['auth-ldap.username_field']); 55 | 56 | return $user; 57 | }); 58 | 59 | $this->app->bind('LaravelAuthLdap\Contracts\LdapUserProvider', function($app) 60 | { 61 | $provider = new BaseLdapUserProvider; 62 | 63 | $driverName = array_get($app->config['auth-ldap'], 'provider.driver'); 64 | 65 | if ($driverName !== null) 66 | { 67 | $driver = $this->app['auth']->driver($driverName); 68 | 69 | $provider->setProvider($driver->getProvider()); 70 | } 71 | 72 | $provider->setLdapServer($app->make('LaravelAuthLdap\Contracts\LdapServer')); 73 | 74 | $mustExist = array_get($app->config['auth-ldap'], 'provider.must_exist'); 75 | $provider->setUserMustExistInProvider($mustExist === null ? false : $mustExist); 76 | 77 | $provider->setCredentialsFields($app->config['auth-ldap.credentials_fields']); 78 | 79 | return $provider; 80 | }); 81 | } 82 | 83 | } -------------------------------------------------------------------------------- /src/BaseLdapUser.php: -------------------------------------------------------------------------------- 1 | convertFields = $convertFields; 36 | } 37 | 38 | /** 39 | * Set the username field name. 40 | * 41 | * @param $usernameField 42 | */ 43 | public function setUsernameField($usernameField) 44 | { 45 | $this->usernameField = $usernameField; 46 | } 47 | 48 | /** 49 | * Set the user information. 50 | * 51 | * @param $user 52 | */ 53 | public function setUser($user) 54 | { 55 | $this->user = $user; 56 | } 57 | 58 | /** 59 | * Use the magic method to get to properties on the user object. 60 | * 61 | * @param string $field 62 | * @return mixed 63 | */ 64 | public function __get($field) 65 | { 66 | if (isset($this->convertFields[$field])) 67 | { 68 | $field = $this->convertFields[$field]; 69 | } 70 | 71 | if (isset($this->user->$field)) 72 | { 73 | return $this->user->$field; 74 | } 75 | } 76 | 77 | public function getAuthIdentifier() 78 | { 79 | $usernameField = $this->usernameField; 80 | return $this->user->$usernameField; 81 | } 82 | 83 | public function getAuthPassword() 84 | { 85 | return null; 86 | } 87 | 88 | public function getRememberToken() 89 | { 90 | return null; 91 | } 92 | 93 | public function setRememberToken($value) 94 | { 95 | } 96 | 97 | public function getRememberTokenName() 98 | { 99 | return null; 100 | } 101 | 102 | } -------------------------------------------------------------------------------- /src/BaseLdapUserProvider.php: -------------------------------------------------------------------------------- 1 | credentialsFields, $type); 48 | } 49 | 50 | /** 51 | * Sets the credentials fields. 52 | * 53 | * @param $credentialsFields 54 | */ 55 | public function setCredentialsFields($credentialsFields) 56 | { 57 | $this->credentialsFields = $credentialsFields; 58 | } 59 | 60 | /** 61 | * Sets the LDAP server. 62 | * 63 | * @param LdapServer $ldapServer 64 | */ 65 | public function setLdapServer(LdapServer $ldapServer) 66 | { 67 | $this->ldapServer = $ldapServer; 68 | } 69 | 70 | /** 71 | * Sets the provider. 72 | * 73 | * @param UserProvider $provider 74 | */ 75 | public function setProvider(UserProvider $provider) 76 | { 77 | $this->provider = $provider; 78 | } 79 | 80 | /** 81 | * Sets whether or not a user must exist in the provider to log in. 82 | * 83 | * @param bool $mustExist 84 | */ 85 | public function setUserMustExistInProvider($mustExist) 86 | { 87 | $this->userMustExistInProvider = $mustExist; 88 | } 89 | 90 | /** 91 | * Checks whether a secondary provider is set or not. 92 | * 93 | * @return bool 94 | */ 95 | public function isUsingProvider() 96 | { 97 | return $this->provider !== null; 98 | } 99 | 100 | public function retrieveById($identifier) 101 | { 102 | // if provider is set, grab from there 103 | if ($this->isUsingProvider()) 104 | { 105 | $user = $this->provider->retrieveById($identifier); 106 | if ($user !== null || $this->userMustExistInProvider) return $user; 107 | } 108 | 109 | // else grab from LDAP 110 | return $this->ldapServer->retrieveByUsername($identifier); 111 | } 112 | 113 | public function retrieveByToken($identifier, $token) 114 | { 115 | // if provider is set, grab from there 116 | if ($this->isUsingProvider()) return $this->provider->retrieveByToken($identifier, $token); 117 | } 118 | 119 | public function updateRememberToken(Authenticatable $user, $token) 120 | { 121 | // if provider is set and the user passed isn't an LDAP one, update using the provider 122 | if ($this->isUsingProvider() && ! $user instanceof LdapUser) 123 | { 124 | $this->provider->updateRememberToken($user, $token); 125 | } 126 | } 127 | 128 | public function retrieveByCredentials(array $credentials) 129 | { 130 | if ($this->isUsingProvider()) 131 | { 132 | // get the user from the provider 133 | $user = $this->provider->retrieveByCredentials($credentials); 134 | if ($user !== null || $this->userMustExistInProvider) return $user; 135 | } 136 | 137 | // get the name of the credentials username field 138 | $usernameField = $this->getCredentialsField('username'); 139 | // grab the username from the credentials passed 140 | $username = ($usernameField !== null) ? array_get($credentials, $usernameField) : null; 141 | 142 | if ($username !== null) 143 | { 144 | // get the user from LDAP 145 | return $this->ldapServer->retrieveByUsername($username); 146 | } 147 | } 148 | 149 | public function validateCredentials(Authenticatable $user, array $credentials) 150 | { 151 | // get the names of the credentials fields 152 | $usernameField = $this->getCredentialsField('username'); 153 | $passwordField = $this->getCredentialsField('password'); 154 | 155 | // validate with the LDAP server 156 | return $this->ldapServer->authenticate($credentials[$usernameField], $credentials[$passwordField]); 157 | } 158 | 159 | } 160 | -------------------------------------------------------------------------------- /src/Contracts/LdapServer.php: -------------------------------------------------------------------------------- 1 | adLDAPLdapServer = new AdLDAPLdapServer(); 16 | $this->adLDAP = Mockery::mock(); 17 | $this->adLDAPLdapServer->setAdServer($this->adLDAP); 18 | } 19 | 20 | public function testRetrieveByUsername() 21 | { 22 | $username = 'foo'; 23 | 24 | // set up mocks 25 | $adLDAPUsers = Mockery::mock(); 26 | $this->adLDAP->shouldReceive('user')->andReturn($adLDAPUsers); 27 | 28 | // should be null if no user was found 29 | $adLDAPUsers->shouldReceive('infoCollection')->once()->andReturn(false); 30 | $this->assertNull($this->adLDAPLdapServer->retrieveByUsername($username)); 31 | 32 | // should return an instance of LdapUser if the user was found 33 | $adLDAPUsers->shouldReceive('infoCollection')->once()->andReturn(true); 34 | $this->assertInstanceOf('LaravelAuthLdap\Contracts\LdapUser', $this->adLDAPLdapServer->retrieveByUsername($username)); 35 | } 36 | 37 | public function testAuthenticate() 38 | { 39 | $username = 'foo'; 40 | $validPassword = 'password'; 41 | $invalidPassword = 'invalid'; 42 | 43 | // set up expectations 44 | $this->adLDAP->shouldReceive('authenticate')->with($username, $validPassword)->andReturn(true); 45 | $this->adLDAP->shouldReceive('authenticate')->with($username, $invalidPassword)->andReturn(false); 46 | 47 | $this->assertTrue($this->adLDAPLdapServer->authenticate($username, $validPassword)); 48 | $this->assertFalse($this->adLDAPLdapServer->authenticate($username, $invalidPassword)); 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /tests/BaseLdapUserProviderTest.php: -------------------------------------------------------------------------------- 1 | baseLdapUserProvider = new BaseLdapUserProvider; 17 | } 18 | 19 | public function testIsUsingProvider() 20 | { 21 | // should not be using provider before setting it 22 | $this->assertFalse($this->baseLdapUserProvider->isUsingProvider()); 23 | 24 | $provider = Mockery::mock('Illuminate\Contracts\Auth\UserProvider'); 25 | $this->baseLdapUserProvider->setProvider($provider); 26 | 27 | // after setting provider, should show that one is being used 28 | $this->assertTrue($this->baseLdapUserProvider->isUsingProvider()); 29 | } 30 | 31 | public function testGetCredentialsField() 32 | { 33 | $usernameField = 'foo'; 34 | $passwordField = 'bar'; 35 | 36 | $credentialsFields = [ 37 | 'username' => $usernameField, 38 | 'password' => $passwordField 39 | ]; 40 | 41 | $this->baseLdapUserProvider->setCredentialsFields($credentialsFields); 42 | 43 | $this->assertEquals($usernameField, $this->baseLdapUserProvider->getCredentialsField('username')); 44 | $this->assertEquals($passwordField, $this->baseLdapUserProvider->getCredentialsField('password')); 45 | } 46 | 47 | public function testRetrieveByCredentials() 48 | { 49 | $credentialsFields = [ 50 | 'username' => 'user', 51 | 'password' => 'pass' 52 | ]; 53 | $this->baseLdapUserProvider->setCredentialsFields($credentialsFields); 54 | 55 | $ldapServer = Mockery::mock('LaravelAuthLdap\Contracts\LdapServer'); 56 | $this->baseLdapUserProvider->setLdapServer($ldapServer); 57 | 58 | $ldapUser = Mockery::mock('LaravelAuthLdap\Contracts\LdapUser'); 59 | $ldapServer->shouldReceive('retrieveByUsername')->andReturn($ldapUser); 60 | 61 | $credentials = [ 62 | 'user' => 'foobar', 63 | 'pass' => 'foopass' 64 | ]; 65 | 66 | // if not using provider, should retrieve from LDAP server 67 | $this->assertEquals($ldapUser, $this->baseLdapUserProvider->retrieveByCredentials($credentials)); 68 | 69 | // now using provider 70 | $provider = Mockery::mock('Illuminate\Contracts\Auth\UserProvider'); 71 | $this->baseLdapUserProvider->setProvider($provider); 72 | 73 | $provider->shouldReceive('retrieveByCredentials')->times(2)->andReturn(null); 74 | 75 | // if not found in provider and doesn't matter if it exists, should default back to LDAP 76 | $this->baseLdapUserProvider->setUserMustExistInProvider(false); 77 | $this->assertEquals($ldapUser, $this->baseLdapUserProvider->retrieveByCredentials($credentials)); 78 | 79 | // if not found in provider and must exist 80 | $this->baseLdapUserProvider->setUserMustExistInProvider(true); 81 | $this->assertNull($this->baseLdapUserProvider->retrieveByCredentials($credentials)); 82 | 83 | $providerUser = Mockery::mock('Illuminate\Contracts\Auth\Authenticatable'); 84 | $provider->shouldReceive('retrieveByCredentials')->times(2)->andReturn($providerUser); 85 | 86 | // if found in provider, should always return from the provider 87 | $this->assertEquals($providerUser, $this->baseLdapUserProvider->retrieveByCredentials($credentials)); 88 | $this->baseLdapUserProvider->setUserMustExistInProvider(false); 89 | $this->assertEquals($providerUser, $this->baseLdapUserProvider->retrieveByCredentials($credentials)); 90 | } 91 | 92 | public function testValidateCredentials() 93 | { 94 | $credentialsFields = [ 95 | 'username' => 'user', 96 | 'password' => 'pass' 97 | ]; 98 | $this->baseLdapUserProvider->setCredentialsFields($credentialsFields); 99 | 100 | $ldapServer = Mockery::mock('LaravelAuthLdap\Contracts\LdapServer'); 101 | $this->baseLdapUserProvider->setLdapServer($ldapServer); 102 | $ldapServer->shouldReceive('authenticate')->once()->andReturn(true); 103 | 104 | $credentials = ['user' => 'foo', 'pass' => 'bar']; 105 | $user = Mockery::mock('Illuminate\Contracts\Auth\Authenticatable'); 106 | 107 | $this->assertTrue($this->baseLdapUserProvider->validateCredentials($user, $credentials)); 108 | } 109 | 110 | public function testRetrieveById() 111 | { 112 | $credentialsFields = [ 113 | 'username' => 'user', 114 | 'password' => 'pass' 115 | ]; 116 | $this->baseLdapUserProvider->setCredentialsFields($credentialsFields); 117 | 118 | $ldapServer = Mockery::mock('LaravelAuthLdap\Contracts\LdapServer'); 119 | $this->baseLdapUserProvider->setLdapServer($ldapServer); 120 | 121 | $ldapUser = Mockery::mock('LaravelAuthLdap\Contracts\LdapUser'); 122 | $ldapServer->shouldReceive('retrieveByUsername')->andReturn($ldapUser); 123 | 124 | $id = 'foo'; 125 | 126 | // if not using provider, should retrieve from LDAP server 127 | $this->assertEquals($ldapUser, $this->baseLdapUserProvider->retrieveById($id)); 128 | 129 | // now using provider 130 | $provider = Mockery::mock('Illuminate\Contracts\Auth\UserProvider'); 131 | $this->baseLdapUserProvider->setProvider($provider); 132 | 133 | $provider->shouldReceive('retrieveById')->times(2)->andReturn(null); 134 | 135 | // if not found in provider and doesn't matter if it exists, should default back to LDAP 136 | $this->baseLdapUserProvider->setUserMustExistInProvider(false); 137 | $this->assertEquals($ldapUser, $this->baseLdapUserProvider->retrieveById($id)); 138 | 139 | // if not found in provider and must exist 140 | $this->baseLdapUserProvider->setUserMustExistInProvider(true); 141 | $this->assertNull($this->baseLdapUserProvider->retrieveById($id)); 142 | 143 | $providerUser = Mockery::mock('Illuminate\Contracts\Auth\Authenticatable'); 144 | $provider->shouldReceive('retrieveById')->times(2)->andReturn($providerUser); 145 | 146 | // if found in provider, should always return from the provider 147 | $this->assertEquals($providerUser, $this->baseLdapUserProvider->retrieveById($id)); 148 | $this->baseLdapUserProvider->setUserMustExistInProvider(false); 149 | $this->assertEquals($providerUser, $this->baseLdapUserProvider->retrieveById($id)); 150 | } 151 | 152 | public function testRetrieveByToken() 153 | { 154 | // not using provider, shouldn't return anything 155 | $this->assertNull($this->baseLdapUserProvider->retrieveByToken('id', 'token')); 156 | 157 | $provider = Mockery::mock('Illuminate\Contracts\Auth\UserProvider'); 158 | $this->baseLdapUserProvider->setProvider($provider); 159 | 160 | $providerUser = Mockery::mock('Illuminate\Contracts\Auth\Authenticatable'); 161 | $provider->shouldReceive('retrieveByToken')->andReturn($providerUser); 162 | 163 | // using provider, should return from it 164 | $this->assertEquals($providerUser, $this->baseLdapUserProvider->retrieveByToken('id', 'token')); 165 | } 166 | 167 | public function testUpdateRememberToken() 168 | { 169 | $provider = Mockery::mock('Illuminate\Contracts\Auth\UserProvider'); 170 | $this->baseLdapUserProvider->setProvider($provider); 171 | 172 | $ldapUser = Mockery::mock('LaravelAuthLdap\Contracts\LdapUser'); 173 | $provider->shouldReceive('updateRememberToken')->never(); 174 | 175 | // if not using provider, don't update token 176 | $this->baseLdapUserProvider->updateRememberToken($ldapUser, 'token'); 177 | 178 | $providerUser = Mockery::mock('Illuminate\Contracts\Auth\Authenticatable'); 179 | $provider->shouldReceive('updateRememberToken')->once(); 180 | 181 | // if using provider, should update token in provider 182 | $this->baseLdapUserProvider->updateRememberToken($providerUser, 'token'); 183 | } 184 | 185 | } 186 | -------------------------------------------------------------------------------- /tests/BaseLdapUserTest.php: -------------------------------------------------------------------------------- 1 | baseLdapUser = new BaseLdapUser; 17 | } 18 | 19 | public function testConvertFieldsMagicMethod() 20 | { 21 | $displayName = 'foo'; 22 | $otherField = 500; 23 | 24 | $user = new stdClass; 25 | $user->displayname = $displayName; 26 | $user->otherField = $otherField; 27 | 28 | $this->baseLdapUser->setUser($user); 29 | $this->baseLdapUser->setConvertFields(['name' => 'displayname']); 30 | 31 | // check that the magic method converted the property name correctly 32 | $this->assertEquals($displayName, $this->baseLdapUser->name); 33 | 34 | // check that the magic method also pulls without converting 35 | $this->assertEquals($displayName, $this->baseLdapUser->displayname); 36 | 37 | // check that the magic method grabbed the field from the user info 38 | $this->assertEquals($otherField, $this->baseLdapUser->otherField); 39 | } 40 | 41 | public function testGetAuthIdentifier() 42 | { 43 | $usernameField = 'username'; 44 | $username = 'foobar'; 45 | 46 | $user = new stdClass; 47 | $user->$usernameField = $username; 48 | 49 | $this->baseLdapUser->setUser($user); 50 | $this->baseLdapUser->setUsernameField($usernameField); 51 | 52 | // check that the identifier is being pulled correctly 53 | $this->assertEquals($username, $this->baseLdapUser->getAuthIdentifier()); 54 | } 55 | 56 | } -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 |