1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* OAuth 2.0 Client credentials grant. |
4
|
|
|
* |
5
|
|
|
* @author Alex Bilbie <[email protected]> |
6
|
|
|
* @copyright Copyright (c) Alex Bilbie |
7
|
|
|
* @license http://mit-license.org/ |
8
|
|
|
* |
9
|
|
|
* @link https://github.com/thephpleague/oauth2-server |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace League\OAuth2\Server\Grant; |
13
|
|
|
|
14
|
|
|
use DateInterval; |
15
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
16
|
|
|
use League\OAuth2\Server\RequestEvent; |
17
|
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Client credentials grant class. |
22
|
|
|
*/ |
23
|
|
|
class ClientCredentialsGrant extends AbstractGrant |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
4 |
|
public function respondToAccessTokenRequest( |
29
|
|
|
ServerRequestInterface $request, |
30
|
|
|
ResponseTypeInterface $responseType, |
31
|
|
|
DateInterval $accessTokenTTL |
32
|
|
|
) { |
33
|
4 |
|
list($clientId) = $this->getClientCredentials($request); |
34
|
|
|
|
35
|
4 |
|
$client = $this->getClientEntityOrFail($clientId, $request); |
36
|
|
|
|
37
|
3 |
|
if (!$client->isConfidential()) { |
38
|
|
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); |
39
|
|
|
|
40
|
|
|
throw OAuthServerException::invalidClient($request); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// Validate request |
44
|
3 |
|
$this->validateClient($request); |
45
|
|
|
|
46
|
3 |
|
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request, $this->defaultScope)); |
47
|
|
|
|
48
|
|
|
// Finalize the requested scopes |
49
|
3 |
|
$finalizedScopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client); |
50
|
|
|
|
51
|
3 |
|
$privateClaims = []; |
52
|
3 |
|
if ($this->claimRepository) { |
53
|
1 |
|
$privateClaims = $this->claimRepository->getClaims($privateClaims, $this->getIdentifier(), $client); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Issue and persist access token |
57
|
3 |
|
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, null, $finalizedScopes, $privateClaims); |
58
|
|
|
|
59
|
|
|
// Send event to emitter |
60
|
3 |
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request)); |
61
|
|
|
|
62
|
|
|
// Inject access token into response type |
63
|
3 |
|
$responseType->setAccessToken($accessToken); |
64
|
|
|
|
65
|
3 |
|
return $responseType; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
6 |
|
public function getIdentifier() |
72
|
|
|
{ |
73
|
6 |
|
return 'client_credentials'; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|