|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Saml; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\ServiceProvider; |
|
6
|
|
|
use App\Repository\ServiceProviderRepositoryInterface; |
|
7
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
8
|
|
|
use LightSaml\Credential\X509Certificate; |
|
9
|
|
|
use LightSaml\Model\Metadata\AssertionConsumerService; |
|
10
|
|
|
use LightSaml\Model\Metadata\EntityDescriptor; |
|
11
|
|
|
use LightSaml\Model\Metadata\KeyDescriptor; |
|
12
|
|
|
use LightSaml\Model\Metadata\SpSsoDescriptor; |
|
13
|
|
|
use LightSaml\SamlConstants; |
|
14
|
|
|
use LightSaml\Store\EntityDescriptor\EntityDescriptorStoreInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Helper which is used by LightSAML to retrieve all valid ServiceProviders. These ServiceProviders are loaded from the |
|
18
|
|
|
* database. |
|
19
|
|
|
*/ |
|
20
|
|
|
class ServiceProviderEntityStore implements EntityDescriptorStoreInterface { |
|
21
|
|
|
|
|
22
|
|
|
private $repository; |
|
23
|
|
|
|
|
24
|
1 |
|
public function __construct(ServiceProviderRepositoryInterface $repository) { |
|
25
|
1 |
|
$this->repository = $repository; |
|
26
|
1 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritDoc |
|
30
|
|
|
*/ |
|
31
|
|
|
public function get($entityId) { |
|
32
|
|
|
$provider = $this->repository |
|
33
|
|
|
->findOneByEntityId($entityId); |
|
34
|
|
|
|
|
35
|
|
|
if($provider === null) { |
|
36
|
|
|
return null; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $this->getEntityDescriptor($provider); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritDoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function has($entityId) { |
|
46
|
|
|
return $this->get($entityId) !== null; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritDoc |
|
51
|
|
|
*/ |
|
52
|
|
|
public function all() { |
|
53
|
|
|
/** @var EntityDescriptor[] $all */ |
|
54
|
|
|
$all = [ ]; |
|
55
|
|
|
|
|
56
|
|
|
$serviceProviders = $this->repository |
|
57
|
|
|
->findAll(); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($serviceProviders as $serviceProvider) { |
|
60
|
|
|
$all[] = $this->getEntityDescriptor($serviceProvider); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $all; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Converts a ServiceProvider entity into an entity descriptor for further use within the LightSAML library |
|
68
|
|
|
* |
|
69
|
|
|
* @param ServiceProvider $serviceProvider |
|
70
|
|
|
* @return EntityDescriptor |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getEntityDescriptor(ServiceProvider $serviceProvider) { |
|
73
|
|
|
$entityDescriptor = new EntityDescriptor($serviceProvider->getEntityId()); |
|
74
|
|
|
$spDescriptor = new SpSsoDescriptor(); |
|
75
|
|
|
|
|
76
|
|
|
$spDescriptor->addKeyDescriptor($this->getKeyDescriptor($serviceProvider, KeyDescriptor::USE_SIGNING)); |
|
77
|
|
|
$spDescriptor->addKeyDescriptor($this->getKeyDescriptor($serviceProvider, KeyDescriptor::USE_ENCRYPTION)); |
|
78
|
|
|
|
|
79
|
|
|
$consumerService = new AssertionConsumerService($serviceProvider->getAcs()); |
|
80
|
|
|
$consumerService->setBinding(SamlConstants::BINDING_SAML2_HTTP_POST); |
|
81
|
|
|
$spDescriptor->addAssertionConsumerService($consumerService); |
|
82
|
|
|
|
|
83
|
|
|
$consumerService = new AssertionConsumerService($serviceProvider->getAcs()); |
|
84
|
|
|
$consumerService->setBinding(SamlConstants::BINDING_SAML2_HTTP_REDIRECT); |
|
85
|
|
|
$spDescriptor->addAssertionConsumerService($consumerService); |
|
86
|
|
|
|
|
87
|
|
|
$entityDescriptor->addItem($spDescriptor); |
|
88
|
|
|
return $entityDescriptor; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param ServiceProvider $serviceProvider |
|
93
|
|
|
* @param string $use |
|
94
|
|
|
* @return KeyDescriptor |
|
95
|
|
|
*/ |
|
96
|
|
|
private function getKeyDescriptor(ServiceProvider $serviceProvider, $use) { |
|
97
|
|
|
$keyDescriptor = new KeyDescriptor(); |
|
98
|
|
|
$keyDescriptor->setUse($use); |
|
99
|
|
|
$certificate = new X509Certificate(); |
|
100
|
|
|
$certificate->loadPem($serviceProvider->getCertificate()); |
|
101
|
|
|
$keyDescriptor->setCertificate($certificate); |
|
102
|
|
|
|
|
103
|
|
|
return $keyDescriptor; |
|
104
|
|
|
} |
|
105
|
|
|
} |