|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AerialShip\SamlSPBundle\Bridge; |
|
4
|
|
|
|
|
5
|
|
|
use AerialShip\LightSaml\Binding\HttpRedirect; |
|
6
|
|
|
use AerialShip\LightSaml\Meta\AuthnRequestBuilder; |
|
7
|
|
|
use AerialShip\SamlSPBundle\Config\ServiceInfoCollection; |
|
8
|
|
|
use AerialShip\SamlSPBundle\RelyingParty\RelyingPartyInterface; |
|
9
|
|
|
use AerialShip\SamlSPBundle\State\Request\RequestState; |
|
10
|
|
|
use AerialShip\SamlSPBundle\State\Request\RequestStateStoreInterface; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
14
|
|
|
use Symfony\Component\Security\Http\HttpUtils; |
|
15
|
|
|
|
|
16
|
|
|
class Authenticate implements RelyingPartyInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var ServiceInfoCollection */ |
|
19
|
|
|
protected $serviceInfoCollection; |
|
20
|
|
|
|
|
21
|
|
|
/** @var RequestStateStoreInterface */ |
|
22
|
|
|
protected $requestStore; |
|
23
|
|
|
|
|
24
|
|
|
/** @var \Symfony\Component\Security\Http\HttpUtils */ |
|
25
|
|
|
protected $httpUtils; |
|
26
|
|
|
|
|
27
|
|
|
/** @var \AerialShip\SamlSPBundle\Bridge\BindingManager */ |
|
28
|
|
|
protected $bindingManager; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param ServiceInfoCollection $serviceInfoCollection |
|
33
|
|
|
* @param RequestStateStoreInterface $requestStore |
|
34
|
|
|
* @param HttpUtils $httpUtils |
|
35
|
|
|
* @param BindingManager $bindingManager |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct( |
|
38
|
|
|
ServiceInfoCollection $serviceInfoCollection, |
|
39
|
|
|
RequestStateStoreInterface $requestStore, |
|
40
|
|
|
HttpUtils $httpUtils, |
|
41
|
|
|
BindingManager $bindingManager |
|
42
|
|
|
) { |
|
43
|
|
|
$this->serviceInfoCollection = $serviceInfoCollection; |
|
44
|
|
|
$this->requestStore = $requestStore; |
|
45
|
|
|
$this->httpUtils = $httpUtils; |
|
46
|
|
|
$this->bindingManager = $bindingManager; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
|
|
public function supports(Request $request) |
|
56
|
|
|
{ |
|
57
|
|
|
$result = $request->attributes->get('login_path') == $request->getPathInfo(); |
|
58
|
|
|
return $result; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
63
|
|
|
* @throws \InvalidArgumentException if cannot manage the Request |
|
64
|
|
|
* @return \Symfony\Component\HttpFoundation\Response|SamlSpInfo |
|
65
|
|
|
*/ |
|
66
|
|
|
public function manage(Request $request) |
|
67
|
|
|
{ |
|
68
|
|
|
if (false == $this->supports($request)) { |
|
|
|
|
|
|
69
|
|
|
throw new \InvalidArgumentException('Unsupported request'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$serviceInfo = $this->serviceInfoCollection->findByAS($request->query->get('as')); |
|
73
|
|
|
if (!$serviceInfo) { |
|
74
|
|
|
return new RedirectResponse($this->httpUtils->generateUri($request, $request->attributes->get('discovery_path'))); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$serviceInfo->getSpProvider()->setRequest($request); |
|
78
|
|
|
$spED = $serviceInfo->getSpProvider()->getEntityDescriptor(); |
|
79
|
|
|
|
|
80
|
|
|
$idpED = $serviceInfo->getIdpProvider()->getEntityDescriptor(); |
|
81
|
|
|
$spMeta = $serviceInfo->getSpMetaProvider()->getSpMeta(); |
|
82
|
|
|
|
|
83
|
|
|
$builder = new AuthnRequestBuilder($spED, $idpED, $spMeta); |
|
84
|
|
|
$message = $builder->build(); |
|
85
|
|
|
|
|
86
|
|
|
if ($serviceInfo->getSpSigningProvider()->isEnabled()) { |
|
87
|
|
|
$message->sign($serviceInfo->getSpSigningProvider()->getCertificate(), $serviceInfo->getSpSigningProvider()->getPrivateKey()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$binding = $this->bindingManager->instantiate($spMeta->getAuthnRequestBinding()); |
|
91
|
|
|
|
|
92
|
|
|
$bindingResponse = $binding->send($message); |
|
93
|
|
|
if ($bindingResponse instanceof \AerialShip\LightSaml\Binding\RedirectResponse) { |
|
94
|
|
|
$result = new RedirectResponse($bindingResponse->getDestination()); |
|
95
|
|
|
} else if ($bindingResponse instanceof \AerialShip\LightSaml\Binding\PostResponse) { |
|
96
|
|
|
$result = new Response($bindingResponse->render()); |
|
97
|
|
|
} else { |
|
98
|
|
|
throw new \RuntimeException('Unrecognized binding response '.get_class($bindingResponse)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$state = new RequestState(); |
|
102
|
|
|
$state->setId($message->getID()); |
|
103
|
|
|
$state->setDestination($serviceInfo->getIdpProvider()->getEntityDescriptor()->getEntityID()); |
|
104
|
|
|
$this->requestStore->set($state); |
|
105
|
|
|
|
|
106
|
|
|
return $result; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.