Skip to content

Commit

Permalink
[SecurityBundle] Make security schema deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
MatTheCat authored and fabpot committed Aug 19, 2024
1 parent 86557f2 commit f14a15c
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Resources/config/schema/security-1.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
<xsd:element name="memory" type="memory" />
<xsd:element name="ldap" type="ldap" />
<!-- allow factories to use dynamic elements -->
<xsd:any processContents="lax" />
<xsd:any processContents="lax" namespace="##other" />
</xsd:choice>
<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:attribute name="id" type="xsd:string" />
Expand Down Expand Up @@ -176,7 +176,7 @@
<xsd:element name="x509" type="x509" minOccurs="0" maxOccurs="1" />
<xsd:element name="required-badge" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<!-- allow factories to use dynamic elements -->
<xsd:any processContents="lax" minOccurs="0" maxOccurs="unbounded" />
<xsd:any processContents="lax" minOccurs="0" maxOccurs="unbounded" namespace="##other" />
</xsd:choice>
<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:attribute name="pattern" type="xsd:string" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Fixtures\Authenticator;

use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\AuthenticatorFactoryInterface;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class CustomAuthenticator implements AuthenticatorFactoryInterface
{
public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId): string
{
return 'security.authenticator.custom.'.$firewallName;
}

public function getKey(): string
{
return 'custom';
}

public function addConfiguration(NodeDefinition $builder)
{
}

public function getPriority(): int
{
return 0;
}
}
28 changes: 28 additions & 0 deletions Tests/DependencyInjection/Fixtures/UserProvider/CustomProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Fixtures\UserProvider;

use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class CustomProvider implements UserProviderFactoryInterface
{
public function create(ContainerBuilder $container, string $id, array $config)
{
}

public function getKey(): string
{
return 'custom';
}

public function addConfiguration(NodeDefinition $builder)
{
$builder
->children()
->scalarNode('foo')->defaultValue('bar')->end()
->end()
;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>

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

<sec:config enable-authenticator-manager="true">
<sec:firewall name="main">
<custom xmlns="http://example.com/schema" />
</sec:firewall>
</sec:config>

</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>

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

<sec:config enable-authenticator-manager="true">
<sec:firewall name="main">
<sec:custom />
</sec:firewall>
</sec:config>

</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

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

<sec:config enable-authenticator-manager="true">
<sec:provider name="foo">
<custom xmlns="http://example.com/schema" />
</sec:provider>

<sec:firewall name="main" provider="foo" />
</sec:config>

</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

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

<sec:config enable-authenticator-manager="true">
<sec:provider name="foo">
<sec:custom />
</sec:provider>

<sec:firewall name="main" provider="foo" />
</sec:config>

</container>
51 changes: 51 additions & 0 deletions Tests/DependencyInjection/XmlCustomAuthenticatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
use Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Fixtures\Authenticator\CustomAuthenticator;
use Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Fixtures\UserProvider\CustomProvider;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

class XmlCustomAuthenticatorTest extends TestCase
{
/**
* @dataProvider provideXmlConfigurationFile
*/
public function testCustomProviderElement(string $configurationFile)
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->register('cache.system', \stdClass::class);

$security = new SecurityExtension();
$security->addAuthenticatorFactory(new CustomAuthenticator());
$container->registerExtension($security);

(new XmlFileLoader($container, new FileLocator(__DIR__.'/Fixtures/xml')))->load($configurationFile);

$container->getCompilerPassConfig()->setRemovingPasses([]);
$container->getCompilerPassConfig()->setAfterRemovingPasses([]);
$container->compile();

$this->addToAssertionCount(1);
}

public static function provideXmlConfigurationFile(): iterable
{
yield 'Custom authenticator element under SecurityBundle’s namespace' => ['custom_authenticator_under_security_namespace.xml'];
yield 'Custom authenticator element under its own namespace' => ['custom_authenticator_under_own_namespace.xml'];
}
}
50 changes: 50 additions & 0 deletions Tests/DependencyInjection/XmlCustomProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
use Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Fixtures\UserProvider\CustomProvider;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

class XmlCustomProviderTest extends TestCase
{
/**
* @dataProvider provideXmlConfigurationFile
*/
public function testCustomProviderElement(string $configurationFile)
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->register('cache.system', \stdClass::class);

$security = new SecurityExtension();
$security->addUserProviderFactory(new CustomProvider());
$container->registerExtension($security);

(new XmlFileLoader($container, new FileLocator(__DIR__.'/Fixtures/xml')))->load($configurationFile);

$container->getCompilerPassConfig()->setRemovingPasses([]);
$container->getCompilerPassConfig()->setAfterRemovingPasses([]);
$container->compile();

$this->addToAssertionCount(1);
}

public static function provideXmlConfigurationFile(): iterable
{
yield 'Custom provider element under SecurityBundle’s namespace' => ['custom_provider_under_security_namespace.xml'];
yield 'Custom provider element under its own namespace' => ['custom_provider_under_own_namespace.xml'];
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"php": ">=7.2.5",
"ext-xml": "*",
"symfony/config": "^4.4|^5.0|^6.0",
"symfony/dependency-injection": "^5.3|^6.0",
"symfony/dependency-injection": "^5.4.43|^6.4.11",
"symfony/deprecation-contracts": "^2.1|^3",
"symfony/event-dispatcher": "^5.1|^6.0",
"symfony/http-kernel": "^5.3|^6.0",
Expand Down

0 comments on commit f14a15c

Please sign in to comment.