Skip to content

Commit

Permalink
set authenticator on security data
Browse files Browse the repository at this point in the history
  • Loading branch information
jrushlow committed Apr 21, 2024
1 parent ab12c72 commit b4ed9e7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
31 changes: 30 additions & 1 deletion src/Maker/Security/MakeCustomAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\FileManager;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Bundle\MakerBundle\Maker\Common\InstallDependencyTrait;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator;
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;
use Symfony\Bundle\MakerBundle\Validator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -39,9 +43,14 @@
*/
final class MakeCustomAuthenticator extends AbstractMaker
{
use InstallDependencyTrait;

private const SECURITY_CONFIG_PATH = 'config/packages/security.yaml';

private ClassNameDetails $authenticatorClassName;

public function __construct(
private FileManager $fileManager,
private Generator $generator,
) {
}
Expand All @@ -65,6 +74,16 @@ public function configureCommand(Command $command, InputConfiguration $inputConf

public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
{
$this->installDependencyIfNeeded(
io: $io,
expectedClassToExist: AbstractAuthenticator::class,
composerPackage: 'symfony/security-bundle'
);

if (!$this->fileManager->fileExists(self::SECURITY_CONFIG_PATH)) {
throw new RuntimeCommandException(sprintf('The file "%s" does not exist. PHP & XML configuration formats are currently not supported.', self::SECURITY_CONFIG_PATH));
}

$name = $io->ask(
question: 'What is the class name of the authenticator (e.g. <fg=yellow>CustomAuthenticator</>)',
validator: static function (mixed $answer) {
Expand All @@ -81,6 +100,17 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
// Configure security to use custom authenticator
$securityConfig = ($ysm = new YamlSourceManipulator(
$this->fileManager->getFileContents(self::SECURITY_CONFIG_PATH)
))->getData();

$securityConfig['security']['firewalls']['main']['custom_authenticators'] = [$this->authenticatorClassName->getFullName()];

$ysm->setData($securityConfig);
$generator->dumpFile(self::SECURITY_CONFIG_PATH, $ysm->getContents());

// Generate the new authenticator
$useStatements = new UseStatementGenerator([
Request::class,
Response::class,
Expand Down Expand Up @@ -110,6 +140,5 @@ className: $this->authenticatorClassName->getFullName(),

public function configureDependencies(DependencyBuilder $dependencies): void
{
// TODO: Implement configureDependencies() method.
}
}
1 change: 1 addition & 0 deletions src/Resources/config/makers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
</service>

<service id="maker.maker.make_custom_authenticator" class="Symfony\Bundle\MakerBundle\Maker\Security\MakeCustomAuthenticator">
<argument type="service" id="maker.file_manager" />
<argument type="service" id="maker.generator" />
<tag name="maker.command" />
</service>
Expand Down
10 changes: 4 additions & 6 deletions tests/Maker/Security/MakeCustomAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ public function getTestDetails(): \Generator

$this->assertFileEquals($fixturePath.'/FixtureAuthenticator.php', $runner->getPath('src/Security/FixtureAuthenticator.php'));

// $securityConfig = $runner->readYaml('config/packages/security.yaml');
//
// $this->assertSame('app_login', $securityConfig['security']['firewalls']['main']['form_login']['login_path']);
// $this->assertSame('app_login', $securityConfig['security']['firewalls']['main']['form_login']['check_path']);
// $this->assertTrue($securityConfig['security']['firewalls']['main']['form_login']['enable_csrf']);
// $this->assertSame('app_logout', $securityConfig['security']['firewalls']['main']['logout']['path']);
$securityConfig = $runner->readYaml('config/packages/security.yaml');

self::assertArrayHasKey('custom_authenticators', $mainFirewall = $securityConfig['security']['firewalls']['main']);
self::assertSame(['App\Security\FixtureAuthenticator'], $mainFirewall['custom_authenticators']);
}),
];
}
Expand Down

0 comments on commit b4ed9e7

Please sign in to comment.