diff --git a/src/Security/SecurityConfigUpdater.php b/src/Security/SecurityConfigUpdater.php index faf5866d6..e6d5b1acb 100644 --- a/src/Security/SecurityConfigUpdater.php +++ b/src/Security/SecurityConfigUpdater.php @@ -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); } /** @@ -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); @@ -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 diff --git a/tests/Security/SecurityConfigUpdaterTest.php b/tests/Security/SecurityConfigUpdaterTest.php index 34911f98d..f84cbce92 100644 --- a/tests/Security/SecurityConfigUpdaterTest.php +++ b/tests/Security/SecurityConfigUpdaterTest.php @@ -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 @@ -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); @@ -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); } @@ -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) { @@ -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)); + } } diff --git a/tests/Security/yaml_fixtures/expected_form_login/form_login.yaml b/tests/Security/yaml_fixtures/expected_form_login/form_login.yaml new file mode 100644 index 000000000..f969847a1 --- /dev/null +++ b/tests/Security/yaml_fixtures/expected_form_login/form_login.yaml @@ -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 diff --git a/tests/Security/yaml_fixtures/expected_json_login/json_login.yaml b/tests/Security/yaml_fixtures/expected_json_login/json_login.yaml new file mode 100644 index 000000000..f535bfdb5 --- /dev/null +++ b/tests/Security/yaml_fixtures/expected_json_login/json_login.yaml @@ -0,0 +1,6 @@ +security: + enable_authenticator_manager: true + firewalls: + main: + json_login: + check_path: a_check_path diff --git a/tests/Security/yaml_fixtures/expected_logout/logout.yaml b/tests/Security/yaml_fixtures/expected_logout/logout.yaml new file mode 100644 index 000000000..6b51ccce7 --- /dev/null +++ b/tests/Security/yaml_fixtures/expected_logout/logout.yaml @@ -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