1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kuleuven\AuthenticationBundle\Security; |
4
|
|
|
|
5
|
|
|
use Kuleuven\AuthenticationBundle\Service\ShibbolethServiceProvider; |
6
|
|
|
use Kuleuven\AuthenticationBundle\Traits\LoggerTrait; |
7
|
|
|
use Psr\Log\LoggerAwareInterface; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
9
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
10
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
12
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
13
|
|
|
use Symfony\Component\Security\Http\Event\SwitchUserEvent; |
14
|
|
|
use Symfony\Component\Security\Http\SecurityEvents; |
15
|
|
|
|
16
|
|
|
class ShibbolethSwitchUserPersistenceSubscriber implements EventSubscriberInterface, LoggerAwareInterface |
17
|
|
|
{ |
18
|
|
|
use LoggerTrait; |
19
|
|
|
|
20
|
|
|
protected $session; |
21
|
|
|
protected $tokenStorage; |
22
|
|
|
protected $shibbolethServiceProvider; |
23
|
|
|
protected $userProvider; |
24
|
|
|
protected $userChecker; |
25
|
|
|
protected $accessDecisionManager; |
26
|
|
|
protected $usernameParameter; |
27
|
|
|
protected $role; |
28
|
|
|
protected $sessionKey; |
29
|
|
|
protected $eventDispatcher; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
SessionInterface $session, |
33
|
|
|
TokenStorageInterface $tokenStorage, |
34
|
|
|
ShibbolethServiceProvider $shibbolethServiceProvider, |
35
|
|
|
$sessionKey |
36
|
|
|
) |
37
|
|
|
{ |
38
|
|
|
$this->session = $session; |
39
|
|
|
$this->tokenStorage = $tokenStorage; |
40
|
|
|
$this->shibbolethServiceProvider = $shibbolethServiceProvider; |
41
|
|
|
$this->sessionKey = $sessionKey; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function getSubscribedEvents() |
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
|
|
KernelEvents::REQUEST => [['onKernelRequest', 255]], |
48
|
|
|
SecurityEvents::SWITCH_USER => [['onUserSwitch', 255]], |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function onKernelRequest() |
53
|
|
|
{ |
54
|
|
|
$token = $this->tokenStorage->getToken(); |
55
|
|
|
if (!empty($token)) { |
56
|
|
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$persistedToken = $this->session->get($this->sessionKey); |
60
|
|
|
if (empty($persistedToken) || !$this->supportsToken($persistedToken)) { |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->log(sprintf('Token found in session: %s', $persistedToken)); |
65
|
|
|
|
66
|
|
|
$this->tokenStorage->setToken($persistedToken); |
67
|
|
|
$this->log(sprintf('Token written to storage: %s', $persistedToken)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function onUserSwitch(SwitchUserEvent $event) |
71
|
|
|
{ |
72
|
|
|
if ($event instanceof ShibbolethSwitchUserEvent) { |
73
|
|
|
if ($event->getTargetUser()->getUsername() !== $this->shibbolethServiceProvider->getUsername()) { |
74
|
|
|
$this->session->set($this->sessionKey, $event->getToken()); |
75
|
|
|
$this->log(sprintf('Token persisted in session for username "%s": %s', $event->getTargetUser()->getUsername(), $event->getToken())); |
76
|
|
|
} else { |
77
|
|
|
$this->session->remove($this->sessionKey); |
78
|
|
|
$this->log(sprintf('Token removed from session: %s', $event->getToken())); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function supportsToken(TokenInterface $token) |
84
|
|
|
{ |
85
|
|
|
return |
86
|
|
|
$token instanceof KuleuvenUserToken |
87
|
|
|
&& $token->getUsername() !== $this->shibbolethServiceProvider->getUsername(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|