1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Security; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Symfony\Component\Routing\RouterInterface; |
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
10
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
11
|
|
|
use Symfony\Component\Security\Core\Security; |
12
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
13
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
14
|
|
|
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Provides authentication using Shibboleth. |
19
|
|
|
*/ |
20
|
|
|
class ShibbolethAuthenticator extends AbstractGuardAuthenticator |
21
|
|
|
{ |
22
|
|
|
private $router; |
23
|
|
|
private $security; |
24
|
|
|
private $shibUaid; |
25
|
|
|
|
26
|
|
|
public function __construct(RouterInterface $router, Security $security, $shibUaid) |
27
|
|
|
{ |
28
|
|
|
$this->router = $router; |
29
|
|
|
$this->security = $security; |
30
|
|
|
$this->shibUaid = $shibUaid; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function supports(Request $request) |
34
|
|
|
{ |
35
|
|
|
if ($this->security->getUser()) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
return $request->server->has($this->shibUaid); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Request $request The request that resulted in an AuthenticationException |
43
|
|
|
* @param AuthenticationException $authException The exception that started the authentication process |
44
|
|
|
* |
45
|
|
|
* @return Response |
46
|
|
|
*/ |
47
|
|
|
public function start(Request $request, AuthenticationException $authException = null) |
48
|
|
|
{ |
49
|
|
|
return new RedirectResponse($this->router->generate('login')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Request $request |
54
|
|
|
* |
55
|
|
|
* @return mixed|null |
56
|
|
|
*/ |
57
|
|
|
public function getCredentials(Request $request) |
58
|
|
|
{ |
59
|
|
|
return ['uaid' => $request->server->get($this->shibUaid)]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* |
64
|
|
|
* @param mixed $credentials |
65
|
|
|
* @param UserProviderInterface $userProvider |
66
|
|
|
* |
67
|
|
|
* @throws AuthenticationException |
68
|
|
|
* |
69
|
|
|
* @return UserInterface|null |
70
|
|
|
*/ |
71
|
|
|
public function getUser($credentials, UserProviderInterface $userProvider) |
72
|
|
|
{ |
73
|
|
|
return $userProvider->loadUserByUsername($credentials['uaid']); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param mixed $credentials |
78
|
|
|
* @param UserInterface $user |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
* |
82
|
|
|
* @throws AuthenticationException |
83
|
|
|
*/ |
84
|
|
|
public function checkCredentials($credentials, UserInterface $user) |
85
|
|
|
{ |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Request $request |
91
|
|
|
* @param AuthenticationException $exception |
92
|
|
|
* |
93
|
|
|
* @return Response|null |
94
|
|
|
*/ |
95
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
96
|
|
|
{ |
97
|
|
|
return new RedirectResponse($this->router->generate('login')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Request $request |
102
|
|
|
* @param TokenInterface $token |
103
|
|
|
* @param string $providerKey The provider (i.e. firewall) key |
104
|
|
|
* |
105
|
|
|
* @return Response|null |
106
|
|
|
*/ |
107
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) |
108
|
|
|
{ |
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
public function supportsRememberMe() |
116
|
|
|
{ |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|