|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2Oauth2Server\components\repositories; |
|
4
|
|
|
|
|
5
|
|
|
use rhertogh\Yii2Oauth2Server\components\repositories\base\Oauth2BaseRepository; |
|
6
|
|
|
use rhertogh\Yii2Oauth2Server\components\repositories\traits\Oauth2ModelRepositoryTrait; |
|
7
|
|
|
use rhertogh\Yii2Oauth2Server\helpers\DiHelper; |
|
8
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\repositories\Oauth2ClientRepositoryInterface; |
|
9
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface; |
|
10
|
|
|
use yii\base\InvalidConfigException; |
|
11
|
|
|
|
|
12
|
|
|
class Oauth2ClientRepository extends Oauth2BaseRepository implements Oauth2ClientRepositoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use Oauth2ModelRepositoryTrait { |
|
15
|
|
|
findModelByPk as traitFindModelByPk; |
|
16
|
|
|
findModelByIdentifier as traitFindModelByIdentifier; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritDoc |
|
21
|
|
|
* @return class-string<Oauth2ClientInterface> |
|
|
|
|
|
|
22
|
|
|
*/ |
|
23
|
12 |
|
public function getModelClass() |
|
24
|
|
|
{ |
|
25
|
12 |
|
return Oauth2ClientInterface::class; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function findModelByPk($pk) |
|
29
|
|
|
{ |
|
30
|
|
|
/** @var Oauth2ClientInterface $client */ |
|
31
|
|
|
$client = $this->traitFindModelByPk($pk); |
|
32
|
|
|
if ($client) { |
|
|
|
|
|
|
33
|
|
|
$client->setModule($this->_module); |
|
34
|
|
|
} |
|
35
|
|
|
return $client; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
12 |
|
public function findModelByIdentifier($identifier) |
|
39
|
|
|
{ |
|
40
|
|
|
/** @var Oauth2ClientInterface $client */ |
|
41
|
12 |
|
$client = $this->traitFindModelByIdentifier($identifier); |
|
42
|
12 |
|
if ($client) { |
|
|
|
|
|
|
43
|
10 |
|
$client->setModule($this->_module); |
|
44
|
|
|
} |
|
45
|
12 |
|
return $client; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @inheritDoc |
|
50
|
|
|
* @throws InvalidConfigException |
|
51
|
|
|
*/ |
|
52
|
5 |
|
public function getClientEntity($clientIdentifier) |
|
53
|
|
|
{ |
|
54
|
5 |
|
return $this->findModelByIdentifier($clientIdentifier); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @inheritDoc |
|
59
|
|
|
*/ |
|
60
|
7 |
|
public function validateClient($clientIdentifier, $clientSecret, $grantType) |
|
61
|
|
|
{ |
|
62
|
|
|
/** @var Oauth2ClientInterface $client */ |
|
63
|
7 |
|
$client = $this->findModelByIdentifier($clientIdentifier); |
|
64
|
|
|
|
|
65
|
|
|
if ( |
|
66
|
7 |
|
$client |
|
67
|
7 |
|
&& $client->isEnabled() |
|
68
|
7 |
|
&& $client->validateGrantType($grantType) |
|
69
|
|
|
&& ( |
|
70
|
7 |
|
!$client->isConfidential() |
|
71
|
7 |
|
|| $client->validateSecret($clientSecret, $this->_module->getCryptographer()) |
|
|
|
|
|
|
72
|
|
|
) |
|
73
|
|
|
) { |
|
74
|
3 |
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
4 |
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getAllClients($filter = []) |
|
81
|
|
|
{ |
|
82
|
|
|
$class = $this->getModelClass(); |
|
83
|
|
|
/** @var class-string<Oauth2ClientInterface> $className */ |
|
84
|
|
|
$className = DiHelper::getValidatedClassName($class); |
|
85
|
|
|
|
|
86
|
|
|
return array_map( |
|
87
|
|
|
fn(Oauth2ClientInterface $client) => $client->setModule($this->_module), |
|
88
|
|
|
$className::find() |
|
89
|
|
|
->andFilterWhere($filter) |
|
90
|
|
|
->all(), |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|