1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Oauth2\Repository; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Exception\Database; |
8
|
|
|
use AbterPhp\Admin\Oauth2\Entity\Client as Entity; |
9
|
|
|
use AbterPhp\Framework\Crypto\Crypto; |
10
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
11
|
|
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface; |
12
|
|
|
use Opulence\Databases\ConnectionPools\ConnectionPool; |
13
|
|
|
use Opulence\QueryBuilders\MySql\QueryBuilder; |
14
|
|
|
|
15
|
|
|
/** @phan-file-suppress PhanTypeMismatchArgument */ |
16
|
|
|
class Client implements ClientRepositoryInterface |
17
|
|
|
{ |
18
|
|
|
protected Crypto $crypto; |
19
|
|
|
|
20
|
|
|
protected ConnectionPool $connectionPool; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Client constructor. |
24
|
|
|
* |
25
|
|
|
* @param Crypto $crypto |
26
|
|
|
* @param ConnectionPool $connectionPool |
27
|
|
|
*/ |
28
|
|
|
public function __construct(Crypto $crypto, ConnectionPool $connectionPool) |
29
|
|
|
{ |
30
|
|
|
$this->crypto = $crypto; |
31
|
|
|
$this->connectionPool = $connectionPool; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
36
|
|
|
* |
37
|
|
|
* @param string $clientIdentifier |
38
|
|
|
* @param string|null $grantType |
39
|
|
|
* @param string|null $clientSecret |
40
|
|
|
* @param bool $mustValidateSecret |
41
|
|
|
* |
42
|
|
|
* @return ClientEntityInterface|null |
43
|
|
|
*/ |
44
|
|
|
public function getClientEntity( |
45
|
|
|
$clientIdentifier, |
46
|
|
|
?string $grantType = null, |
47
|
|
|
?string $clientSecret = null, |
48
|
|
|
bool $mustValidateSecret = true |
49
|
|
|
) { |
50
|
|
|
$clientData = $this->query($clientIdentifier); |
51
|
|
|
|
52
|
|
|
if (empty($clientData['secret'])) { |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($mustValidateSecret && empty($clientSecret)) { |
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($clientSecret) { |
61
|
|
|
$clientSecret = $this->crypto->prepareSecret($clientSecret); |
62
|
|
|
if (!$this->crypto->verifySecret($clientSecret, $clientData['secret'])) { |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return new Entity($clientIdentifier, $clientIdentifier, ''); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $clientId |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
protected function query(string $clientId): array |
76
|
|
|
{ |
77
|
|
|
$query = (new QueryBuilder()) |
78
|
|
|
->select('ac.secret') |
79
|
|
|
->from('api_clients', 'ac') |
80
|
|
|
->where('ac.deleted_at IS NULL') |
81
|
|
|
->andWhere('ac.id = :clientId'); |
82
|
|
|
|
83
|
|
|
$sql = $query->getSql(); |
84
|
|
|
$params = ['clientId' => [$clientId, \PDO::PARAM_STR]]; |
85
|
|
|
|
86
|
|
|
$connection = $this->connectionPool->getReadConnection(); |
87
|
|
|
$statement = $connection->prepare($sql); |
88
|
|
|
$statement->bindValues($params); |
89
|
|
|
if (!$statement->execute()) { |
90
|
|
|
throw new Database($statement->errorInfo()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $statement->fetch(\PDO::FETCH_ASSOC); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
98
|
|
|
* |
99
|
|
|
* Validate a client's secret. |
100
|
|
|
* |
101
|
|
|
* @param string $clientIdentifier The client's identifier |
102
|
|
|
* @param null|string $clientSecret The client's secret (if sent) |
103
|
|
|
* @param null|string $grantType The type of grant the client is using (if sent) |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function validateClient($clientIdentifier, $clientSecret, $grantType) |
108
|
|
|
{ |
109
|
|
|
// TODO: Improve |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|