1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kuleuven\AuthenticationBundle\Security; |
4
|
|
|
|
5
|
|
|
use Kuleuven\AuthenticationBundle\Service\ShibbolethServiceProvider; |
6
|
|
|
use Kuleuven\AuthenticationBundle\Traits\LoggerTrait; |
7
|
|
|
use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface; |
8
|
|
|
use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
12
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
13
|
|
|
|
14
|
|
|
class ShibbolethHttpLogoutHandler implements LogoutHandlerInterface, LogoutSuccessHandlerInterface |
15
|
|
|
{ |
16
|
|
|
use LoggerTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ShibbolethServiceProvider |
20
|
|
|
*/ |
21
|
|
|
protected $shibbolethServiceProvider; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $target; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param ShibbolethServiceProvider $shibbolethServiceProvider |
30
|
|
|
* @param null|string $target |
31
|
|
|
*/ |
32
|
|
|
public function __construct(ShibbolethServiceProvider $shibbolethServiceProvider, $target = null) |
33
|
|
|
{ |
34
|
|
|
$this->shibbolethServiceProvider = $shibbolethServiceProvider; |
35
|
|
|
$this->target = $target; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
41
|
|
|
public function logout(Request $request, Response $response, TokenInterface $token) |
42
|
|
|
{ |
43
|
|
|
if ($token instanceof KuleuvenUserToken) { |
44
|
|
|
$request->getSession()->invalidate(); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
|
public function onLogoutSuccess(Request $request) |
52
|
|
|
{ |
53
|
|
|
$target = $this->target; |
54
|
|
|
if (empty($target)) { |
55
|
|
|
if ($request->query->has('target')) { |
56
|
|
|
$target = $request->query->get('target'); |
57
|
|
|
} else { |
58
|
|
|
$target = $request->headers->get('referer', '/'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
$url = $this->shibbolethServiceProvider->getLogoutUrl($target); |
62
|
|
|
$this->log(sprintf('Redirecting after logout to "%s"...', $url)); |
63
|
|
|
return new RedirectResponse($url); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|