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