1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\OAuth2\Storage\NDB; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\OAuth2\Storage\Clients\Client; |
7
|
|
|
use kalanis\OAuth2\Storage\Clients\IClient; |
8
|
|
|
use kalanis\OAuth2\Storage\Clients\IClientStorage; |
9
|
|
|
use Nette\Database\Explorer; |
10
|
|
|
use Nette\Database\Table\ActiveRow; |
11
|
|
|
use Nette\Database\Table\Selection; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Nette database client storage |
16
|
|
|
* @package kalanis\OAuth2\Storage\NDB |
17
|
|
|
*/ |
18
|
1 |
|
class ClientStorage implements IClientStorage |
19
|
|
|
{ |
20
|
|
|
|
21
|
1 |
|
public function __construct( |
22
|
|
|
private readonly Explorer $context, |
23
|
|
|
) |
24
|
|
|
{ |
25
|
1 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Find client by ID and/or secret key |
29
|
|
|
* @param string|int $clientId |
30
|
|
|
* @param string|null $clientSecret |
31
|
|
|
* @return IClient|null |
32
|
|
|
*/ |
33
|
|
|
public function getClient(string|int $clientId, #[\SensitiveParameter] string|null $clientSecret = null): ?IClient |
34
|
|
|
{ |
35
|
1 |
|
if (!$clientId) { |
36
|
|
|
return null; |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
$selection = $this->getTable()->where(['client_id' => $clientId]); |
40
|
1 |
|
if ($clientSecret) { |
41
|
1 |
|
$selection->where(['secret' => $clientSecret]); |
42
|
|
|
} |
43
|
1 |
|
$data = $selection->fetch(); |
44
|
1 |
|
if (!$data) { |
45
|
|
|
return null; |
46
|
|
|
} |
47
|
1 |
|
return new Client( |
48
|
1 |
|
is_numeric($data['client_id']) ? intval($data['client_id']) : strval($data['client_id']), |
49
|
1 |
|
strval($data['secret']), |
50
|
1 |
|
strval($data['redirect_url']), |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get client table selection |
56
|
|
|
* @return Selection<ActiveRow> |
57
|
|
|
*/ |
58
|
|
|
protected function getTable(): Selection |
59
|
|
|
{ |
60
|
1 |
|
return $this->context->table('oauth_client'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Can client use given grant type |
65
|
|
|
* @param string|int $clientId |
66
|
|
|
* @param string $grantType |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function canUseGrantType(string|int $clientId, string $grantType): bool |
70
|
|
|
{ |
71
|
1 |
|
$result = $this->context->query(' |
72
|
|
|
SELECT g.name |
73
|
|
|
FROM oauth_client_grant AS cg |
74
|
|
|
RIGHT JOIN oauth_grant AS g ON g.grant_id = cg.grant_id AND g.name = ? |
75
|
|
|
WHERE cg.client_id = ? |
76
|
|
|
', $grantType, $clientId); |
77
|
1 |
|
return !empty($result->fetch()); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|