Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/Security/SecurityConfigUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,22 @@ public function __construct(

public function updateForFormLogin(string $yamlSource, string $firewallToUpdate, string $loginPath, string $checkPath): string
{
$this->manipulator = new YamlSourceManipulator($yamlSource);

if (null !== $this->ysmLogger) {
$this->manipulator->setLogger($this->ysmLogger);
}

$this->normalizeSecurityYamlFile();

$newData = $this->manipulator->getData();
$newData = $this->createYamlSourceManipulator($yamlSource);

$newData['security']['firewalls'][$firewallToUpdate]['form_login']['login_path'] = $loginPath;
$newData['security']['firewalls'][$firewallToUpdate]['form_login']['check_path'] = $checkPath;
$newData['security']['firewalls'][$firewallToUpdate]['form_login']['enable_csrf'] = true;

$this->manipulator->setData($newData);
return $this->getYamlContentsFromData($newData);
}

return $this->manipulator->getContents();
public function updateForJsonLogin(string $yamlSource, string $firewallToUpdate, string $checkPath): string
{
$data = $this->createYamlSourceManipulator($yamlSource);

$data['security']['firewalls'][$firewallToUpdate]['json_login']['check_path'] = $checkPath;

return $this->getYamlContentsFromData($data);
}

/**
Expand Down Expand Up @@ -149,7 +148,7 @@ private function configureLogout(array $securityData, string $firewallName): voi
$this->manipulator->setData($securityData);
}

private function createYamlSourceManipulator(string $yamlSource): void
private function createYamlSourceManipulator(string $yamlSource): array
{
$this->manipulator = new YamlSourceManipulator($yamlSource);

Expand All @@ -158,6 +157,15 @@ private function createYamlSourceManipulator(string $yamlSource): void
}

$this->normalizeSecurityYamlFile();

return $this->manipulator->getData();
}

private function getYamlContentsFromData(array $yamlData): string
{
$this->manipulator->setData($yamlData);

return $this->manipulator->getContents();
}

private function normalizeSecurityYamlFile(): void
Expand Down
73 changes: 61 additions & 12 deletions tests/Security/SecurityConfigUpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,9 @@ class SecurityConfigUpdaterTest extends TestCase
/**
* Set to true to enable low level debug logging during tests for
* the YamlSourceManipulator.
*
* @var bool
*/
private $enableYsmLogging = false;

/**
* @var Logger|null
*/
private $ysmLogger = null;
private bool $enableYsmLogging = false;
private ?Logger $ysmLogger = null;

/**
* @dataProvider getUserClassTests
Expand All @@ -45,9 +39,9 @@ public function testUpdateForUserClass(UserClassConfiguration $userConfig, strin
}

$updater = new SecurityConfigUpdater($this->ysmLogger);
$source = file_get_contents(__DIR__.'/yaml_fixtures/source/'.$startingSourceFilename);
$source = $this->getYamlSource($startingSourceFilename);
$actualSource = $updater->updateForUserClass($source, $userConfig, $userClass);
$expectedSource = file_get_contents(__DIR__.'/yaml_fixtures/expected_user_class/5.3/'.$expectedSourceFilename);
$expectedSource = $this->getExpectedYaml('expected_user_class/5.3', $expectedSourceFilename);

$expectedSource = str_replace('{BCRYPT_OR_AUTO}', 'auto', $expectedSource);

Expand Down Expand Up @@ -115,9 +109,9 @@ public function testUpdateForAuthenticator(string $firewallName, $entryPoint, st
$this->createLogger();

$updater = new SecurityConfigUpdater($this->ysmLogger);
$source = file_get_contents(__DIR__.'/yaml_fixtures/source/'.$startingSourceFilename);
$source = $this->getYamlSource($startingSourceFilename);
$actualSource = $updater->updateForAuthenticator($source, $firewallName, $entryPoint, 'App\\Security\\AppCustomAuthenticator', $logoutSetup);
$expectedSource = file_get_contents(__DIR__.'/yaml_fixtures/expected_authenticator/'.$expectedSourceFilename);
$expectedSource = $this->getExpectedYaml('expected_authenticator', $expectedSourceFilename);

$this->assertSame($expectedSource, $actualSource);
}
Expand Down Expand Up @@ -173,6 +167,51 @@ public function getAuthenticatorTests(): \Generator
];
}

public function testUpdateForFormLogin(): void
{
$this->createLogger();

$updater = new SecurityConfigUpdater($this->ysmLogger);
$source = $this->getYamlSource('empty_security.yaml');

$actualSource = $updater->updateForFormLogin($source, 'main', 'a_login_path', 'a_check_path');

$this->assertSame(
$this->getExpectedYaml('expected_form_login', 'form_login.yaml'),
$actualSource
);
}

public function testUpdateForJsonLogin(): void
{
$this->createLogger();

$updater = new SecurityConfigUpdater($this->ysmLogger);
$source = $this->getYamlSource('empty_security.yaml');

$actualSource = $updater->updateForJsonLogin($source, 'main', 'a_check_path');

$this->assertSame(
$this->getExpectedYaml('expected_json_login', 'json_login.yaml'),
$actualSource
);
}

public function testUpdateForLogout(): void
{
$this->createLogger();

$updater = new SecurityConfigUpdater($this->ysmLogger);
$source = $this->getYamlSource('simple_security_with_firewalls.yaml');

$actualSource = $updater->updateForLogout($source, 'main');

$this->assertSame(
$this->getExpectedYaml('expected_logout', 'logout.yaml'),
$actualSource
);
}

private function createLogger(): void
{
if (!$this->enableYsmLogging) {
Expand All @@ -194,4 +233,14 @@ private function createLogger(): void
return $message."\n\n";
});
}

private function getYamlSource(string $yamlFileName): string
{
return file_get_contents(sprintf('%s/yaml_fixtures/source/%s', __DIR__, $yamlFileName));
}

private function getExpectedYaml(string $subDirectory, string $yamlFileName): string
{
return file_get_contents(sprintf('%s/yaml_fixtures/%s/%s', __DIR__, $subDirectory, $yamlFileName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
security:
enable_authenticator_manager: true
firewalls:
main:
form_login:
login_path: a_login_path
check_path: a_check_path
enable_csrf: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
security:
enable_authenticator_manager: true
firewalls:
main:
json_login:
check_path: a_check_path
17 changes: 17 additions & 0 deletions tests/Security/yaml_fixtures/expected_logout/logout.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
security:
enable_authenticator_manager: true

# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
in_memory: { memory: ~ }

firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
logout:
path: app_logout
# where to redirect after logout
# target: app_any_route