1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Oauth2\Repository; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Oauth2\Entity\AccessToken as Entity; |
8
|
|
|
use AbterPhp\Admin\Oauth2\Entity\Scope; |
|
|
|
|
9
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; |
|
|
|
|
10
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
|
|
|
|
11
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; |
|
|
|
|
12
|
|
|
use Opulence\Databases\ConnectionPools\ConnectionPool; |
13
|
|
|
use Opulence\Orm\Ids\Generators\UuidV4Generator; |
14
|
|
|
use Opulence\QueryBuilders\MySql\QueryBuilder; |
15
|
|
|
|
16
|
|
|
class AccessToken implements AccessTokenRepositoryInterface |
17
|
|
|
{ |
18
|
|
|
/** @var UuidV4Generator */ |
19
|
|
|
protected $uuidGenerator; |
20
|
|
|
|
21
|
|
|
/** @var ConnectionPool */ |
22
|
|
|
protected $connectionPool; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* AccessToken constructor. |
26
|
|
|
* |
27
|
|
|
* @param UuidV4Generator $uuidGenerator |
28
|
|
|
* @param ConnectionPool $connectionPool |
29
|
|
|
*/ |
30
|
|
|
public function __construct(UuidV4Generator $uuidGenerator, ConnectionPool $connectionPool) |
31
|
|
|
{ |
32
|
|
|
$this->uuidGenerator = $uuidGenerator; |
33
|
|
|
$this->connectionPool = $connectionPool; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
38
|
|
|
* |
39
|
|
|
* @param ClientEntityInterface $clientEntity |
40
|
|
|
* @param array $scopes |
41
|
|
|
* @param null $userIdentifier |
|
|
|
|
42
|
|
|
* |
43
|
|
|
* @return AccessTokenEntityInterface |
44
|
|
|
*/ |
45
|
|
|
public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null) |
46
|
|
|
{ |
47
|
|
|
$accessToken = new Entity(); |
48
|
|
|
|
49
|
|
|
return $accessToken; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param AccessTokenEntityInterface $accessTokenEntity |
54
|
|
|
*/ |
55
|
|
|
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity) |
56
|
|
|
{ |
57
|
|
|
$this->persistToken($accessTokenEntity); |
58
|
|
|
|
59
|
|
|
foreach ($accessTokenEntity->getScopes() as $scope) { |
60
|
|
|
$this->persistTokenScope($accessTokenEntity, $scope); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param AccessTokenEntityInterface $accessTokenEntity |
66
|
|
|
*/ |
67
|
|
|
protected function persistToken(AccessTokenEntityInterface $accessTokenEntity) |
68
|
|
|
{ |
69
|
|
|
$data = [ |
70
|
|
|
'id' => $accessTokenEntity->getIdentifier(), |
71
|
|
|
'api_client_id' => $accessTokenEntity->getClient()->getName(), |
72
|
|
|
'expires_at' => $accessTokenEntity->getExpiryDateTime()->format('Y-m-d H:i:s'), |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
$query = (new QueryBuilder())->insert('tokens', $data); |
76
|
|
|
|
77
|
|
|
$sql = $query->getSql(); |
78
|
|
|
$params = array_values($data); |
79
|
|
|
|
80
|
|
|
$connection = $this->connectionPool->getWriteConnection(); |
81
|
|
|
$statement = $connection->prepare($sql); |
82
|
|
|
$statement->bindValues($params); |
83
|
|
|
$statement->execute(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param AccessTokenEntityInterface $accessTokenEntity |
88
|
|
|
* @param Scope $scope |
89
|
|
|
*/ |
90
|
|
|
protected function persistTokenScope(AccessTokenEntityInterface $accessTokenEntity, Scope $scope) |
91
|
|
|
{ |
92
|
|
|
$data = [ |
93
|
|
|
'id' => $this->uuidGenerator->generate(null), |
94
|
|
|
'token_id' => $accessTokenEntity->getIdentifier(), |
95
|
|
|
'admin_resource_id' => $scope->getIdentifier(), |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
$query = (new QueryBuilder())->insert('tokens_admin_resources', $data); |
99
|
|
|
|
100
|
|
|
$sql = $query->getSql(); |
101
|
|
|
$params = array_values($data); |
102
|
|
|
|
103
|
|
|
$connection = $this->connectionPool->getWriteConnection(); |
104
|
|
|
$statement = $connection->prepare($sql); |
105
|
|
|
$statement->bindValues($params); |
106
|
|
|
$statement->execute(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
111
|
|
|
* |
112
|
|
|
* @param string $tokenId |
113
|
|
|
*/ |
114
|
|
|
public function revokeAccessToken($tokenId) |
115
|
|
|
{ |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
120
|
|
|
* |
121
|
|
|
* @param string $tokenId |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function isAccessTokenRevoked($tokenId) |
126
|
|
|
{ |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: