|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Lookyman\NetteOAuth2Server\Storage\Doctrine\Client; |
|
5
|
|
|
|
|
6
|
|
|
use Kdyby\Doctrine\EntityManager; |
|
7
|
|
|
use Kdyby\Doctrine\EntityRepository; |
|
8
|
|
|
use Kdyby\Doctrine\QueryObject; |
|
9
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
|
10
|
|
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface; |
|
11
|
|
|
|
|
12
|
|
|
class ClientRepository implements ClientRepositoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var EntityManager |
|
16
|
|
|
*/ |
|
17
|
|
|
private $entityManager; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var EntityRepository |
|
21
|
|
|
*/ |
|
22
|
|
|
private $repository; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var callable |
|
26
|
|
|
*/ |
|
27
|
|
|
private $secretValidator; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param EntityManager $entityManager |
|
31
|
|
|
* @param callable|null $secretValidator |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(EntityManager $entityManager, callable $secretValidator = null) |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$this->entityManager = $entityManager; |
|
36
|
|
|
$this->repository = $entityManager->getRepository(ClientEntity::class); |
|
37
|
|
|
$this->secretValidator = $secretValidator ?: function ($expected, $actual) { return hash_equals($expected, $actual); }; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $clientIdentifier |
|
42
|
|
|
* @param string $grantType |
|
43
|
|
|
* @param string|null $clientSecret |
|
44
|
|
|
* @param bool $mustValidateSecret |
|
45
|
|
|
* @return ClientEntityInterface|null |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getClientEntity($clientIdentifier, $grantType, $clientSecret = null, $mustValidateSecret = true) |
|
48
|
|
|
{ |
|
49
|
|
|
/** @var ClientEntity|null $client */ |
|
50
|
|
|
$client = $this->repository->fetchOne($this->createQuery()->byIdentifier($clientIdentifier)); |
|
51
|
|
|
return $client |
|
52
|
|
|
&& $client->getSecret() |
|
|
|
|
|
|
53
|
|
|
&& $mustValidateSecret |
|
54
|
|
|
&& !call_user_func($this->secretValidator, $client->getSecret(), $clientSecret) |
|
55
|
|
|
? null |
|
56
|
|
|
: $client; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return QueryObject |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function createQuery(): QueryObject |
|
63
|
|
|
{ |
|
64
|
|
|
return new ClientQuery(); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.