|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AerialShip\SamlSPBundle\Bridge; |
|
4
|
|
|
|
|
5
|
|
|
use AerialShip\SamlSPBundle\Config\ServiceInfoCollection; |
|
6
|
|
|
use AerialShip\SamlSPBundle\RelyingParty\RelyingPartyInterface; |
|
7
|
|
|
use Symfony\Component\Templating\EngineInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
11
|
|
|
use Symfony\Component\Security\Http\HttpUtils; |
|
12
|
|
|
|
|
13
|
|
|
class Discovery implements RelyingPartyInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var ServiceInfoCollection */ |
|
16
|
|
|
protected $metaProviders; |
|
17
|
|
|
|
|
18
|
|
|
/** @var \Symfony\Component\Templating\EngineInterface */ |
|
19
|
|
|
protected $twig; |
|
20
|
|
|
|
|
21
|
|
|
/** @var \Symfony\Component\Security\Http\HttpUtils */ |
|
22
|
|
|
protected $httpUtils; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $providerID |
|
27
|
|
|
* @param ServiceInfoCollection $metaProviders |
|
28
|
|
|
* @param EngineInterface $twig |
|
29
|
|
|
* @param HttpUtils $httpUtils |
|
30
|
|
|
*/ |
|
31
|
|
|
function __construct($providerID, ServiceInfoCollection $metaProviders, EngineInterface $twig, HttpUtils $httpUtils) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
$this->metaProviders = $metaProviders; |
|
34
|
|
|
$this->twig = $twig; |
|
35
|
|
|
$this->httpUtils = $httpUtils; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
41
|
|
|
* @return bool |
|
42
|
|
|
*/ |
|
43
|
|
|
public function supports(Request $request) |
|
44
|
|
|
{ |
|
45
|
|
|
$result = $request->attributes->get('discovery_path') == $request->getPathInfo(); |
|
46
|
|
|
return $result; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
51
|
|
|
* @throws \RuntimeException |
|
52
|
|
|
* @throws \InvalidArgumentException if cannot manage the Request |
|
53
|
|
|
* @return \Symfony\Component\HttpFoundation\Response|SamlSpInfo |
|
54
|
|
|
*/ |
|
55
|
|
|
public function manage(Request $request) |
|
56
|
|
|
{ |
|
57
|
|
|
if (!$this->supports($request)) { |
|
58
|
|
|
throw new \InvalidArgumentException('Unsupported request'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$path = $this->getPath($request); |
|
62
|
|
|
|
|
63
|
|
|
$allProviders = $this->metaProviders->all(); |
|
64
|
|
|
|
|
65
|
|
|
if (count($allProviders) == 1) { |
|
66
|
|
|
// there's only one idp... go straight to it |
|
67
|
|
|
$names = array_keys($allProviders); |
|
68
|
|
|
return new RedirectResponse($path.'?as='.array_pop($names)); |
|
69
|
|
|
} else if (count($allProviders) == 0) { |
|
70
|
|
|
// configuration validation should ensure this... but anyway just to be sure |
|
71
|
|
|
throw new \RuntimeException('At least one authentication service required in configuration'); |
|
72
|
|
|
} else { |
|
73
|
|
|
//$this->metaProviders->get('')->getIdpProvider()->getEntityDescriptor()->getEntityID() |
|
74
|
|
|
// present user to choose which idp he wants to authenticate with |
|
75
|
|
|
return new Response($this->twig->render( |
|
76
|
|
|
'@AerialShipSamlSP/discovery.html.twig', |
|
77
|
|
|
array( |
|
78
|
|
|
'providers' => $this->metaProviders->all(), |
|
79
|
|
|
'path' => $path |
|
80
|
|
|
) |
|
81
|
|
|
)); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
protected function getPath(Request $request) |
|
87
|
|
|
{ |
|
88
|
|
|
$type = $request->query->get('type'); |
|
89
|
|
|
switch ($type) { |
|
90
|
|
|
case 'metadata': |
|
91
|
|
|
$path = $request->attributes->get('metadata_path'); |
|
92
|
|
|
break; |
|
93
|
|
|
case 'logout': |
|
94
|
|
|
$path = $request->attributes->get('logout_path'); |
|
95
|
|
|
break; |
|
96
|
|
|
default: |
|
97
|
|
|
$path = $request->attributes->get('login_path'); |
|
98
|
|
|
} |
|
99
|
|
|
$path = $this->httpUtils->generateUri($request, $path); |
|
100
|
|
|
return $path; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.