Skip to content

Commit b30321a

Browse files
committed
Remove logout path condition for Symfony versions < 7
1 parent 95c16cc commit b30321a

File tree

3 files changed

+2
-97
lines changed

3 files changed

+2
-97
lines changed

src/bundle/Resources/config/security.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@
5151
->args([
5252
service('security.access_map'),
5353
service('security.access.decision_manager'),
54-
service('security.http_utils'),
55-
service('security.logout_url_generator'),
5654
])
5755

5856
->set('scheb_two_factor.security.listener.token_created', AuthenticationTokenListener::class)

src/bundle/Security/Authorization/TwoFactorAccessDecider.php

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ class TwoFactorAccessDecider
2323
public function __construct(
2424
private readonly AccessMapInterface $accessMap,
2525
private readonly AccessDecisionManagerInterface $accessDecisionManager,
26-
private readonly HttpUtils $httpUtils,
27-
private readonly LogoutUrlGenerator $logoutUrlGenerator,
2826
) {
2927
}
3028

@@ -48,16 +46,7 @@ public function isAccessible(Request $request, TokenInterface $token): bool
4846
return true;
4947
}
5048

51-
// Compatibility for Symfony < 7.0
52-
// This block of code ensures requests to the logout route can pass.
53-
// The bundle's TwoFactorAccessListener prioritized after the LogoutListener. Though the Firewall class is still
54-
// sorting the LogoutListener in programmatically. When a lazy firewall is used, the LogoutListener is executed
55-
// last, because all other listeners are encapsulated into LazyFirewallContext, which is invoked first.
56-
$logoutPath = $this->removeQueryParameters(
57-
$this->makeRelativeToBaseUrl($this->logoutUrlGenerator->getLogoutPath(), $request),
58-
);
59-
60-
return $this->httpUtils->checkRequestPath($request, $logoutPath); // Let the logout route pass
49+
return false;
6150
}
6251

6352
private function isPubliclyAccessAttribute(array|null $attributes): bool
@@ -69,29 +58,4 @@ private function isPubliclyAccessAttribute(array|null $attributes): bool
6958

7059
return [AuthenticatedVoter::PUBLIC_ACCESS] === $attributes;
7160
}
72-
73-
private function makeRelativeToBaseUrl(string $logoutPath, Request $request): string
74-
{
75-
$baseUrl = $request->getBaseUrl();
76-
if (0 === strlen($baseUrl)) {
77-
return $logoutPath;
78-
}
79-
80-
$pathInfo = substr($logoutPath, strlen($baseUrl));
81-
if ('' === $pathInfo) {
82-
return '/';
83-
}
84-
85-
return $pathInfo;
86-
}
87-
88-
private function removeQueryParameters(string $path): string
89-
{
90-
$queryPos = strpos($path, '?');
91-
if (false !== $queryPos) {
92-
$path = substr($path, 0, $queryPos);
93-
}
94-
95-
return $path;
96-
}
9761
}

tests/Security/Authorization/TwoFactorAccessDeciderTest.php

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,16 @@
1515
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
1616
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
1717
use Symfony\Component\Security\Http\AccessMapInterface;
18-
use Symfony\Component\Security\Http\HttpUtils;
19-
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
2018
use function defined;
2119

2220
class TwoFactorAccessDeciderTest extends TestCase
2321
{
24-
private const BASE_URL = '/app_dev.php';
25-
private const LOGOUT_PATH = '/logout';
26-
private const LOGOUT_PATH_WITH_BASE_URL = self::BASE_URL.self::LOGOUT_PATH;
2722
private const ACCESS_MAP_ATTRIBUTES = [TwoFactorInProgressVoter::IS_AUTHENTICATED_2FA_IN_PROGRESS];
2823

2924
private MockObject|Request $request;
3025
private MockObject|TokenInterface $token;
3126
private MockObject|AccessMapInterface $accessMap;
3227
private MockObject|AccessDecisionManagerInterface $accessDecisionManager;
33-
private MockObject|HttpUtils $httpUtils;
34-
private MockObject|LogoutUrlGenerator $logoutUrlGenerator;
3528
private TwoFactorAccessDecider $accessDecider;
3629

3730
/** @var string[]|null */
@@ -43,9 +36,7 @@ protected function setUp(): void
4336
$this->token = $this->createMock(TokenInterface::class);
4437
$this->accessMap = $this->createMock(AccessMapInterface::class);
4538
$this->accessDecisionManager = $this->createMock(AccessDecisionManagerInterface::class);
46-
$this->httpUtils = $this->createMock(HttpUtils::class);
47-
$this->logoutUrlGenerator = $this->createMock(LogoutUrlGenerator::class);
48-
$this->accessDecider = new TwoFactorAccessDecider($this->accessMap, $this->accessDecisionManager, $this->httpUtils, $this->logoutUrlGenerator);
39+
$this->accessDecider = new TwoFactorAccessDecider($this->accessMap, $this->accessDecisionManager);
4940
}
5041

5142
private function stubAccessMapReturnsAttributes(array|null $attributes): void
@@ -58,14 +49,6 @@ private function stubAccessMapReturnsAttributes(array|null $attributes): void
5849
->willReturn([$attributes, 'https']);
5950
}
6051

61-
private function whenGeneratedLogoutPath(string $generatedLogoutPath): void
62-
{
63-
$this->logoutUrlGenerator
64-
->expects($this->any())
65-
->method('getLogoutPath')
66-
->willReturn($generatedLogoutPath);
67-
}
68-
6952
private function whenRequestBaseUrl(string $baseUrl): void
7053
{
7154
$this->request
@@ -83,15 +66,6 @@ private function whenPathAccess(bool $accessGranted): void
8366
->willReturn($accessGranted);
8467
}
8568

86-
private function whenIsLogoutPath(bool $accessGranted): void
87-
{
88-
$this->httpUtils
89-
->expects($this->any())
90-
->method('checkRequestPath')
91-
->with($this->request, self::LOGOUT_PATH)
92-
->willReturn($accessGranted);
93-
}
94-
9569
/**
9670
* @return iterable<string>
9771
*/
@@ -134,7 +108,6 @@ public function isAccessible_pathAccessGranted_returnTrue(): void
134108
{
135109
$this->stubAccessMapReturnsAttributes(self::ACCESS_MAP_ATTRIBUTES);
136110
$this->whenPathAccess(true);
137-
$this->whenIsLogoutPath(false);
138111

139112
$returnValue = $this->accessDecider->isAccessible($this->request, $this->token);
140113
$this->assertTrue($returnValue);
@@ -146,35 +119,7 @@ public function isAccessible_isPubliclyAccessible_returnTrue(string $publicAcces
146119
{
147120
$this->stubAccessMapReturnsAttributes([$publicAccessAttribute]);
148121
$this->whenRequestBaseUrl('');
149-
$this->whenGeneratedLogoutPath(self::LOGOUT_PATH);
150-
$this->whenPathAccess(false);
151-
$this->whenIsLogoutPath(false);
152-
153-
$returnValue = $this->accessDecider->isAccessible($this->request, $this->token);
154-
$this->assertTrue($returnValue);
155-
}
156-
157-
#[Test]
158-
public function isAccessible_isLogoutPathNoBasePath_returnTrue(): void
159-
{
160-
$this->stubAccessMapReturnsAttributes(self::ACCESS_MAP_ATTRIBUTES);
161-
$this->whenRequestBaseUrl('');
162-
$this->whenGeneratedLogoutPath(self::LOGOUT_PATH);
163-
$this->whenPathAccess(false);
164-
$this->whenIsLogoutPath(true);
165-
166-
$returnValue = $this->accessDecider->isAccessible($this->request, $this->token);
167-
$this->assertTrue($returnValue);
168-
}
169-
170-
#[Test]
171-
public function isAccessible_isLogoutPathWithBasePath_returnTrue(): void
172-
{
173-
$this->stubAccessMapReturnsAttributes(self::ACCESS_MAP_ATTRIBUTES);
174-
$this->whenRequestBaseUrl(self::BASE_URL);
175-
$this->whenGeneratedLogoutPath(self::LOGOUT_PATH_WITH_BASE_URL);
176122
$this->whenPathAccess(false);
177-
$this->whenIsLogoutPath(true);
178123

179124
$returnValue = $this->accessDecider->isAccessible($this->request, $this->token);
180125
$this->assertTrue($returnValue);
@@ -185,9 +130,7 @@ public function isAccessible_isNotAccessible_returnFalse(): void
185130
{
186131
$this->stubAccessMapReturnsAttributes(self::ACCESS_MAP_ATTRIBUTES);
187132
$this->whenRequestBaseUrl('');
188-
$this->whenGeneratedLogoutPath(self::LOGOUT_PATH);
189133
$this->whenPathAccess(false);
190-
$this->whenIsLogoutPath(false);
191134

192135
$returnValue = $this->accessDecider->isAccessible($this->request, $this->token);
193136
$this->assertFalse($returnValue);

0 commit comments

Comments
 (0)