|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine PHP |
|
5
|
|
|
* |
|
6
|
|
|
* Platine PHP is a lightweight, high-performance, simple and elegant |
|
7
|
|
|
* PHP Web framework |
|
8
|
|
|
* |
|
9
|
|
|
* This content is released under the MIT License (MIT) |
|
10
|
|
|
* |
|
11
|
|
|
* Copyright (c) 2020 Platine PHP |
|
12
|
|
|
* |
|
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
15
|
|
|
* in the Software without restriction, including without limitation the rights |
|
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
18
|
|
|
* furnished to do so, subject to the following conditions: |
|
19
|
|
|
* |
|
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
21
|
|
|
* copies or substantial portions of the Software. |
|
22
|
|
|
* |
|
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
29
|
|
|
* SOFTWARE. |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @file AccessTokenRepository.php |
|
34
|
|
|
* |
|
35
|
|
|
* The Access Token Repository class |
|
36
|
|
|
* |
|
37
|
|
|
* @package Platine\Framework\OAuth2\Repository |
|
38
|
|
|
* @author Platine Developers team |
|
39
|
|
|
* @copyright Copyright (c) 2020 |
|
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
41
|
|
|
* @link http://www.iacademy.cf |
|
42
|
|
|
* @version 1.0.0 |
|
43
|
|
|
* @filesource |
|
44
|
|
|
*/ |
|
45
|
|
|
|
|
46
|
|
|
declare(strict_types=1); |
|
47
|
|
|
|
|
48
|
|
|
namespace Platine\Framework\OAuth2\Repository; |
|
49
|
|
|
|
|
50
|
|
|
use Platine\Framework\OAuth2\Entity\OauthAccessToken; |
|
51
|
|
|
use Platine\Framework\OAuth2\User\TokenOwner; |
|
52
|
|
|
use Platine\OAuth2\Entity\AccessToken; |
|
53
|
|
|
use Platine\OAuth2\Entity\BaseToken; |
|
54
|
|
|
use Platine\OAuth2\Repository\AccessTokenRepositoryInterface; |
|
55
|
|
|
use Platine\OAuth2\Service\ClientService; |
|
56
|
|
|
use Platine\Orm\EntityManager; |
|
57
|
|
|
use Platine\Orm\Repository; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @class AccessTokenRepository |
|
61
|
|
|
* @package Platine\Framework\OAuth2\Repository |
|
62
|
|
|
*/ |
|
63
|
|
|
class AccessTokenRepository extends Repository implements AccessTokenRepositoryInterface |
|
64
|
|
|
{ |
|
65
|
|
|
/** |
|
66
|
|
|
* The Client Service |
|
67
|
|
|
* @var ClientService |
|
68
|
|
|
*/ |
|
69
|
|
|
protected ClientService $clientService; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Create new instance |
|
73
|
|
|
* @param EntityManager $manager |
|
74
|
|
|
* @param ClientService $clientService |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct(EntityManager $manager, ClientService $clientService) |
|
77
|
|
|
{ |
|
78
|
|
|
parent::__construct($manager, OauthAccessToken::class); |
|
79
|
|
|
$this->clientService = $clientService; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function cleanExpiredTokens(): void |
|
86
|
|
|
{ |
|
87
|
|
|
$this->query()->where('expires')->lte(date('Y-m-d H:i:s')) |
|
88
|
|
|
->delete(); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function deleteToken(BaseToken $token): bool |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->query()->where('access_token')->is($token->getToken()) |
|
97
|
|
|
->delete() >= 0; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getByToken(string $token): ?BaseToken |
|
104
|
|
|
{ |
|
105
|
|
|
$accessToken = $this->find($token); |
|
106
|
|
|
if ($accessToken === null) { |
|
107
|
|
|
return null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$client = null; |
|
111
|
|
|
if ($accessToken->client_id !== null) { |
|
112
|
|
|
$client = $this->clientService->find($accessToken->client_id); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return AccessToken::hydrate([ |
|
116
|
|
|
'token' => $accessToken->access_token, |
|
117
|
|
|
'owner' => new TokenOwner($accessToken->user_id), |
|
118
|
|
|
'client' => $client, |
|
119
|
|
|
'expires_at' => $accessToken->expires, |
|
120
|
|
|
'scopes' => explode(' ', $accessToken->scope), |
|
121
|
|
|
]); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
*/ |
|
127
|
|
|
public function isTokenExists(string $token): bool |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->find($token) !== null; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
|
|
public function saveAccessToken(AccessToken $token): AccessToken |
|
136
|
|
|
{ |
|
137
|
|
|
$clientId = null; |
|
138
|
|
|
if ($token->getClient() !== null) { |
|
139
|
|
|
$clientId = $token->getClient()->getId(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$ownerId = null; |
|
143
|
|
|
if ($token->getOwner() !== null) { |
|
144
|
|
|
$ownerId = $token->getOwner()->getOwnerId(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$accessToken = $this->create([ |
|
148
|
|
|
'access_token' => $token->getToken(), |
|
149
|
|
|
'client_id' => $clientId, |
|
150
|
|
|
'user_id' => $ownerId, |
|
151
|
|
|
'expires' => $token->getExpireAt(), |
|
152
|
|
|
'scope' => implode(' ', $token->getScopes()), |
|
153
|
|
|
]); |
|
154
|
|
|
|
|
155
|
|
|
$this->save($accessToken); |
|
156
|
|
|
|
|
157
|
|
|
return $token; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|