Skip to content

Commit

Permalink
[SecurityBundle] Set request stateless when firewall is stateless
Browse files Browse the repository at this point in the history
  • Loading branch information
alamirault authored and fabpot committed Dec 18, 2022
1 parent c79d4ab commit ce458c6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Deprecate enabling bundle and not configuring it
* Add `_stateless` attribute to the request when firewall is stateless

6.2
---
Expand Down
9 changes: 8 additions & 1 deletion src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ private function getFirewallContext(Request $request): ?FirewallContext
if (null === $requestMatcher || $requestMatcher->matches($request)) {
$request->attributes->set('_firewall_context', $contextId);

return $this->container->get($contextId);
/** @var FirewallContext $context */
$context = $this->container->get($contextId);

if ($context->getConfig()?->isStateless()) {
$request->attributes->set('_stateless', true);
}

return $context;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function testGetListenersWithInvalidParameter()
$this->assertEquals([[], null, null], $firewallMap->getListeners($request));
$this->assertNull($firewallMap->getFirewallConfig($request));
$this->assertFalse($request->attributes->has(self::ATTRIBUTE_FIREWALL_CONTEXT));
$this->assertFalse($request->attributes->has('_stateless'));
}

public function testGetListeners()
Expand All @@ -62,8 +63,8 @@ public function testGetListeners()

$firewallContext = $this->createMock(FirewallContext::class);

$firewallConfig = new FirewallConfig('main', 'user_checker');
$firewallContext->expects($this->once())->method('getConfig')->willReturn($firewallConfig);
$firewallConfig = new FirewallConfig('main', 'user_checker', null, true, true);
$firewallContext->expects($this->exactly(2))->method('getConfig')->willReturn($firewallConfig);

$listener = function () {};
$firewallContext->expects($this->once())->method('getListeners')->willReturn([$listener]);
Expand All @@ -88,5 +89,6 @@ public function testGetListeners()
$this->assertEquals([[$listener], $exceptionListener, $logoutListener], $firewallMap->getListeners($request));
$this->assertEquals($firewallConfig, $firewallMap->getFirewallConfig($request));
$this->assertEquals('security.firewall.map.context.foo', $request->attributes->get(self::ATTRIBUTE_FIREWALL_CONTEXT));
$this->assertTrue($request->attributes->get('_stateless'));
}
}

0 comments on commit ce458c6

Please sign in to comment.