Skip to content

Commit 71c434b

Browse files
chalasrnicolas-grekas
authored andcommitted
[Security] Deprecate UserInterface & TokenInterface's eraseCredentials()
1 parent ca54e55 commit 71c434b

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

Authentication/AuthenticatorManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public function __construct(
6969
}
7070

7171
$this->exposeSecurityErrors = $exposeSecurityErrors;
72+
73+
if ($eraseCredentials) {
74+
trigger_deprecation('symfony/security-http', '7.3', sprintf('Passing true as "$eraseCredentials" argument to "%s::__construct()" is deprecated and won\'t have any effect in 8.0, pass "false" instead and use your own erasing logic if needed.', __CLASS__));
75+
}
7276
}
7377

7478
/**
@@ -208,6 +212,7 @@ private function executeAuthenticator(AuthenticatorInterface $authenticator, Req
208212
// announce the authentication token
209213
$authenticatedToken = $this->eventDispatcher->dispatch(new AuthenticationTokenCreatedEvent($authenticatedToken, $passport))->getAuthenticatedToken();
210214

215+
// @deprecated since Symfony 7.3, remove the if statement in 8.0
211216
if (true === $this->eraseCredentials) {
212217
$authenticatedToken->eraseCredentials();
213218
}

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ CHANGELOG
66

77
* Add encryption support to `OidcTokenHandler` (JWE)
88
* Replace `$hideAccountStatusExceptions` argument with `$exposeSecurityErrors` in `AuthenticatorManager` constructor
9+
* Deprecate passing `true` for the `$eraseCredentials` parameter of `AuthenticatorManager::__construct()`, erase credentials
10+
on your own e.g. upon `AuthenticationTokenCreatedEvent` instead. Passing it won't thave any effect in 8.0.
911

1012
7.2
1113
---

Tests/Authentication/AuthenticatorManagerTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPUnit\Framework\TestCase;
1616
use Psr\Log\AbstractLogger;
1717
use Psr\Log\LoggerInterface;
18+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1819
use Symfony\Component\EventDispatcher\EventDispatcher;
1920
use Symfony\Component\HttpFoundation\Request;
2021
use Symfony\Component\HttpFoundation\Response;
@@ -40,6 +41,8 @@
4041

4142
class AuthenticatorManagerTest extends TestCase
4243
{
44+
use ExpectDeprecationTrait;
45+
4346
private MockObject&TokenStorageInterface $tokenStorage;
4447
private EventDispatcher $eventDispatcher;
4548
private Request $request;
@@ -163,7 +166,7 @@ public function testRequiredBadgeMissing()
163166

164167
$authenticator->expects($this->once())->method('onAuthenticationFailure')->with($this->anything(), $this->callback(fn ($exception) => 'Authentication failed; Some badges marked as required by the firewall config are not available on the passport: "'.CsrfTokenBadge::class.'".' === $exception->getMessage()));
165168

166-
$manager = $this->createManager([$authenticator], 'main', true, [CsrfTokenBadge::class], exposeSecurityErrors: ExposeSecurityLevel::None);
169+
$manager = $this->createManager([$authenticator], 'main', false, [CsrfTokenBadge::class], exposeSecurityErrors: ExposeSecurityLevel::None);
167170
$manager->authenticateRequest($this->request);
168171
}
169172

@@ -179,11 +182,12 @@ public function testAllRequiredBadgesPresent()
179182

180183
$authenticator->expects($this->once())->method('onAuthenticationSuccess');
181184

182-
$manager = $this->createManager([$authenticator], 'main', true, [CsrfTokenBadge::class], exposeSecurityErrors: ExposeSecurityLevel::None);
185+
$manager = $this->createManager([$authenticator], 'main', false, [CsrfTokenBadge::class], exposeSecurityErrors: ExposeSecurityLevel::None);
183186
$manager->authenticateRequest($this->request);
184187
}
185188

186189
/**
190+
* @group legacy
187191
* @dataProvider provideEraseCredentialsData
188192
*/
189193
public function testEraseCredentials($eraseCredentials)
@@ -197,6 +201,10 @@ public function testEraseCredentials($eraseCredentials)
197201

198202
$this->token->expects($eraseCredentials ? $this->once() : $this->never())->method('eraseCredentials');
199203

204+
if ($eraseCredentials) {
205+
$this->expectDeprecation('Since symfony/security-http 7.3: Passing true as "$eraseCredentials" argument to "Symfony\Component\Security\Http\Authentication\AuthenticatorManager::__construct()" is deprecated and won\'t have any effect in 8.0, pass "false" instead and use your own erasing logic if needed.');
206+
}
207+
200208
$manager = $this->createManager([$authenticator], 'main', $eraseCredentials, exposeSecurityErrors: ExposeSecurityLevel::None);
201209
$manager->authenticateRequest($this->request);
202210
}
@@ -403,7 +411,7 @@ public function log($level, $message, array $context = []): void
403411
}
404412
};
405413

406-
$manager = $this->createManager([$authenticator], 'main', true, [], $logger, exposeSecurityErrors: ExposeSecurityLevel::None);
414+
$manager = $this->createManager([$authenticator], 'main', false, [], $logger, exposeSecurityErrors: ExposeSecurityLevel::None);
407415
$response = $manager->authenticateRequest($this->request);
408416
$this->assertSame($this->response, $response);
409417
$this->assertStringContainsString($authenticator::class, $logger->logContexts[0]['authenticator']);
@@ -422,7 +430,7 @@ private static function createDummySupportsAuthenticator(?bool $supports = true)
422430
return new DummySupportsAuthenticator($supports);
423431
}
424432

425-
private function createManager($authenticators, $firewallName = 'main', $eraseCredentials = true, array $requiredBadges = [], ?LoggerInterface $logger = null, ExposeSecurityLevel $exposeSecurityErrors = ExposeSecurityLevel::AccountStatus)
433+
private function createManager($authenticators, $firewallName = 'main', $eraseCredentials = false, array $requiredBadges = [], ?LoggerInterface $logger = null, ExposeSecurityLevel $exposeSecurityErrors = ExposeSecurityLevel::AccountStatus)
426434
{
427435
return new AuthenticatorManager($authenticators, $this->tokenStorage, $this->eventDispatcher, $firewallName, $logger, $eraseCredentials, $exposeSecurityErrors, $requiredBadges);
428436
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"symfony/http-kernel": "^6.4|^7.0",
2323
"symfony/polyfill-mbstring": "~1.0",
2424
"symfony/property-access": "^6.4|^7.0",
25-
"symfony/security-core": "^7.2",
25+
"symfony/security-core": "^7.3",
2626
"symfony/service-contracts": "^2.5|^3"
2727
},
2828
"require-dev": {

0 commit comments

Comments
 (0)