Skip to content

Commit 5317c40

Browse files
authored
decorate ContextListener for flex stateless (#10)
1 parent 261a211 commit 5317c40

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace FlexAuth\Security;
4+
5+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
6+
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
7+
8+
/**
9+
* Class EnableListenerDecorator
10+
* Allow to switch off listener which was subscribed already
11+
*
12+
* @author Aleksandr Arofikin <sashaaro@gmail.com>
13+
*/
14+
abstract class EnableListenerDecorator implements ListenerInterface
15+
{
16+
/** @var ListenerInterface */
17+
protected $listener;
18+
19+
public function __construct(ListenerInterface $listener)
20+
{
21+
$this->listener = $listener;
22+
}
23+
24+
public function handle(GetResponseEvent $event)
25+
{
26+
if ($this->isEnabled()) {
27+
$this->listener->handle($event);
28+
}
29+
}
30+
31+
abstract public function isEnabled(): bool;
32+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace FlexAuth\Security;
4+
5+
use FlexAuth\FlexAuthTypeProviderInterface;
6+
use Symfony\Component\Security\Http\Firewall\ContextListener;
7+
8+
/**
9+
* Allow dynamically determinate firewall is stateless or not.
10+
* Serve for switch between login via form with session authentication and jwt which stateless
11+
*
12+
* Class FlexAuthContextListenerDecorator
13+
* @author Aleksandr Arofikin <sashaaro@gmail.com>
14+
*/
15+
class FlexAuthContextListenerDecorator extends EnableListenerDecorator
16+
{
17+
/** @var FlexAuthTypeProviderInterface */
18+
protected $authTypeProvider;
19+
20+
protected $statelessTypes = [];
21+
22+
public function __construct(ContextListener $contextListener, FlexAuthTypeProviderInterface $authTypeProvider)
23+
{
24+
parent::__construct($contextListener);
25+
$this->authTypeProvider = $authTypeProvider;
26+
}
27+
28+
public function isEnabled(): bool
29+
{
30+
return !$this->isStateless();
31+
}
32+
33+
private function isStateless()
34+
{
35+
return in_array($this->authTypeProvider->provide()['type'], $this->statelessTypes, true);
36+
}
37+
38+
public function addStatelessType(string $type)
39+
{
40+
$this->statelessTypes[] = $type;
41+
}
42+
}

0 commit comments

Comments
 (0)