1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: GCC-MED |
5
|
|
|
* Date: 12/03/2018 |
6
|
|
|
* Time: 15:41 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace OAuth2\Flows; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use OAuth2\Endpoints\AuthorizationEndpoint; |
13
|
|
|
use OAuth2\Endpoints\TokenEndpoint; |
14
|
|
|
use OAuth2\Exceptions\OAuthException; |
15
|
|
|
use OAuth2\GrantTypes\AbstractGrantType; |
16
|
|
|
use OAuth2\GrantTypes\GrantTypeInterface; |
17
|
|
|
use OAuth2\ResponseTypes\ResponseTypeInterface; |
18
|
|
|
use OAuth2\Roles\Clients\ConfidentialClientInterface; |
19
|
|
|
use OAuth2\ScopePolicy\ScopePolicyManager; |
20
|
|
|
use OAuth2\Storages\AccessTokenStorageInterface; |
21
|
|
|
use OAuth2\Storages\RefreshTokenStorageInterface; |
22
|
|
|
|
23
|
|
|
class ClientCredentialsFlow extends AbstractGrantType implements FlowInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ScopePolicyManager |
27
|
|
|
*/ |
28
|
|
|
private $scopePolicyManager; |
29
|
|
|
|
30
|
|
|
public function __construct(ScopePolicyManager $scopePolicyManager, |
31
|
|
|
AccessTokenStorageInterface $accessTokenStorage, |
32
|
|
|
RefreshTokenStorageInterface $refreshTokenStorage) |
33
|
|
|
{ |
34
|
|
|
parent::__construct($accessTokenStorage, $refreshTokenStorage); |
35
|
|
|
$this->scopePolicyManager = $scopePolicyManager; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return ResponseTypeInterface[] |
40
|
|
|
*/ |
41
|
|
|
function getResponseTypes(): array |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
return []; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return GrantTypeInterface[] |
48
|
|
|
*/ |
49
|
|
|
function getGrantTypes(): array |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
return ['client_credentials']; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
function handleAccessTokenRequest(TokenEndpoint $tokenEndpoint, array $requestData): array |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
if (!$tokenEndpoint->getClient() instanceof ConfidentialClientInterface) { |
57
|
|
|
throw new OAuthException('unauthorized_client', |
58
|
|
|
'The authenticated client is not authorized to use this authorization grant type. |
59
|
|
|
The client credentials grant type MUST only be used by confidential clients.', |
60
|
|
|
'https://tools.ietf.org/html/rfc6749#section-4.4'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$scopes = $this->scopePolicyManager->getScopes($tokenEndpoint->getClient(), $requestData['scope'] ?? null); |
64
|
|
|
$this->scopePolicyManager->verifyScopes($tokenEndpoint->getClient(), $scopes); |
65
|
|
|
|
66
|
|
|
return $this->issueAccessToken(implode(' ', $scopes), $tokenEndpoint->getClient()->getIdentifier(), null); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
function handleAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, array $requestData): array |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
throw new \BadMethodCallException(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
function getDefaultResponseMode(): string |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
throw new \BadMethodCallException(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
function getUnsupportedResponseModes(): array |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
throw new \BadMethodCallException(); |
82
|
|
|
} |
83
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.