|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CodexShaper\OAuth2\Server; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use CodexShaper\OAuth2\Server\Entities\Client as ClientEntity; |
|
7
|
|
|
use DateTime; |
|
8
|
|
|
|
|
9
|
|
|
class Model |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
protected static $authCodeModel = '\CodexShaper\OAuth2\Server\Models\AuthCode'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected static $clientModel = '\CodexShaper\OAuth2\Server\Models\Client'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected static $refreshTokenModel = '\CodexShaper\OAuth2\Server\Models\RefreshToken'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected static $tokenModel = '\CodexShaper\OAuth2\Server\Models\Token'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected static $userModel = '\CodexShaper\OAuth2\Server\Models\User'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Create a new model instance. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $model The model name |
|
40
|
|
|
* |
|
41
|
|
|
* @return \CodexShaper\OAuth2\Server\Models\AuthCode|\CodexShaper\OAuth2\Server\Models\Client|\CodexShaper\OAuth2\Server\Models\RefreshToken|\CodexShaper\OAuth2\Server\Models\Token|\CodexShaper\OAuth2\Server\Models\User|null |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function instance($model) |
|
44
|
|
|
{ |
|
45
|
|
|
if (!static::${$model}) { |
|
46
|
|
|
return null; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return new static::${$model}(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get a client. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $clientIdentifier The client's identifier |
|
56
|
|
|
* |
|
57
|
|
|
* @return \League\OAuth2\Server\Entities\ClientEntityInterface|null |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function getClientEntity($clientIdentifier) |
|
60
|
|
|
{ |
|
61
|
|
|
$client = static::instance('clientModel'); |
|
62
|
|
|
|
|
63
|
|
|
$record = $client->where($client->getKeyName(), $clientIdentifier)->first(); |
|
64
|
|
|
|
|
65
|
|
|
if (!$record) { |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return new ClientEntity( |
|
70
|
|
|
$clientIdentifier, |
|
71
|
|
|
$record->name, |
|
72
|
|
|
$record->redirect, |
|
73
|
|
|
$record->isConfidential() |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* store a new auth code to permanent storage. |
|
79
|
|
|
* |
|
80
|
|
|
* @param \League\OAuth2\Server\Entities\AuthCodeEntityInterface $accessTokenEntity |
|
|
|
|
|
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function storeAuthCode($authCodeEntity) |
|
85
|
|
|
{ |
|
86
|
|
|
static::instance('authCodeModel')->create([ |
|
87
|
|
|
'id' => $authCodeEntity->getIdentifier(), |
|
88
|
|
|
'user_id' => $authCodeEntity->getUserIdentifier(), |
|
89
|
|
|
'client_id' => $authCodeEntity->getClient()->getIdentifier(), |
|
90
|
|
|
'scopes' => $authCodeEntity->getScopes(), |
|
91
|
|
|
'revoked' => 0, |
|
92
|
|
|
'expires_at' => $authCodeEntity->getExpiryDateTime(), |
|
93
|
|
|
]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* store a new access token to permanent storage. |
|
98
|
|
|
* |
|
99
|
|
|
* @param \League\OAuth2\Server\Entities\AccessTokenEntityInterface $accessTokenEntity |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function storeAccessToken($accessTokenEntity) |
|
104
|
|
|
{ |
|
105
|
|
|
static::instance('tokenModel')->create([ |
|
106
|
|
|
'id' => $accessTokenEntity->getIdentifier(), |
|
107
|
|
|
'user_id' => $accessTokenEntity->getUserIdentifier(), |
|
108
|
|
|
'client_id' => $accessTokenEntity->getClient()->getIdentifier(), |
|
109
|
|
|
'scopes' => $accessTokenEntity->getScopes(), |
|
110
|
|
|
'revoked' => false, |
|
111
|
|
|
'created_at' => new DateTime(), |
|
112
|
|
|
'updated_at' => new DateTime(), |
|
113
|
|
|
'expires_at' => $accessTokenEntity->getExpiryDateTime(), |
|
114
|
|
|
]); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* store a new access refresh token to permanent storage. |
|
119
|
|
|
* |
|
120
|
|
|
* @param \League\OAuth2\Server\Entities\RefreshTokenEntityInterface $refreshTokenEntity |
|
121
|
|
|
* |
|
122
|
|
|
* @return void |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function storeRefreshToken($refreshTokenEntity) |
|
125
|
|
|
{ |
|
126
|
|
|
static::instance('refreshTokenModel')->create([ |
|
127
|
|
|
'id' => $refreshTokenEntity->getIdentifier(), |
|
128
|
|
|
'access_token_id' => $refreshTokenEntity->getAccessToken()->getIdentifier(), |
|
129
|
|
|
'revoked' => false, |
|
130
|
|
|
'expires_at' => $refreshTokenEntity->getExpiryDateTime(), |
|
131
|
|
|
]); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Validate a client's secret. |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $clientIdentifier The client's identifier |
|
138
|
|
|
* @param null|string $clientSecret The client's secret (if sent) |
|
139
|
|
|
* @param null|string $grantType The type of grant the client is using (if sent) |
|
140
|
|
|
* |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
|
|
public static function validateClientCredentials($clientIdentifier, $clientSecret, $grantType) |
|
144
|
|
|
{ |
|
145
|
|
|
$client = static::instance('clientModel'); |
|
146
|
|
|
|
|
147
|
|
|
$record = $client->where($client->getKeyName(), $clientIdentifier)->first(); |
|
148
|
|
|
|
|
149
|
|
|
if (!$record || !static::handlesGrant($record, $grantType)) { |
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return !$record->isConfidential() || hash_equals($record->secret, (string) $clientSecret); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Determine if the given client can handle the given grant type. |
|
158
|
|
|
* |
|
159
|
|
|
* @param \CodexShaper\OAuth2\Server\Models\Client $record |
|
160
|
|
|
* @param string $grantType |
|
161
|
|
|
* |
|
162
|
|
|
* @return bool |
|
163
|
|
|
*/ |
|
164
|
|
|
protected static function handlesGrant($record, $grantType) |
|
165
|
|
|
{ |
|
166
|
|
|
if (is_array($record->grant_types) && !in_array($grantType, $record->grant_types)) { |
|
167
|
|
|
return false; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
switch ($grantType) { |
|
171
|
|
|
case 'authorization_code': |
|
172
|
|
|
return $record->authorization_code_client; |
|
173
|
|
|
case 'password': |
|
174
|
|
|
return $record->password_client; |
|
175
|
|
|
case 'client_credentials': |
|
176
|
|
|
return $record->isConfidential(); |
|
177
|
|
|
default: |
|
178
|
|
|
return true; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Veryfy user credentials for current grant. |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $username |
|
186
|
|
|
* @param string $password |
|
187
|
|
|
* @param string $grantType |
|
188
|
|
|
* @param \League\OAuth2\Server\Entities\ClientEntityInterface $clientEntity |
|
189
|
|
|
* |
|
190
|
|
|
* @return \CodexShaper\OAuth2\Server\Models\User|null |
|
191
|
|
|
*/ |
|
192
|
|
|
public static function verifyUserForGrant($username, $password, $grantType, $clientEntity) |
|
|
|
|
|
|
193
|
|
|
{ |
|
194
|
|
|
if ($grantType == 'password') { |
|
195
|
|
|
$model = static::instance('userModel'); |
|
196
|
|
|
|
|
197
|
|
|
$user = $model->where($model->getUserIndentifier(), $username)->first(); |
|
198
|
|
|
|
|
199
|
|
|
if (!$user) { |
|
200
|
|
|
return null; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
if (!md5($password) === $user->password) { |
|
204
|
|
|
return null; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
return $user->{$user->getKeyName()}; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return null; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Revoke provided token id. |
|
215
|
|
|
* |
|
216
|
|
|
* @param string $model |
|
217
|
|
|
* @param string $grantType |
|
|
|
|
|
|
218
|
|
|
* |
|
219
|
|
|
* @return void |
|
220
|
|
|
*/ |
|
221
|
|
|
public static function revoke($model, $tokenId) |
|
222
|
|
|
{ |
|
223
|
|
|
static::instance($model)->whereId($tokenId)->update(['revoked' => true]); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Determine if the given token id is revoked or not. |
|
228
|
|
|
* |
|
229
|
|
|
* @param string $model |
|
230
|
|
|
* @param string $grantType |
|
|
|
|
|
|
231
|
|
|
* |
|
232
|
|
|
* @return bool |
|
233
|
|
|
*/ |
|
234
|
|
|
public static function isRevoked($model, $tokenId) |
|
235
|
|
|
{ |
|
236
|
|
|
return static::instance($model)->whereId($tokenId)->whereRevoked(1)->exists(); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Authorization token. |
|
241
|
|
|
* |
|
242
|
|
|
* @param string $model |
|
243
|
|
|
* @param array $data |
|
|
|
|
|
|
244
|
|
|
* |
|
245
|
|
|
* @return \CodexShaper\OAuth2\Server\Models\AuthCode|\CodexShaper\OAuth2\Server\Models\Client|\CodexShaper\OAuth2\Server\Models\RefreshToken|\CodexShaper\OAuth2\Server\Models\Token|\CodexShaper\OAuth2\Server\Models\User|null |
|
246
|
|
|
*/ |
|
247
|
|
|
public static function findToken($model, $authRequest, $user) |
|
248
|
|
|
{ |
|
249
|
|
|
return static::instance($model) |
|
250
|
|
|
->find($authRequest->getClient()->getIdentifier()) |
|
251
|
|
|
->tokens() |
|
252
|
|
|
->whereUserId($user->getKey()) |
|
253
|
|
|
->whereRevoked(0) |
|
254
|
|
|
->where('expires_at', '>', Carbon::now()) |
|
255
|
|
|
->latest('expires_at') |
|
256
|
|
|
->first(); |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.