1 | <?php |
||
31 | class ExplicitSessionTimeoutHandler implements AuthenticationHandler |
||
32 | { |
||
33 | /** |
||
34 | * @var AuthenticationHandler|null |
||
35 | */ |
||
36 | private $nextHandler; |
||
37 | |||
38 | /** |
||
39 | * @var TokenStorageInterface |
||
40 | */ |
||
41 | private $tokenStorage; |
||
42 | |||
43 | /** |
||
44 | * @var SessionLifetimeGuard |
||
45 | */ |
||
46 | private $sessionLifetimeGuard; |
||
47 | |||
48 | /** |
||
49 | * @var AuthenticatedSessionStateHandler |
||
50 | */ |
||
51 | private $authenticatedSession; |
||
52 | |||
53 | /** |
||
54 | * @var SessionLogoutHandler |
||
55 | */ |
||
56 | private $sessionLogoutHandler; |
||
57 | |||
58 | /** |
||
59 | * @var CookieClearingLogoutHandler |
||
60 | */ |
||
61 | private $cookieClearingLogoutHandler; |
||
62 | |||
63 | /** |
||
64 | * @var RouterInterface |
||
65 | */ |
||
66 | private $router; |
||
67 | /** |
||
68 | * @var LoggerInterface |
||
69 | */ |
||
70 | private $logger; |
||
71 | |||
72 | public function __construct( |
||
89 | |||
90 | public function process(GetResponseEvent $event) |
||
91 | { |
||
92 | if ($this->tokenStorage->getToken() !== null |
||
93 | && !$this->sessionLifetimeGuard->sessionLifetimeWithinLimits($this->authenticatedSession) |
||
94 | ) { |
||
95 | $invalidatedBy = []; |
||
96 | if (!$this->sessionLifetimeGuard->sessionLifetimeWithinAbsoluteLimit($this->authenticatedSession)) { |
||
97 | $invalidatedBy[] = 'absolute'; |
||
98 | } |
||
99 | |||
100 | if (!$this->sessionLifetimeGuard->sessionLifetimeWithinRelativeLimit($this->authenticatedSession)) { |
||
101 | $invalidatedBy[] = 'relative'; |
||
102 | } |
||
103 | |||
104 | $this->logger->notice(sprintf( |
||
105 | 'Authenticated user found, but session was determined to be outside of the "%s" time limit. User will ' |
||
106 | . 'be logged out and redirected to session-expired page to attempt new login.', |
||
107 | implode(' and ', $invalidatedBy) |
||
108 | )); |
||
109 | |||
110 | |||
111 | $token = $this->tokenStorage->getToken(); |
||
112 | $request = $event->getRequest(); |
||
113 | |||
114 | // if the current request was not a GET request we cannot safely redirect to that page after login as it |
||
115 | // may require a form resubmit for instance. Therefor, we redirect to the last GET request (either current |
||
116 | // or previous). |
||
117 | $afterLoginRedirectTo = $this->authenticatedSession->getCurrentRequestUri(); |
||
118 | if ($event->getRequest()->getMethod() === 'GET') { |
||
119 | $afterLoginRedirectTo = $event->getRequest()->getRequestUri(); |
||
120 | } |
||
121 | |||
122 | // log the user out using Symfony methodology, see the LogoutListener |
||
123 | $event->setResponse(new RedirectResponse($this->router->generate('selfservice_security_session_expired'))); |
||
124 | |||
125 | $this->sessionLogoutHandler->logout($request, $event->getResponse(), $token); |
||
126 | $this->cookieClearingLogoutHandler->logout($request, $event->getResponse(), $token); |
||
127 | $this->tokenStorage->setToken(null); |
||
128 | |||
129 | // the session is restarted after invalidation during the logout, so we can (re)store the last GET request |
||
130 | $this->authenticatedSession->setCurrentRequestUri($afterLoginRedirectTo); |
||
131 | |||
132 | return; |
||
133 | } |
||
134 | |||
135 | if ($this->nextHandler !== null) { |
||
136 | $this->nextHandler->process($event); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | public function setNext(AuthenticationHandler $handler) |
||
144 | } |
||
145 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.