|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rinvex\OAuth\Grants; |
|
6
|
|
|
|
|
7
|
|
|
use DateInterval; |
|
8
|
|
|
use League\OAuth2\Server\RequestEvent; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use League\OAuth2\Server\Grant\AbstractGrant; |
|
11
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
|
12
|
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; |
|
13
|
|
|
|
|
14
|
|
|
class ClientCredentialsGrant extends AbstractGrant |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Respond to an access token request. |
|
18
|
|
|
* |
|
19
|
|
|
* @param ServerRequestInterface $request |
|
20
|
|
|
* @param ResponseTypeInterface $responseType |
|
21
|
|
|
* @param DateInterval $accessTokenTTL |
|
22
|
|
|
* |
|
23
|
|
|
* @throws OAuthServerException |
|
24
|
|
|
* |
|
25
|
|
|
* @return ResponseTypeInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseTypeInterface $responseType, DateInterval $accessTokenTTL) |
|
28
|
|
|
{ |
|
29
|
|
|
[$clientId] = $this->getClientCredentials($request); |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
$client = $this->getClientEntityOrFail($clientId, $request); |
|
32
|
|
|
|
|
33
|
|
|
if (!$client->isConfidential()) { |
|
34
|
|
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); |
|
35
|
|
|
|
|
36
|
|
|
throw OAuthServerException::invalidClient($request); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// Validate request |
|
40
|
|
|
$this->validateClient($request); |
|
41
|
|
|
$this->validateUser($request); |
|
42
|
|
|
|
|
43
|
|
|
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request, $this->defaultScope)); |
|
44
|
|
|
|
|
45
|
|
|
// Finalize the requested scopes |
|
46
|
|
|
$finalizedScopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client); |
|
47
|
|
|
|
|
48
|
|
|
// Issue and persist access token |
|
49
|
|
|
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $this->getRequestParameter('user_id', $request), $finalizedScopes); |
|
50
|
|
|
|
|
51
|
|
|
// Send event to emitter |
|
52
|
|
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request)); |
|
53
|
|
|
|
|
54
|
|
|
// Inject access token into response type |
|
55
|
|
|
$responseType->setAccessToken($accessToken); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
return $responseType; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Validate the authorization code user. |
|
62
|
|
|
* |
|
63
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
|
64
|
|
|
* |
|
65
|
|
|
* @throws \League\OAuth2\Server\Exception\OAuthServerException |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function validateUser(ServerRequestInterface $request) |
|
68
|
|
|
{ |
|
69
|
|
|
[$userType, $userId] = explode(':', $this->getRequestParameter('user_id', $request)); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
if ($userType !== request()->user()->getMorphClass() || $userId !== request()->user()->getRouteKey()) { |
|
72
|
|
|
throw OAuthServerException::invalidRequest('user_id', 'This action is not authorized to this user'); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getIdentifier() |
|
80
|
|
|
{ |
|
81
|
|
|
return 'client_credentials'; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.