From 6057d997ae7084baf45817122d8e696573ca60bb Mon Sep 17 00:00:00 2001 From: dwgebler Date: Thu, 25 Apr 2024 20:13:05 +0100 Subject: [PATCH 01/10] set translator in AccessTokenAuthenticator in Security bundle config --- .../Resources/config/security_authenticator_access_token.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Bundle/SecurityBundle/Resources/config/security_authenticator_access_token.php b/src/Symfony/Bundle/SecurityBundle/Resources/config/security_authenticator_access_token.php index 66716b23ad892..bf9655307c566 100644 --- a/src/Symfony/Bundle/SecurityBundle/Resources/config/security_authenticator_access_token.php +++ b/src/Symfony/Bundle/SecurityBundle/Resources/config/security_authenticator_access_token.php @@ -42,6 +42,7 @@ null, null, ]) + ->call('setTranslator', [service('translator')->ignoreOnInvalid()]) ->set('security.authenticator.access_token.chain_extractor', ChainAccessTokenExtractor::class) ->abstract() From a57d25f5c1f6ee663f722ce32d4e40933380f764 Mon Sep 17 00:00:00 2001 From: dwgebler Date: Fri, 26 Apr 2024 00:27:33 +0100 Subject: [PATCH 02/10] translate errors in access token handler but revert if non-ascii chars in translated string --- .../AccessTokenAuthenticator.php | 3 + .../AccessTokenAuthenticatorTest.php | 73 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php b/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php index 40494f1e606ae..0884251667f0c 100644 --- a/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php +++ b/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php @@ -84,6 +84,9 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio if (null !== $this->translator) { $errorMessage = $this->translator->trans($exception->getMessageKey(), $exception->getMessageData(), 'security'); + if (false === mb_check_encoding($errorMessage, 'ASCII')) { + $errorMessage = strtr($exception->getMessageKey(), $exception->getMessageData()); + } } else { $errorMessage = strtr($exception->getMessageKey(), $exception->getMessageData()); } diff --git a/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php b/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php index 5ee4869b431ae..208735df143b7 100644 --- a/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Exception\BadCredentialsException; use Symfony\Component\Security\Core\User\InMemoryUser; use Symfony\Component\Security\Core\User\InMemoryUserProvider; @@ -22,6 +23,7 @@ use Symfony\Component\Security\Http\Authenticator\AccessTokenAuthenticator; use Symfony\Component\Security\Http\Authenticator\FallbackUserLoader; use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; +use Symfony\Contracts\Translation\TranslatorInterface; class AccessTokenAuthenticatorTest extends TestCase { @@ -36,6 +38,77 @@ protected function setUp(): void $this->userProvider = new InMemoryUserProvider(['test' => ['password' => 's$cr$t']]); } + public function testOnAuthenticationFailureWithTranslatorTranslatesErrorMessage() + { + $request = Request::create('/test'); + + $this->accessTokenExtractor + ->expects($this->once()) + ->method('extractAccessToken') + ->with($request) + ->willReturn(null); + + $authenticator = new AccessTokenAuthenticator( + $this->accessTokenHandler, + $this->accessTokenExtractor, + $this->userProvider, + ); + + $translator = $this->createMock(TranslatorInterface::class); + $translator + ->expects($this->once()) + ->method('trans') + ->with('Invalid credentials.') + ->willReturn('Credenciales invalidas.'); + + $authenticator->setTranslator($translator); + + $response = null; + try { + $authenticator->authenticate($request); + } catch (BadCredentialsException $e) { + $response = $authenticator->onAuthenticationFailure($request, $e); + } + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Bearer error="invalid_token",error_description="Credenciales invalidas."', $response->headers->get('WWW-Authenticate')); + } + + public function testOnAuthenticationFailureWithTranslatorRevertsTranslationWhenTranslatedMessageContainsNonAscii() + { + $request = Request::create('/test'); + + $this->accessTokenExtractor + ->expects($this->once()) + ->method('extractAccessToken') + ->with($request) + ->willReturn(null); + + $authenticator = new AccessTokenAuthenticator( + $this->accessTokenHandler, + $this->accessTokenExtractor, + $this->userProvider, + ); + + $nonAsciiString = "Credenciales inválidas."; + $translator = $this->createMock(TranslatorInterface::class); + $translator + ->expects($this->once()) + ->method('trans') + ->with('Invalid credentials.') + ->willReturn($nonAsciiString); + + $authenticator->setTranslator($translator); + + $response = null; + try { + $authenticator->authenticate($request); + } catch (BadCredentialsException $e) { + $response = $authenticator->onAuthenticationFailure($request, $e); + } + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Bearer error="invalid_token",error_description="Invalid credentials."', $response->headers->get('WWW-Authenticate')); + } + public function testAuthenticateWithoutAccessToken() { $this->expectException(BadCredentialsException::class); From 23746cfc053baac7b2dc68c506cac1208f52dd27 Mon Sep 17 00:00:00 2001 From: dwgebler Date: Fri, 26 Apr 2024 00:33:18 +0100 Subject: [PATCH 03/10] fabpot code style lint fix --- .../Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php b/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php index 208735df143b7..92522463aa5c7 100644 --- a/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php @@ -89,7 +89,7 @@ public function testOnAuthenticationFailureWithTranslatorRevertsTranslationWhenT $this->userProvider, ); - $nonAsciiString = "Credenciales inválidas."; + $nonAsciiString = 'Credenciales inválidas.'; $translator = $this->createMock(TranslatorInterface::class); $translator ->expects($this->once()) From 7d3e87a57239d73c39b06f1fb5f499d9df8ccb85 Mon Sep 17 00:00:00 2001 From: dwgebler Date: Fri, 26 Apr 2024 00:38:37 +0100 Subject: [PATCH 04/10] replace mb check with preg_match to avoid mbstring dependency --- .../Security/Http/Authenticator/AccessTokenAuthenticator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php b/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php index 0884251667f0c..b85a99b3b5ccf 100644 --- a/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php +++ b/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php @@ -84,7 +84,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio if (null !== $this->translator) { $errorMessage = $this->translator->trans($exception->getMessageKey(), $exception->getMessageData(), 'security'); - if (false === mb_check_encoding($errorMessage, 'ASCII')) { + if (0 !== preg_match('/[^\x00-\x7F]/', $errorMessage)) { $errorMessage = strtr($exception->getMessageKey(), $exception->getMessageData()); } } else { From 727c32af7e240ee4cc1c797edd4649413ea3cf32 Mon Sep 17 00:00:00 2001 From: dwgebler Date: Mon, 29 Apr 2024 21:56:56 +0100 Subject: [PATCH 05/10] emit deprecation on non-ascii translation --- .../Http/Authenticator/AccessTokenAuthenticator.php | 8 ++++++-- .../Tests/Authenticator/AccessTokenAuthenticatorTest.php | 9 ++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php b/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php index b85a99b3b5ccf..3d7eccce249f0 100644 --- a/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php +++ b/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php @@ -84,8 +84,12 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio if (null !== $this->translator) { $errorMessage = $this->translator->trans($exception->getMessageKey(), $exception->getMessageData(), 'security'); - if (0 !== preg_match('/[^\x00-\x7F]/', $errorMessage)) { - $errorMessage = strtr($exception->getMessageKey(), $exception->getMessageData()); + if (preg_match('/[^\x00-\x7F]/', $errorMessage)) { + trigger_deprecation( + 'symfony/security-http', + '6.4', + 'Using non-ASCII characters in the error message is deprecated. Use ASCII characters only.' + ); } } else { $errorMessage = strtr($exception->getMessageKey(), $exception->getMessageData()); diff --git a/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php b/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php index 92522463aa5c7..2eda69914888d 100644 --- a/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php @@ -70,10 +70,13 @@ public function testOnAuthenticationFailureWithTranslatorTranslatesErrorMessage( $response = $authenticator->onAuthenticationFailure($request, $e); } $this->assertInstanceOf(Response::class, $response); - $this->assertEquals('Bearer error="invalid_token",error_description="Credenciales invalidas."', $response->headers->get('WWW-Authenticate')); + $this->assertSame('Bearer error="invalid_token",error_description="Credenciales invalidas."', $response->headers->get('WWW-Authenticate')); } - public function testOnAuthenticationFailureWithTranslatorRevertsTranslationWhenTranslatedMessageContainsNonAscii() + /** + * @expectedDeprecation Since symfony/security-http 6.4: Using non-ASCII characters in the error message is deprecated. Use ASCII characters only. + */ + public function testOnAuthenticationFailureWithTranslatorThrowsDeprecationWhenTranslatedMessageContainsNonAscii() { $request = Request::create('/test'); @@ -106,7 +109,7 @@ public function testOnAuthenticationFailureWithTranslatorRevertsTranslationWhenT $response = $authenticator->onAuthenticationFailure($request, $e); } $this->assertInstanceOf(Response::class, $response); - $this->assertEquals('Bearer error="invalid_token",error_description="Invalid credentials."', $response->headers->get('WWW-Authenticate')); + $this->assertSame('Bearer error="invalid_token",error_description="Credenciales inválidas."', $response->headers->get('WWW-Authenticate')); } public function testAuthenticateWithoutAccessToken() From 097a044fd43a085dcb28750c5c6b9f5750efa186 Mon Sep 17 00:00:00 2001 From: dwgebler Date: Mon, 29 Apr 2024 21:59:26 +0100 Subject: [PATCH 06/10] lint fix --- .../Http/Authenticator/AccessTokenAuthenticator.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php b/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php index 3d7eccce249f0..321085628599c 100644 --- a/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php +++ b/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php @@ -85,11 +85,11 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio if (null !== $this->translator) { $errorMessage = $this->translator->trans($exception->getMessageKey(), $exception->getMessageData(), 'security'); if (preg_match('/[^\x00-\x7F]/', $errorMessage)) { - trigger_deprecation( - 'symfony/security-http', - '6.4', - 'Using non-ASCII characters in the error message is deprecated. Use ASCII characters only.' - ); + trigger_deprecation( + 'symfony/security-http', + '6.4', + 'Using non-ASCII characters in the error message is deprecated. Use ASCII characters only.' + ); } } else { $errorMessage = strtr($exception->getMessageKey(), $exception->getMessageData()); From 1dabbb89075d366eafc44c9de1521e6fcd65e705 Mon Sep 17 00:00:00 2001 From: dwgebler Date: Mon, 29 Apr 2024 22:07:33 +0100 Subject: [PATCH 07/10] add group legacy annotation --- .../Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php b/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php index 2eda69914888d..10051f06bef77 100644 --- a/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php @@ -74,6 +74,7 @@ public function testOnAuthenticationFailureWithTranslatorTranslatesErrorMessage( } /** + * @group legacy * @expectedDeprecation Since symfony/security-http 6.4: Using non-ASCII characters in the error message is deprecated. Use ASCII characters only. */ public function testOnAuthenticationFailureWithTranslatorThrowsDeprecationWhenTranslatedMessageContainsNonAscii() From 7e3637075014e59dca8c9b0b7868c2070595a050 Mon Sep 17 00:00:00 2001 From: dwgebler Date: Mon, 29 Apr 2024 22:08:10 +0100 Subject: [PATCH 08/10] lint fix --- .../Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php b/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php index 10051f06bef77..5fc8b6e08c4da 100644 --- a/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php @@ -75,6 +75,7 @@ public function testOnAuthenticationFailureWithTranslatorTranslatesErrorMessage( /** * @group legacy + * * @expectedDeprecation Since symfony/security-http 6.4: Using non-ASCII characters in the error message is deprecated. Use ASCII characters only. */ public function testOnAuthenticationFailureWithTranslatorThrowsDeprecationWhenTranslatedMessageContainsNonAscii() From 16bc9dade16f6ec46b5d540c074f82ab06e37291 Mon Sep 17 00:00:00 2001 From: dwgebler Date: Mon, 29 Apr 2024 22:08:56 +0100 Subject: [PATCH 09/10] lint fix --- .../Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php b/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php index 5fc8b6e08c4da..e5d6e66ff79c8 100644 --- a/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Authenticator/AccessTokenAuthenticatorTest.php @@ -75,7 +75,7 @@ public function testOnAuthenticationFailureWithTranslatorTranslatesErrorMessage( /** * @group legacy - * + * * @expectedDeprecation Since symfony/security-http 6.4: Using non-ASCII characters in the error message is deprecated. Use ASCII characters only. */ public function testOnAuthenticationFailureWithTranslatorThrowsDeprecationWhenTranslatedMessageContainsNonAscii() From e3b1981e38183a0eb9dd00d7cb970d261490ad47 Mon Sep 17 00:00:00 2001 From: dwgebler Date: Mon, 10 Jun 2024 23:53:51 +0100 Subject: [PATCH 10/10] lint: trigger_deprecation params on one line --- .../Http/Authenticator/AccessTokenAuthenticator.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php b/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php index 321085628599c..b26f76fc36cf7 100644 --- a/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php +++ b/src/Symfony/Component/Security/Http/Authenticator/AccessTokenAuthenticator.php @@ -85,11 +85,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio if (null !== $this->translator) { $errorMessage = $this->translator->trans($exception->getMessageKey(), $exception->getMessageData(), 'security'); if (preg_match('/[^\x00-\x7F]/', $errorMessage)) { - trigger_deprecation( - 'symfony/security-http', - '6.4', - 'Using non-ASCII characters in the error message is deprecated. Use ASCII characters only.' - ); + trigger_deprecation('symfony/security-http', '6.4', 'Using non-ASCII characters in the error message is deprecated. Use ASCII characters only.'); } } else { $errorMessage = strtr($exception->getMessageKey(), $exception->getMessageData()); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy