|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\OpenIDBundle\Manager; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
|
14
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
15
|
|
|
use LoginCidadao\CoreBundle\Entity\PersonRepository; |
|
16
|
|
|
use LoginCidadao\CoreBundle\Event\GetClientEvent; |
|
17
|
|
|
use LoginCidadao\CoreBundle\Event\LoginCidadaoCoreEvents; |
|
18
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
19
|
|
|
use LoginCidadao\OAuthBundle\Model\ClientInterface; |
|
20
|
|
|
use LoginCidadao\OpenIDBundle\Entity\ClientMetadata; |
|
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
22
|
|
|
|
|
23
|
|
|
class ClientManager |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var EventDispatcherInterface */ |
|
26
|
|
|
private $dispatcher; |
|
27
|
|
|
|
|
28
|
|
|
/** @var EntityManagerInterface */ |
|
29
|
|
|
private $em; |
|
30
|
|
|
|
|
31
|
|
|
/** @var PersonRepository */ |
|
32
|
|
|
private $personRepository; |
|
33
|
|
|
|
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
private $publicScopes; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* ClientManager constructor. |
|
39
|
|
|
* @param EntityManagerInterface $em |
|
40
|
|
|
* @param EventDispatcherInterface $dispatcher |
|
41
|
|
|
* @param PersonRepository $personRepository |
|
42
|
|
|
* @param $publicScopes |
|
43
|
|
|
*/ |
|
44
|
7 |
|
public function __construct( |
|
45
|
|
|
EntityManagerInterface $em, |
|
46
|
|
|
EventDispatcherInterface $dispatcher, |
|
47
|
|
|
PersonRepository $personRepository, |
|
48
|
|
|
$publicScopes |
|
49
|
|
|
) { |
|
50
|
7 |
|
$this->em = $em; |
|
51
|
7 |
|
$this->dispatcher = $dispatcher; |
|
52
|
7 |
|
$this->personRepository = $personRepository; |
|
53
|
7 |
|
$this->publicScopes = $publicScopes; |
|
54
|
7 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param mixed $id |
|
58
|
|
|
* @return ClientInterface|null |
|
59
|
|
|
*/ |
|
60
|
3 |
|
public function getClientById($id) |
|
61
|
|
|
{ |
|
62
|
3 |
|
if ($id === null) { |
|
63
|
1 |
|
return null; |
|
64
|
|
|
} |
|
65
|
2 |
|
$randomId = null; |
|
66
|
2 |
|
if (strstr($id, '_') !== false) { |
|
67
|
1 |
|
$parts = explode('_', $id); |
|
68
|
1 |
|
$id = $parts[0]; |
|
69
|
1 |
|
$randomId = $parts[1]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
$repo = $this->em->getRepository('LoginCidadaoOAuthBundle:Client'); |
|
73
|
|
|
|
|
74
|
2 |
|
if ($randomId) { |
|
75
|
1 |
|
$client = $repo->findOneBy([ |
|
76
|
1 |
|
'id' => $id, |
|
77
|
1 |
|
'randomId' => $randomId, |
|
78
|
|
|
]); |
|
79
|
|
|
} else { |
|
80
|
1 |
|
$client = $repo->find($id); |
|
81
|
|
|
} |
|
82
|
2 |
|
$event = new GetClientEvent($client); |
|
83
|
2 |
|
$this->dispatcher->dispatch(LoginCidadaoCoreEvents::GET_CLIENT, $event); |
|
84
|
|
|
|
|
85
|
2 |
|
return $event->getClient(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param ClientMetadata $data |
|
90
|
|
|
* @return ClientInterface |
|
91
|
|
|
* @throws UniqueConstraintViolationException |
|
92
|
|
|
*/ |
|
93
|
3 |
|
public function register(ClientMetadata $data) |
|
94
|
|
|
{ |
|
95
|
3 |
|
$client = $data->getClient(); |
|
96
|
|
|
|
|
97
|
3 |
|
$this->em->persist($client); |
|
98
|
|
|
|
|
99
|
3 |
|
$data->setClient($client); |
|
100
|
3 |
|
$this->em->persist($data); |
|
101
|
|
|
|
|
102
|
3 |
|
$this->em->flush(); |
|
103
|
|
|
|
|
104
|
3 |
|
return $client; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
3 |
|
private function sanitizeClient(ClientInterface $client) |
|
108
|
|
|
{ |
|
109
|
3 |
|
if ($client->getName() === null) { |
|
110
|
3 |
|
$firstUrl = $client->getRedirectUris() |
|
111
|
1 |
|
? parse_url($client->getRedirectUris()[0], PHP_URL_HOST) |
|
112
|
3 |
|
: 'Unamed Client'; |
|
113
|
3 |
|
$client->setName($firstUrl); |
|
114
|
|
|
} |
|
115
|
3 |
|
if ($client->getDescription() === null) { |
|
116
|
3 |
|
$client->setDescription(''); |
|
117
|
|
|
} |
|
118
|
3 |
|
if ($client->getTermsOfUseUrl() === null) { |
|
119
|
3 |
|
$client->setTermsOfUseUrl(''); |
|
120
|
|
|
} |
|
121
|
3 |
|
if ($client->getSiteUrl() === null) { |
|
122
|
3 |
|
$client->setSiteUrl(''); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
3 |
|
return $client; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
3 |
|
public function populateNewMetadata(ClientMetadata $data) |
|
129
|
|
|
{ |
|
130
|
3 |
|
$this->initializeRegistrationAccessToken($data); |
|
131
|
|
|
|
|
132
|
3 |
|
if ($data->getClient() === null) { |
|
133
|
2 |
|
$client = $data->toClient(); |
|
134
|
|
|
} else { |
|
135
|
1 |
|
$client = $data->getClient(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
3 |
|
$client = $this->sanitizeClient($client); |
|
139
|
3 |
|
if ($data->getClientName() === null) { |
|
140
|
3 |
|
$data->setClientName($client->getName()); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
3 |
|
if (count($data->getContacts()) > 0) { |
|
144
|
|
|
/** @var PersonInterface[] $owners */ |
|
145
|
1 |
|
$owners = $this->personRepository->findBy([ |
|
146
|
1 |
|
'email' => $data->getContacts(), |
|
147
|
|
|
]); |
|
148
|
|
|
|
|
149
|
1 |
|
foreach ($owners as $person) { |
|
150
|
1 |
|
if (!$person->getEmailConfirmedAt() instanceof \DateTime) { |
|
151
|
|
|
// Email is not verified. Skipping... |
|
152
|
1 |
|
continue; |
|
153
|
|
|
} |
|
154
|
1 |
|
$client->getOwners()->add($person); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
3 |
|
$publicScopes = explode(' ', $this->publicScopes); |
|
159
|
3 |
|
$client->setAllowedScopes($publicScopes); |
|
160
|
|
|
|
|
161
|
3 |
|
$data->setClient($client); |
|
162
|
|
|
|
|
163
|
3 |
|
return $data; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
3 |
|
private function initializeRegistrationAccessToken(ClientMetadata &$data) |
|
167
|
|
|
{ |
|
168
|
3 |
|
if (null === $data->getRegistrationAccessToken()) { |
|
169
|
3 |
|
$registrationAccessToken = bin2hex(random_bytes(120)); |
|
170
|
3 |
|
$data->setRegistrationAccessToken($registrationAccessToken); |
|
171
|
|
|
} |
|
172
|
3 |
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|