|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Biurad opensource projects. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7.4 and above required |
|
9
|
|
|
* |
|
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
|
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
15
|
|
|
* file that was distributed with this source code. |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Biurad\Security\Handler; |
|
20
|
|
|
|
|
21
|
|
|
use Biurad\Http\Request; |
|
22
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
24
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
25
|
|
|
use Symfony\Component\Security\Csrf\TokenStorage\ClearableTokenStorageInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The default logout handler. |
|
29
|
|
|
* |
|
30
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
class LogoutHandler |
|
33
|
|
|
{ |
|
34
|
|
|
private TokenStorageInterface $tokenStorage; |
|
35
|
|
|
private ClearableTokenStorageInterface $csrfTokenStorage; |
|
36
|
|
|
private ?SessionInterface $session; |
|
37
|
|
|
private ?RememberMeHandler $rememberMeHandler; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct( |
|
40
|
|
|
TokenStorageInterface $tokenStorage, |
|
41
|
|
|
ClearableTokenStorageInterface $csrfTokenStorage, |
|
42
|
|
|
RememberMeHandler $rememberMeHandler = null, |
|
43
|
|
|
SessionInterface $session = null |
|
44
|
|
|
) |
|
45
|
|
|
{ |
|
|
|
|
|
|
46
|
|
|
$this->session = $session; |
|
47
|
|
|
$this->tokenStorage = $tokenStorage; |
|
48
|
|
|
$this->csrfTokenStorage = $csrfTokenStorage; |
|
49
|
|
|
$this->rememberMeHandler = $rememberMeHandler; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Handler for: |
|
54
|
|
|
* - clearing invalidating the current session |
|
55
|
|
|
* - clearing the token storage |
|
56
|
|
|
* - clearing the CSRF token storage |
|
57
|
|
|
* - clearing the remember me cookie if needed. |
|
58
|
|
|
* |
|
59
|
|
|
* @return array<int,Cookie> The remember me clearing cookies if any. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function handle(ServerRequestInterface $request): array |
|
62
|
|
|
{ |
|
63
|
|
|
$this->tokenStorage->setToken(); |
|
64
|
|
|
$this->csrfTokenStorage->clear(); |
|
65
|
|
|
|
|
66
|
|
|
if (null === $this->session && $request instanceof Request && $request->getRequest()->hasSession()) { |
|
67
|
|
|
$this->session = $request->getRequest()->getSession(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (null !== $this->session) { |
|
71
|
|
|
$this->session->invalidate(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (null !== $this->rememberMeHandler) { |
|
75
|
|
|
return $this->rememberMeHandler->clearRememberMeCookies($request); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return null !== $this->rememberMeHandler ? $this->rememberMeHandler->clearRememberMeCookies($request) : []; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|