|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Lookyman\NetteOAuth2Server\Storage\Doctrine\Client; |
|
5
|
|
|
|
|
6
|
|
|
use Kdyby\Doctrine\InvalidStateException; |
|
7
|
|
|
use Kdyby\Doctrine\QueryException; |
|
8
|
|
|
use Kdyby\Doctrine\Registry; |
|
9
|
|
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface; |
|
10
|
|
|
|
|
11
|
|
|
class ClientRepository implements ClientRepositoryInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Registry |
|
15
|
|
|
*/ |
|
16
|
|
|
private $registry; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var callable |
|
20
|
|
|
*/ |
|
21
|
|
|
private $secretValidator; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param Registry $registry |
|
25
|
|
|
* @param callable|null $secretValidator |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(Registry $registry, callable $secretValidator = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->registry = $registry; |
|
30
|
|
|
$this->secretValidator = $secretValidator ?: function ($expected, $actual) { return hash_equals($expected, $actual); }; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $clientIdentifier |
|
35
|
|
|
* @param string $grantType |
|
36
|
|
|
* @param string|null $clientSecret |
|
37
|
|
|
* @param bool $mustValidateSecret |
|
38
|
|
|
* @return ClientEntity|null |
|
39
|
|
|
* @throws InvalidStateException |
|
40
|
|
|
* @throws QueryException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getClientEntity($clientIdentifier, $grantType, $clientSecret = null, $mustValidateSecret = true) |
|
43
|
|
|
{ |
|
44
|
|
|
/** @var ClientEntity|null $clientEntity */ |
|
45
|
|
|
$clientEntity = $this->registry->getManager()->getRepository(ClientEntity::class)->fetchOne($this->createQuery()->byIdentifier($clientIdentifier)); |
|
46
|
|
|
return $clientEntity |
|
47
|
|
|
&& $mustValidateSecret |
|
48
|
|
|
&& $clientEntity->getSecret() !== null |
|
49
|
|
|
&& !call_user_func($this->secretValidator, $clientEntity->getSecret(), $clientSecret) |
|
50
|
|
|
? null |
|
51
|
|
|
: $clientEntity; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return ClientQuery |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function createQuery(): ClientQuery |
|
58
|
|
|
{ |
|
59
|
|
|
return new ClientQuery(); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|