Skip to content

Commit a78b850

Browse files
committed
feature #41965 [Security] Deprecate "always authenticate" and "exception on no token" (wouterj)
This PR was merged into the 5.4 branch. Discussion ---------- [Security] Deprecate "always authenticate" and "exception on no token" | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | Ref #41613 | License | MIT | Doc PR | n/a Commits ------- 4bba287259 [Security] Deprecate "always authenticate" and "exception on no token"
2 parents 4a84343 + a3ea6f9 commit a78b850

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

Authorization/AuthorizationChecker.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ class AuthorizationChecker implements AuthorizationCheckerInterface
3434

3535
public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager, AccessDecisionManagerInterface $accessDecisionManager, bool $alwaysAuthenticate = false, bool $exceptionOnNoToken = true)
3636
{
37+
if (false !== $alwaysAuthenticate) {
38+
trigger_deprecation('symfony/security-core', '5.4', 'Not setting the 4th argument of "%s" to "false" is deprecated.', __METHOD__);
39+
}
40+
if (false !== $exceptionOnNoToken) {
41+
trigger_deprecation('symfony/security-core', '5.4', 'Not setting the 5th argument of "%s" to "false" is deprecated.', __METHOD__);
42+
}
43+
3744
$this->tokenStorage = $tokenStorage;
3845
$this->authenticationManager = $authenticationManager;
3946
$this->accessDecisionManager = $accessDecisionManager;

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
7+
* Deprecate setting the 4th argument (`$alwaysAuthenticate`) to `true` and not setting the
8+
5th argument (`$exceptionOnNoToken`) to `false` of `AuthorizationChecker`
9+
410
5.3
511
---
612

Tests/Authorization/AuthorizationCheckerTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ protected function setUp(): void
3636
$this->authorizationChecker = new AuthorizationChecker(
3737
$this->tokenStorage,
3838
$this->authenticationManager,
39-
$this->accessDecisionManager
39+
$this->accessDecisionManager,
40+
false,
41+
false
4042
);
4143
}
4244

@@ -71,13 +73,23 @@ public function testVoteAuthenticatesTokenIfNecessary()
7173
$this->assertSame($newToken, $this->tokenStorage->getToken());
7274
}
7375

74-
public function testVoteWithoutAuthenticationToken()
76+
/**
77+
* @group legacy
78+
*/
79+
public function testLegacyVoteWithoutAuthenticationToken()
7580
{
81+
$authorizationChecker = new AuthorizationChecker(
82+
$this->tokenStorage,
83+
$this->authenticationManager,
84+
$this->accessDecisionManager
85+
);
86+
7687
$this->expectException(AuthenticationCredentialsNotFoundException::class);
77-
$this->authorizationChecker->isGranted('ROLE_FOO');
88+
89+
$authorizationChecker->isGranted('ROLE_FOO');
7890
}
7991

80-
public function testVoteWithoutAuthenticationTokenAndExceptionOnNoTokenIsFalse()
92+
public function testVoteWithoutAuthenticationToken()
8193
{
8294
$authorizationChecker = new AuthorizationChecker($this->tokenStorage, $this->authenticationManager, $this->accessDecisionManager, false, false);
8395

Tests/Authorization/ExpressionLanguageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testIsAuthenticated($token, $expression, $result)
3737
$tokenStorage = new TokenStorage();
3838
$tokenStorage->setToken($token);
3939
$accessDecisionManager = new AccessDecisionManager([new RoleVoter(), new AuthenticatedVoter($trustResolver)]);
40-
$authChecker = new AuthorizationChecker($tokenStorage, $this->createMock(AuthenticationManagerInterface::class), $accessDecisionManager);
40+
$authChecker = new AuthorizationChecker($tokenStorage, $this->createMock(AuthenticationManagerInterface::class), $accessDecisionManager, false, false);
4141

4242
$context = [];
4343
$context['auth_checker'] = $authChecker;

0 commit comments

Comments
 (0)