Navigation Menu

Skip to content

Commit

Permalink
refactored almost everything
Browse files Browse the repository at this point in the history
  • Loading branch information
jfsimon committed Dec 4, 2011
1 parent ec29974 commit 1a3b8af
Show file tree
Hide file tree
Showing 55 changed files with 1,005 additions and 884 deletions.
8 changes: 8 additions & 0 deletions BeSimpleSsoAuthBundle.php
Expand Up @@ -3,7 +3,15 @@
namespace BeSimple\SsoAuthBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use BeSimple\SsoAuthBundle\DependencyInjection\Compiler\FactoryPass;

class BeSimpleSsoAuthBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new FactoryPass());
}
}
12 changes: 6 additions & 6 deletions Controller/TrustedSsoController.php
Expand Up @@ -2,25 +2,25 @@

namespace BeSimple\SsoAuthBundle\Controller;

use BeSimple\SsoAuthBundle\Sso\ProviderInterface;
use BeSimple\SsoAuthBundle\Sso\Manager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

class TrustedSsoController extends Controller
{
public function loginAction(ProviderInterface $provider, Request $request, AuthenticationException $exception = null)
public function loginAction(Manager $manager, Request $request, AuthenticationException $exception = null)
{
return $this->render(
'BeSimpleSsoAuthBundle:TrustedSso:login.html.twig',
array('provider' => $provider, 'request' => $request, 'exception' => $exception)
array('manager' => $manager, 'request' => $request, 'exception' => $exception)
);
}

public function logoutAction(ProviderInterface $provider, Request $request)
public function logoutAction(Manager $manager, Request $request)
{
return $this->render(
'BeSimpleSsoAuthBundle:TrustedSso:logout.html.twig',
array('provider' => $provider, 'request' => $request)
array('manager' => $manager, 'request' => $request)
);
}
}
}
14 changes: 9 additions & 5 deletions DependencyInjection/BeSimpleSsoAuthExtension.php
Expand Up @@ -8,19 +8,23 @@
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;

/**
* @author: Jean-François Simon <contact@jfsimon.fr>
*/
class BeSimpleSsoAuthExtension extends Extension
{
public function load(array $config, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$processor = new Processor();
$config = $processor->processConfiguration(new Configuration($container->getParameter('kernel.debug')), $config);
$providers = $processor->processConfiguration(new Configuration($container->getParameter('kernel.debug')), $config);

foreach ($config as $serverName => $serverConfig) {
$container->setParameter(sprintf('be_simple_sso_auth.%s_config', $serverName), $serverConfig);
foreach ($providers as $id => $provider) {
$container->setParameter(sprintf('be_simple.sso_auth.manager.%s', $id), $provider);
}

$loader->load('sso_providers.xml');
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('security_listeners.xml');
$loader->load('factory.xml');
$loader->load('cas.xml');
}
}
32 changes: 32 additions & 0 deletions DependencyInjection/Compiler/FactoryPass.php
@@ -0,0 +1,32 @@
<?php

namespace BeSimple\SsoAuthBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

/**
* @author: Jean-François Simon <contact@jfsimon.fr>
*/
class FactoryPass implements CompilerPassInterface
{
/**
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
if (!$container->has('be_simple.sso_auth.factory')) {
return;
}

$factoryBuilder = $container->getDefinition('be_simple.sso_auth.factory');

foreach ($container->findTaggedServiceIds('be_simple.sso_auth.protocol') as $id => $attributes) {
$factoryBuilder->addMethodCall('addProtocol', array($attributes[0]['id'], $id));
}

foreach ($container->findTaggedServiceIds('be_simple.sso_auth.server') as $id => $attributes) {
$factoryBuilder->addMethodCall('addServer', array($attributes[0]['id'], $id));
}
}
}
58 changes: 35 additions & 23 deletions DependencyInjection/Configuration.php
Expand Up @@ -3,16 +3,22 @@
namespace BeSimple\SsoAuthBundle\DependencyInjection;

use Symfony\Component\Config\Definition\NodeInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* @author: Jean-François Simon <contact@jfsimon.fr>
*/
class Configuration implements ConfigurationInterface
{
/**
* @var bool
*/
private $debug;

/**
* @param Boolean $debug
* @param bool $debug
*/
public function __construct($debug)
{
Expand All @@ -25,30 +31,36 @@ public function __construct($debug)
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$treeBuilder

$serverDefinition = $treeBuilder
->root('be_simple_sso_auth')
->fixXmlConfig('server')
->fixXmlConfig('provider')
->useAttributeAsKey('id')
->prototype('array')
->children()
->scalarNode('protocol')->cannotBeEmpty()->end()
->scalarNode('base_url')->cannotBeEmpty()->end()
->scalarNode('version')->defaultValue(1)->end()
->scalarNode('username')->defaultValue('{username}@{server_id}')->end()
->arrayNode('validation_request')
->addDefaultsIfNotSet()
->children()
->scalarNode('client')->defaultValue('FileGetContents')->end()
->scalarNode('method')->defaultValue('get')->end()
->scalarNode('timeout')->defaultValue(5)->end()
->scalarNode('max_redirects')->defaultValue(5)->end()
->end()
->end()
->end()
->end()
;
->prototype('array');

$this->setComponentDefinition($serverDefinition, 'protocol');
$this->setComponentDefinition($serverDefinition, 'server');

return $treeBuilder;
}

/**
* @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $serverDefinition
* @param string $name
*
* todo: validate component configuration
*/
private function setComponentDefinition(ArrayNodeDefinition $serverDefinition, $name)
{
$serverDefinition
->children()
->arrayNode($name)
->useAttributeAsKey('id')
->beforeNormalization()
->ifString()->then(function($value) {
return array('id' => $value);
})
->end()
->prototype('scalar');
}
}
5 changes: 4 additions & 1 deletion DependencyInjection/Security/Factory/AbstractSsoFactory.php
Expand Up @@ -7,6 +7,9 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* @author: Jean-François Simon <contact@jfsimon.fr>
*/
abstract class AbstractSsoFactory extends AbstractFactory
{
public function create(ContainerBuilder $container, $id, $config, $userProviderId, $defaultEntryPointId)
Expand All @@ -24,7 +27,7 @@ public function getPosition()
protected function createAuthProvider(ContainerBuilder $container, $id, $config, $userProviderId)
{
$provider = 'security.authentication.provider.sso.'.$id;

$container
->setDefinition($provider, new DefinitionDecorator('security.authentication.provider.sso'))
->replaceArgument(0, new Reference($userProviderId))
Expand Down
7 changes: 3 additions & 4 deletions DependencyInjection/Security/Factory/OpenSsoFactory.php
Expand Up @@ -2,12 +2,11 @@

namespace BeSimple\SsoAuthBundle\DependencyInjection\Security\Factory;

use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use BeSimple\SsoAuthBundle\Sso\ProviderInterface;

/**
* @author: Jean-François Simon <contact@jfsimon.fr>
*/
class OpenSsoFactory extends AbstractSsoFactory
{
public function __construct()
Expand Down
8 changes: 4 additions & 4 deletions DependencyInjection/Security/Factory/TrustedSsoFactory.php
Expand Up @@ -2,17 +2,17 @@

namespace BeSimple\SsoAuthBundle\DependencyInjection\Security\Factory;

use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use BeSimple\SsoAuthBundle\Sso\ProviderInterface;

/**
* @author: Jean-François Simon <contact@jfsimon.fr>
*/
class TrustedSsoFactory extends AbstractSsoFactory
{
public function __construct()
{
$this->addOption('server');
$this->addOption('manager');
$this->addOption('login_action', 'BeSimpleSsoAuthBundle:TrustedSso:login');
$this->addOption('logout_action', 'BeSimpleSsoAuthBundle:TrustedSso:logout');
}
Expand Down
14 changes: 14 additions & 0 deletions Exception/ConfigNotFoundException.php
@@ -0,0 +1,14 @@
<?php

namespace BeSimple\SsoAuthBundle\Exception;

/**
* @author: Jean-François Simon <contact@jfsimon.fr>
*/
class ConfigNotFoundException extends NotFoundException
{
public function __construct($id, $code = null, $previous = null)
{
parent::__construct('Config', $id, $code, $previous);
}
}
12 changes: 12 additions & 0 deletions Exception/InvalidConfigurationException.php
@@ -0,0 +1,12 @@
<?php

namespace BeSimple\SsoAuthBundle\Exception;

use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException as BaseException;

/**
* @author: Jean-François Simon <contact@jfsimon.fr>
*/
class InvalidConfigurationException extends BaseException
{
}
14 changes: 14 additions & 0 deletions Exception/NotFoundException.php
@@ -0,0 +1,14 @@
<?php

namespace BeSimple\SsoAuthBundle\Exception;

/**
* @author: Jean-François Simon <contact@jfsimon.fr>
*/
class NotFoundException extends \InvalidArgumentException
{
public function __construct($id, $subject = 'Service', $code = null, $previous = null)
{
parent::__construct(sprintf('[BeSimpleSsoAuthBundle] %s "%s" is not defined.', $subject, $id), $code, $previous);
}
}
14 changes: 14 additions & 0 deletions Exception/ProtocolNotFoundException.php
@@ -0,0 +1,14 @@
<?php

namespace BeSimple\SsoAuthBundle\Exception;

/**
* @author: Jean-François Simon <contact@jfsimon.fr>
*/
class ProtocolNotFoundException extends NotFoundException
{
public function __construct($id, $code = null, $previous = null)
{
parent::__construct('Protocol', $id, $code, $previous);
}
}
14 changes: 14 additions & 0 deletions Exception/ServerNotFoundException.php
@@ -0,0 +1,14 @@
<?php

namespace BeSimple\SsoAuthBundle\Exception;

/**
* @author: Jean-François Simon <contact@jfsimon.fr>
*/
class ServerNotFoundException extends NotFoundException
{
public function __construct($id, $code = null, $previous = null)
{
parent::__construct('Server', $id, $code, $previous);
}
}
21 changes: 21 additions & 0 deletions Resources/config/cas.xml
@@ -0,0 +1,21 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="be_simple.sso_auth.protocol.cas.class">BeSimple\SsoAuthBundle\Sso\Cas\Protocol</parameter>
<parameter key="be_simple.sso_auth.server.cas.class">BeSimple\SsoAuthBundle\Sso\Cas\Server</parameter>
</parameters>

<services>
<service id="be_simple.sso_auth.protocol.cas" class="%be_simple.sso_auth.protocol.cas.class%">
<tag name="be_simple.sso_auth.protocol" id="cas" />
</service>

<service id="be_simple.sso_auth.server.cas" class="%be_simple.sso_auth.server.cas.class%">
<tag name="be_simple.sso_auth.server" id="cas" />
</service>
</services>
</container>
21 changes: 21 additions & 0 deletions Resources/config/factory.xml
@@ -0,0 +1,21 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="be_simple.sso_auth.factory.class">BeSimple\SsoAuthBundle\Sso\Factory</parameter>
<parameter key="be_simple.sso_auth.client.class">Buzz\Client\Client</parameter>
</parameters>

<services>
<service id="be_simple.sso_auth.client" class="%be_simple.sso_auth.client.class%">
</service>

<service id="be_simple.sso_auth.factory" class="%be_simple.sso_auth.factory.class%">
<argument type="service" id="service_container" />
<argument type="service" id="be_simple.sso_auth.client" />
</service>
</services>
</container>

0 comments on commit 1a3b8af

Please sign in to comment.