1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AerialShip\SamlSPBundle\Security\Http\Firewall; |
4
|
|
|
|
5
|
|
|
use AerialShip\SamlSPBundle\Bridge\SamlSpInfo; |
6
|
|
|
use AerialShip\SamlSPBundle\Error\RelyingPartyNotSetException; |
7
|
|
|
use AerialShip\SamlSPBundle\RelyingParty\RelyingPartyInterface; |
8
|
|
|
use AerialShip\SamlSPBundle\Security\Core\Authentication\Token\SamlSpToken; |
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\Security\Core\Exception\AuthenticationException; |
13
|
|
|
use Symfony\Component\Security\Http\Firewall\AbstractAuthenticationListener; |
14
|
|
|
|
15
|
|
|
class SamlSpAuthenticationListener extends AbstractAuthenticationListener |
16
|
|
|
{ |
17
|
|
|
/** @var RelyingPartyInterface */ |
18
|
|
|
protected $relyingParty; |
19
|
|
|
|
20
|
|
|
|
21
|
3 |
|
public function setRelyingParty(RelyingPartyInterface $relyingParty) |
22
|
|
|
{ |
23
|
3 |
|
$this->relyingParty = $relyingParty; |
24
|
3 |
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return RelyingPartyInterface |
29
|
|
|
* @throws RelyingPartyNotSetException |
30
|
|
|
*/ |
31
|
4 |
|
protected function getRelyingParty() |
32
|
|
|
{ |
33
|
4 |
|
if (false == $this->relyingParty) { |
34
|
1 |
|
throw new RelyingPartyNotSetException('The relying party is required for the listener work, but it was not set. Seems like miss configuration'); |
35
|
|
|
} |
36
|
3 |
|
return $this->relyingParty; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
4 |
|
protected function requiresAuthentication(Request $request) |
43
|
|
|
{ |
44
|
4 |
|
return true; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Performs authentication. |
50
|
|
|
* @param Request $request A Request instance |
51
|
|
|
* @throws \Exception |
52
|
|
|
* @throws \Symfony\Component\Security\Core\Exception\AuthenticationException |
53
|
|
|
* @throws \RuntimeException |
54
|
|
|
* @return TokenInterface|Response|null The authenticated token, null if full authentication is not possible, or a Response |
55
|
|
|
*/ |
56
|
4 |
|
protected function attemptAuthentication(Request $request) |
57
|
|
|
{ |
58
|
4 |
|
$myRequest = $request->duplicate(); |
59
|
4 |
|
$this->copyOptionsToRequestAttributes($myRequest); |
60
|
|
|
|
61
|
4 |
|
if (!$this->getRelyingParty()->supports($myRequest)) { |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
$result = $this->getRelyingParty()->manage($myRequest); |
66
|
|
|
|
67
|
3 |
|
if ($result instanceof Response) { |
68
|
2 |
|
return $result; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
if ($result instanceof SamlSpInfo) { |
72
|
1 |
|
$token = new SamlSpToken($this->providerKey); |
73
|
1 |
|
$token->setSamlSpInfo($result); |
74
|
|
|
try { |
75
|
1 |
|
return $this->authenticationManager->authenticate($token); |
76
|
|
|
} catch (AuthenticationException $e) { |
77
|
|
|
$e->setToken($token); |
78
|
|
|
throw $e; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
4 |
|
protected function copyOptionsToRequestAttributes(Request $myRequest) |
86
|
|
|
{ |
87
|
4 |
|
$options = array('login_path', 'check_path', 'logout_path', 'metadata_path', 'discovery_path', |
88
|
4 |
|
'failure_path', 'local_logout_path' |
89
|
4 |
|
); |
90
|
4 |
|
foreach ($options as $name) { |
91
|
4 |
|
if (!empty($this->options[$name])) { |
92
|
4 |
|
$myRequest->attributes->set($name, $this->options[$name]); |
93
|
4 |
|
} |
94
|
4 |
|
} |
95
|
4 |
|
} |
96
|
|
|
} |
97
|
|
|
|