Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #13 from DerManoMann/psr_logger
Browse files Browse the repository at this point in the history
Psr logger
  • Loading branch information
DerManoMann committed Feb 22, 2016
2 parents c7be63e + cbfb365 commit ea12fc1
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 62 deletions.
25 changes: 21 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ Install the latest version with:
$ composer require radebatz/ldap-auth-service-provider
```

Alternatively, you can download the [`ldap-auth-service-provider.zip`][1] file and extract it.


### Configuring Ldap
The Ldap related code depends on [`zend-ldap`](https://github.com/zendframework/zend-ldap), so all configuration options are just passed through.
Expand Down Expand Up @@ -93,8 +91,27 @@ Only restriction is that the custom class has a constructor that is compatible w


## License

All code is licensed under the MIT license.


[1]: https://github.com/DerManoMann/ldap-auth-service-provider/archive/master.zip
## Changelog
Issues that break backwards compatibility are flagged [BC].

### v1.0.0
* Initial release

### v1.1.0
* Move options into security.ldap.[serviceName] namespace
* Add preconfigured user provider

### v1.2.0
* Add Silex 1.3 support
* bug fixes

### v1.2.1
* Add hosts option to allow a list of fallback servers

### v1.2.2
* Fix LdapException handling
* Add Psr\Log dependency
* [BC] Make the logger an optional second constructor argument instead of taking it from $app
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"require": {
"php": ">=5.5.9",
"psr/log": "^1.0",
"silex/silex": "^1.3|~2.0@dev",
"symfony/security": "~2.6|3.0.*",
"zendframework/zend-ldap": "~2.4"
Expand Down
28 changes: 17 additions & 11 deletions src/LdapAuthenticationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Radebatz\Silex\LdapAuth;

use Psr\Log\LoggerInterface;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Zend\Ldap\Exception\LdapException;
Expand All @@ -24,15 +25,18 @@
class LdapAuthenticationServiceProvider implements ServiceProviderInterface
{
protected $serviceName;
protected $logger;

/**
* Create new instance.
*
* @param string $serviceName Service name.
* @param Psr\Log\LoggerInterface $logger Optional logger.
*/
public function __construct($serviceName = 'ldap')
public function __construct($serviceName = 'ldap', LoggerInterface $logger = null)
{
$this->serviceName = $serviceName;
$this->logger = $logger;
}

/**
Expand All @@ -42,6 +46,8 @@ public function register(Container $app)
{
// our name
$serviceName = $this->serviceName;
// a logger (or not);
$logger = $this->logger;

$defaults = array(
// authentication defaults
Expand Down Expand Up @@ -75,7 +81,7 @@ public function register(Container $app)

// the actual Ldap resource
if (!isset($app['security.ldap.'.$serviceName.'.ldap'])) {
$app['security.ldap.'.$serviceName.'.ldap'] = function () use ($app, $serviceName) {
$app['security.ldap.'.$serviceName.'.ldap'] = function () use ($app, $serviceName, $logger) {
// ldap options
$options = $app['security.ldap.config']($serviceName)['ldap'];

Expand All @@ -97,15 +103,15 @@ public function register(Container $app)

return $ldap;
} catch (LdapException $le) {
if ($app->offsetExists('logger')) {
$app['logger']->warning(sprintf('LDAP: Failed connecting to host: %s', $host));
if ($logger) {
$logger->warning(sprintf('LDAP: Failed connecting to host: %s', $host));
}
}
}
}

if ($app->offsetExists('logger')) {
$app['logger']->info(sprintf('LDAP: Using default host: %s', $options['host']));
if ($logger) {
$logger->info(sprintf('LDAP: Using default host: %s', $options['host']));
}

// just pass through all options using configured (single) host
Expand All @@ -115,13 +121,13 @@ public function register(Container $app)

// ready made user provider
if (!isset($app['security.ldap.'.$serviceName.'.user_provider'])) {
$app['security.ldap.'.$serviceName.'.user_provider'] = $app->protect(function ($options = array()) use ($app, $serviceName) {
return new LdapUserProvider($serviceName, $app['security.ldap.'.$serviceName.'.ldap'], $app['logger'], $options);
$app['security.ldap.'.$serviceName.'.user_provider'] = $app->protect(function ($options = array()) use ($app, $serviceName, $logger) {
return new LdapUserProvider($serviceName, $app['security.ldap.'.$serviceName.'.ldap'], $logger, $options);
});
}

// set up authentication provider factory and user provider
$app['security.authentication_listener.factory.'.$serviceName] = $app->protect(function ($name, $options) use ($app, $serviceName) {
$app['security.authentication_listener.factory.'.$serviceName] = $app->protect(function ($name, $options) use ($app, $serviceName, $logger) {
$serviceOptions = $app['security.ldap.config']($serviceName);
$entryPoint = $serviceOptions['auth']['entryPoint'];

Expand All @@ -130,12 +136,12 @@ public function register(Container $app)
}

// define the authentication provider object
$app['security.authentication_provider.'.$name.'.'.$serviceName] = function () use ($app, $name, $serviceOptions, $serviceName) {
$app['security.authentication_provider.'.$name.'.'.$serviceName] = function () use ($app, $name, $serviceOptions, $serviceName, $logger) {
return new LdapAuthenticationProvider(
$serviceName,
$app['security.user_provider.'.$name],
$app['security.ldap.'.$serviceName.'.ldap'],
$app['logger'],
$logger,
$serviceOptions['auth']
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class LdapAuthenticationProvider implements AuthenticationProviderInterface
* @param string $providerKey The provider key.
* @param UserProviderInterface $userProvider A user provider.
* @param Zend\Ldap\Ldap $ldap Ldap serivce.
* @param Logger $logger Optional logger.
* @param Psr\Log\LoggerInterface $logger Optional logger.
* @param array $options Options.
*/
public function __construct($providerKey, UserProviderInterface $userProvider, $ldap, LoggerInterface $logger = null, array $options = array())
Expand Down
28 changes: 17 additions & 11 deletions src/Silex1LdapAuthenticationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Radebatz\Silex\LdapAuth;

use Psr\Log\LoggerInterface;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Zend\Ldap\Exception\LdapException;
Expand All @@ -24,15 +25,18 @@
class Silex1LdapAuthenticationServiceProvider implements ServiceProviderInterface
{
protected $serviceName;
protected $logger;

/**
* Create new instance.
*
* @param string $serviceName Service name.
* @param Psr\Log\LoggerInterface $logger Optional logger.
*/
public function __construct($serviceName = 'ldap')
public function __construct($serviceName = 'ldap', LoggerInterface $logger = null)
{
$this->serviceName = $serviceName;
$this->logger = $logger;
}

/**
Expand All @@ -42,6 +46,8 @@ public function register(Application $app)
{
// our name
$serviceName = $this->serviceName;
// a logger (or not);
$logger = $this->logger;

$defaults = array(
// authentication defaults
Expand Down Expand Up @@ -75,7 +81,7 @@ public function register(Application $app)

// the actual Ldap resource
if (!isset($app['security.ldap.'.$serviceName.'.ldap'])) {
$app['security.ldap.'.$serviceName.'.ldap'] = function () use ($app, $serviceName) {
$app['security.ldap.'.$serviceName.'.ldap'] = function () use ($app, $serviceName, $logger) {
// ldap options
$options = $app['security.ldap.config']($serviceName)['ldap'];

Expand All @@ -97,15 +103,15 @@ public function register(Application $app)

return $ldap;
} catch (LdapException $le) {
if ($app->offsetExists('logger')) {
$app['logger']->warning(sprintf('LDAP: Failed connecting to host: %s', $host));
if ($logger) {
$logger->warning(sprintf('LDAP: Failed connecting to host: %s', $host));
}
}
}
}

if ($app->offsetExists('logger')) {
$app['logger']->info(sprintf('LDAP: Using default host: %s', $options['host']));
if ($logger) {
$logger->info(sprintf('LDAP: Using default host: %s', $options['host']));
}

// just pass through all options using configured (single) host
Expand All @@ -115,13 +121,13 @@ public function register(Application $app)

// ready made user provider
if (!isset($app['security.ldap.'.$serviceName.'.user_provider'])) {
$app['security.ldap.'.$serviceName.'.user_provider'] = $app->protect(function ($options = array()) use ($app, $serviceName) {
return new LdapUserProvider($serviceName, $app['security.ldap.'.$serviceName.'.ldap'], $app['logger'], $options);
$app['security.ldap.'.$serviceName.'.user_provider'] = $app->protect(function ($options = array()) use ($app, $serviceName, $logger) {
return new LdapUserProvider($serviceName, $app['security.ldap.'.$serviceName.'.ldap'], $logger, $options);
});
}

// set up authentication provider factory and user provider
$app['security.authentication_listener.factory.'.$serviceName] = $app->protect(function ($name, $options) use ($app, $serviceName) {
$app['security.authentication_listener.factory.'.$serviceName] = $app->protect(function ($name, $options) use ($app, $serviceName, $logger) {
$serviceOptions = $app['security.ldap.config']($serviceName);
$entryPoint = $serviceOptions['auth']['entryPoint'];

Expand All @@ -130,12 +136,12 @@ public function register(Application $app)
}

// define the authentication provider object
$app['security.authentication_provider.'.$name.'.'.$serviceName] = function () use ($app, $name, $serviceOptions, $serviceName) {
$app['security.authentication_provider.'.$name.'.'.$serviceName] = function () use ($app, $name, $serviceOptions, $serviceName, $logger) {
return new LdapAuthenticationProvider(
$serviceName,
$app['security.user_provider.'.$name],
$app['security.ldap.'.$serviceName.'.ldap'],
$app['logger'],
$logger,
$serviceOptions['auth']
);
};
Expand Down
57 changes: 36 additions & 21 deletions tests/LdapAuthenticationServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,23 @@
*/
class LdapAuthenticationServiceProviderTest extends LdapAuthTestCase
{
public function testLdapHttpAuthentication()
public function loggerProvider()
{
$app = $this->createApplication('http');
$logger = new Logger('CLI');
$logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));

return [
'null' => [null],
'psr' => [$logger],
];
}

/**
* @dataProvider loggerProvider
*/
public function testLdapHttpAuthentication($logger)
{
$app = $this->createApplication('http', $logger);

$client = new Client($app);

Expand All @@ -54,9 +68,12 @@ public function testLdapHttpAuthentication()
$this->assertEquals('admin', $client->getResponse()->getContent());
}

public function testLdapFormAuthentication()
/**
* @dataProvider loggerProvider
*/
public function testLdapFormAuthentication($logger)
{
$app = $this->createApplication('form');
$app = $this->createApplication('form', $logger);

$client = new Client($app);

Expand Down Expand Up @@ -102,39 +119,37 @@ public function testLdapFormAuthentication()
$this->assertEquals('admin', $client->getResponse()->getContent());
}

public function createApplication($authenticationMethod)
public function createApplication($authenticationMethod, $logger)
{
$app = new Application();
$app['debug'] = true;
$app->register(new SessionServiceProvider());

/*
$app['logger'] = new Logger('CLI');
$app['logger']->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
*/

// ********* //
$serviceName = 'ldap-'.$authenticationMethod;
$this->registerLdapAuthenticationServiceProvider($app, $authenticationMethod, $serviceName);
$this->registerLdapAuthenticationServiceProvider($app, $authenticationMethod, $serviceName, $logger);
$app = call_user_func(array($this, 'add'.ucfirst($authenticationMethod ?: 'null').'Authentication'), $app, $serviceName);

$app['session.test'] = true;

return $app;
}

protected function registerLdapAuthenticationServiceProvider($app, $authenticationMethod, $serviceName)
protected function registerLdapAuthenticationServiceProvider($app, $authenticationMethod, $serviceName, $logger)
{
$app->register(new LdapAuthenticationServiceProvider($serviceName), array(
'security.ldap.'.$serviceName.'.options' => array_merge(
$this->getOptions(),
array(
'auth' => array(
'entryPoint' => $authenticationMethod,
),
$app->register(new LdapAuthenticationServiceProvider($serviceName, $logger),
array(
'security.ldap.'.$serviceName.'.options' => array_merge(
$this->getOptions(),
array(
'auth' => array(
'entryPoint' => $authenticationMethod,
),
)
)
)
));
),
$app['logger']
);

// need this before the firewall is configured
$app['security.ldap.'.$serviceName.'.ldap'] = function () {
Expand Down
Loading

0 comments on commit ea12fc1

Please sign in to comment.