|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rinvex\OAuth\Grants; |
|
6
|
|
|
|
|
7
|
|
|
use DateInterval; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
use League\OAuth2\Server\Grant\AbstractGrant; |
|
10
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
|
11
|
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; |
|
12
|
|
|
|
|
13
|
|
|
class PersonalAccessGrant extends AbstractGrant |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Respond to an access token request. |
|
17
|
|
|
* |
|
18
|
|
|
* @param ServerRequestInterface $request |
|
19
|
|
|
* @param ResponseTypeInterface $responseType |
|
20
|
|
|
* @param DateInterval $accessTokenTTL |
|
21
|
|
|
* |
|
22
|
|
|
* @throws OAuthServerException |
|
23
|
|
|
* |
|
24
|
|
|
* @return ResponseTypeInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseTypeInterface $responseType, DateInterval $accessTokenTTL) |
|
27
|
|
|
{ |
|
28
|
|
|
// Validate request |
|
29
|
|
|
$this->validateUser($request); |
|
30
|
|
|
$client = $this->validateClient($request); |
|
31
|
|
|
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request)); |
|
32
|
|
|
|
|
33
|
|
|
// Finalize the requested scopes |
|
34
|
|
|
$scopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client); |
|
35
|
|
|
|
|
36
|
|
|
// Issue and persist access token |
|
37
|
|
|
$accessToken = $this->issueAccessToken( |
|
38
|
|
|
$accessTokenTTL, |
|
39
|
|
|
$client, |
|
40
|
|
|
$this->getRequestParameter('user_id', $request), |
|
41
|
|
|
$scopes |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
// Inject access token into response type |
|
45
|
|
|
$responseType->setAccessToken($accessToken); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
return $responseType; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Validate the authorization code user. |
|
52
|
|
|
* |
|
53
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
|
54
|
|
|
* |
|
55
|
|
|
* @throws \League\OAuth2\Server\Exception\OAuthServerException |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function validateUser(ServerRequestInterface $request) |
|
58
|
|
|
{ |
|
59
|
|
|
[$userType, $userId] = explode(':', $this->getRequestParameter('user_id', $request)); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
if ($userType !== request()->user()->getMorphClass() || $userId !== request()->user()->getRouteKey()) { |
|
62
|
|
|
throw OAuthServerException::invalidRequest('user_id', 'This action is not authorized to this user'); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getIdentifier() |
|
70
|
|
|
{ |
|
71
|
|
|
return 'personal_access'; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: