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 AuthorizationCodeRepository.php |
34
|
|
|
* |
35
|
|
|
* The Authorization Code 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 https://www.platine-php.com |
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\OauthAuthorizationCode; |
51
|
|
|
use Platine\Framework\OAuth2\User\TokenOwner; |
52
|
|
|
use Platine\OAuth2\Entity\AuthorizationCode; |
53
|
|
|
use Platine\OAuth2\Entity\BaseToken; |
54
|
|
|
use Platine\OAuth2\Repository\AuthorizationCodeRepositoryInterface; |
55
|
|
|
use Platine\OAuth2\Service\ClientService; |
56
|
|
|
use Platine\Orm\EntityManager; |
57
|
|
|
use Platine\Orm\Repository; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @class AuthorizationCodeRepository |
61
|
|
|
* @package Platine\Framework\OAuth2\Repository |
62
|
|
|
* @extends Repository<OauthAuthorizationCode> |
63
|
|
|
*/ |
64
|
|
|
class AuthorizationCodeRepository extends Repository implements AuthorizationCodeRepositoryInterface |
65
|
|
|
{ |
66
|
|
|
/** |
67
|
|
|
* The Client Service |
68
|
|
|
* @var ClientService |
69
|
|
|
*/ |
70
|
|
|
protected ClientService $clientService; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Create new instance |
74
|
|
|
* @param EntityManager<OauthAuthorizationCode> $manager |
75
|
|
|
* @param ClientService $clientService |
76
|
|
|
*/ |
77
|
|
|
public function __construct(EntityManager $manager, ClientService $clientService) |
78
|
|
|
{ |
79
|
|
|
parent::__construct($manager, OauthAuthorizationCode::class); |
80
|
|
|
$this->clientService = $clientService; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function cleanExpiredTokens(): void |
87
|
|
|
{ |
88
|
|
|
$this->query()->where('expires')->lte(date('Y-m-d H:i:s')) |
89
|
|
|
->delete(); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function deleteToken(BaseToken $token): bool |
96
|
|
|
{ |
97
|
|
|
return $this->query()->where('authorization_code')->is($token->getToken()) |
98
|
|
|
->delete() >= 0; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function getByToken(string $token): ?BaseToken |
105
|
|
|
{ |
106
|
|
|
$code = $this->find($token); |
107
|
|
|
if ($code === null) { |
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$client = null; |
112
|
|
|
if ($code->client_id !== null) { |
113
|
|
|
$client = $this->clientService->find($code->client_id); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return AuthorizationCode::hydrate([ |
117
|
|
|
'token' => $code->authorization_code, |
118
|
|
|
'owner' => new TokenOwner($code->user_id), |
119
|
|
|
'client' => $client, |
120
|
|
|
'expires_at' => $code->expires, |
121
|
|
|
'scopes' => explode(' ', $code->scope), |
122
|
|
|
'redirect_uri' => $code->redirect_uri, |
123
|
|
|
]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function isTokenExists(string $token): bool |
130
|
|
|
{ |
131
|
|
|
return $this->find($token) !== null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
|
public function saveCode(AuthorizationCode $token): AuthorizationCode |
138
|
|
|
{ |
139
|
|
|
$clientId = null; |
140
|
|
|
if ($token->getClient() !== null) { |
141
|
|
|
$clientId = $token->getClient()->getId(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$ownerId = null; |
145
|
|
|
if ($token->getOwner() !== null) { |
146
|
|
|
$ownerId = $token->getOwner()->getOwnerId(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$code = $this->create([ |
150
|
|
|
'authorization_code' => $token->getToken(), |
151
|
|
|
'client_id' => $clientId, |
152
|
|
|
'user_id' => $ownerId, |
153
|
|
|
'expires' => $token->getExpireAt(), |
154
|
|
|
'scope' => implode(' ', $token->getScopes()), |
155
|
|
|
'redirect_uri' => $token->getRedirectUri(), |
156
|
|
|
]); |
157
|
|
|
|
158
|
|
|
$this->save($code); |
159
|
|
|
|
160
|
|
|
return $token; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|