|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\SamlServiceProvider; |
|
6
|
|
|
use App\Entity\ServiceProvider; |
|
7
|
|
|
use App\Form\ServiceProviderType; |
|
8
|
|
|
use App\Repository\ServiceProviderRepositoryInterface; |
|
9
|
|
|
use App\Service\ServiceProviderTokenGenerator; |
|
10
|
|
|
use Exception; |
|
11
|
|
|
use LightSaml\Model\Metadata\EntityDescriptor; |
|
12
|
|
|
use SchulIT\CommonBundle\Form\ConfirmType; |
|
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Exception\BadRequestException; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
18
|
|
|
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
|
19
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
|
20
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @Route("/admin/service_providers") |
|
24
|
|
|
*/ |
|
25
|
|
|
class ServiceProviderController extends AbstractController { |
|
26
|
|
|
|
|
27
|
|
|
private $repository; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(ServiceProviderRepositoryInterface $repository) { |
|
30
|
|
|
$this->repository = $repository; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @Route("", name="service_providers") |
|
35
|
|
|
*/ |
|
36
|
|
|
public function index() { |
|
37
|
|
|
$serviceProviders = $this->repository |
|
38
|
|
|
->findAll(); |
|
39
|
|
|
|
|
40
|
|
|
return $this->render('service_providers/index.html.twig', [ |
|
41
|
|
|
'service_providers' => $serviceProviders |
|
42
|
|
|
]); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @Route("/{uuid}/certificate", name="service_provider_certificate") |
|
47
|
|
|
*/ |
|
48
|
|
|
public function certificateInfo(ServiceProvider $serviceProvider) { |
|
49
|
|
|
if(!$serviceProvider instanceof SamlServiceProvider) { |
|
50
|
|
|
return $this->redirectToRoute('service_providers'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$cert = openssl_x509_read($serviceProvider->getCertificate()); |
|
54
|
|
|
$certificateInfo = openssl_x509_parse($cert); |
|
55
|
|
|
openssl_x509_free($cert); |
|
56
|
|
|
|
|
57
|
|
|
return $this->render('service_providers/info.html.twig', [ |
|
58
|
|
|
'service_provider' => $serviceProvider, |
|
59
|
|
|
'certificate' => $certificateInfo |
|
60
|
|
|
]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @Route("/add", name="add_service_provider") |
|
65
|
|
|
*/ |
|
66
|
|
|
public function add(Request $request) { |
|
67
|
|
|
if($request->query->get('type', 'default') === 'saml') { |
|
68
|
|
|
$serviceProvider = new SamlServiceProvider(); |
|
69
|
|
|
} else { |
|
70
|
|
|
$serviceProvider = new ServiceProvider(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$form = $this->createForm(ServiceProviderType::class, $serviceProvider); |
|
74
|
|
|
$form->handleRequest($request); |
|
75
|
|
|
|
|
76
|
|
|
if($form->isSubmitted() && $form->isValid()) { |
|
77
|
|
|
$this->repository->persist($serviceProvider); |
|
78
|
|
|
|
|
79
|
|
|
$this->addFlash('success', 'service_providers.add.success'); |
|
80
|
|
|
return $this->redirectToRoute('service_providers'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->render('service_providers/add.html.twig', [ |
|
84
|
|
|
'form' => $form->createView(), |
|
85
|
|
|
'type' => get_class($serviceProvider) |
|
86
|
|
|
]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @Route("/{uuid}/edit", name="edit_service_provider") |
|
91
|
|
|
*/ |
|
92
|
|
|
public function edit(Request $request, ServiceProvider $serviceProvider) { |
|
93
|
|
|
$form = $this->createForm(ServiceProviderType::class, $serviceProvider); |
|
94
|
|
|
$form->handleRequest($request); |
|
95
|
|
|
|
|
96
|
|
|
if($form->isSubmitted() && $form->isValid()) { |
|
97
|
|
|
$this->repository->persist($serviceProvider); |
|
98
|
|
|
|
|
99
|
|
|
$this->addFlash('success', 'service_providers.edit.success'); |
|
100
|
|
|
return $this->redirectToRoute('service_providers'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this->render('service_providers/edit.html.twig', [ |
|
104
|
|
|
'form' => $form->createView(), |
|
105
|
|
|
'service_provider' => $serviceProvider |
|
106
|
|
|
]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @Route("/{uuid}/remove", name="remove_service_provider") |
|
111
|
|
|
*/ |
|
112
|
|
|
public function remove(ServiceProvider $serviceProvider, Request $request, TranslatorInterface $translator) { |
|
113
|
|
|
$form = $this->createForm(ConfirmType::class, [], [ |
|
114
|
|
|
'message' => $translator->trans('service_providers.remove.confirm', [ |
|
115
|
|
|
'%name%' => $serviceProvider->getName() |
|
116
|
|
|
]), |
|
117
|
|
|
'label' => 'service_providers.remove.label' |
|
118
|
|
|
]); |
|
119
|
|
|
$form->handleRequest($request); |
|
120
|
|
|
|
|
121
|
|
|
if($form->isSubmitted() && $form->isValid()) { |
|
122
|
|
|
$this->repository->remove($serviceProvider); |
|
123
|
|
|
|
|
124
|
|
|
$this->addFlash('success', 'service_providers.remove.success'); |
|
125
|
|
|
return $this->redirectToRoute('service_providers'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $this->render('service_providers/remove.html.twig', [ |
|
129
|
|
|
'form' => $form->createView(), |
|
130
|
|
|
'service_provider' => $serviceProvider |
|
131
|
|
|
]); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @Route("/metadata", name="load_xml_metadata") |
|
136
|
|
|
* @throws TransportExceptionInterface |
|
137
|
|
|
* @throws Exception |
|
138
|
|
|
*/ |
|
139
|
|
|
public function loadXml(Request $request, HttpClientInterface $httpClient) { |
|
140
|
|
|
$url = $request->query->get('url'); |
|
141
|
|
|
|
|
142
|
|
|
if(empty($url)) { |
|
143
|
|
|
throw new BadRequestException(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$response = $httpClient->request('GET', $url); |
|
147
|
|
|
if($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) { |
|
148
|
|
|
throw new Exception(sprintf('Request was not successful. Got status %d', $response->getStatusCode())); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$xml = $response->getContent(); |
|
152
|
|
|
$descriptor = EntityDescriptor::loadXml($xml); |
|
153
|
|
|
$certificate = null; |
|
154
|
|
|
$acs = null; |
|
155
|
|
|
|
|
156
|
|
|
foreach($descriptor->getAllSpKeyDescriptors() as $spKeyDescriptor) { |
|
157
|
|
|
if($spKeyDescriptor->getUse() === 'encryption') { |
|
158
|
|
|
$certificate = $spKeyDescriptor->getCertificate()->toPem(); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$sspSsoDescriptor = $descriptor->getFirstSpSsoDescriptor(); |
|
163
|
|
|
|
|
164
|
|
|
if($sspSsoDescriptor !== null && $sspSsoDescriptor->getFirstAssertionConsumerService() !== null) { |
|
165
|
|
|
$acs = $sspSsoDescriptor->getFirstAssertionConsumerService()->getLocation(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return new JsonResponse([ |
|
169
|
|
|
'entity_id' => $descriptor->getEntityID(), |
|
170
|
|
|
'certificate' => $certificate, |
|
171
|
|
|
'acs' => $acs |
|
172
|
|
|
]); |
|
173
|
|
|
} |
|
174
|
|
|
} |