Passed
Push — master ( cd0cec...4b3abd )
by Alexandre
06:20
created

verifyAuthorizationRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
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\Roles\Clients\ConfidentialClientInterface;
17
use OAuth2\ScopePolicy\ScopePolicyManager;
18
use OAuth2\Storages\AccessTokenStorageInterface;
19
use OAuth2\Storages\RefreshTokenStorageInterface;
20
21
class ClientCredentialsFlow extends AbstractGrantType implements FlowInterface
22
{
23
    /**
24
     * @var ScopePolicyManager
25
     */
26
    private $scopePolicyManager;
27
28
    public function __construct(ScopePolicyManager $scopePolicyManager,
29
                                AccessTokenStorageInterface $accessTokenStorage,
30
                                RefreshTokenStorageInterface $refreshTokenStorage)
31
    {
32
        parent::__construct($accessTokenStorage, $refreshTokenStorage);
33
        $this->scopePolicyManager = $scopePolicyManager;
34
    }
35
36
    public function getResponseTypes(): array
37
    {
38
        return [];
39
    }
40
41
    public function getGrantTypes(): array
42
    {
43
        return ['client_credentials'];
44
    }
45
46
    /**
47
     * @param TokenEndpoint $tokenEndpoint
48
     * @param array $requestData
49
     * @return array
50
     * @throws OAuthException
51
     */
52
    public function handleAccessTokenRequest(TokenEndpoint $tokenEndpoint, array $requestData): array
53
    {
54
        if (!$tokenEndpoint->getClient() instanceof ConfidentialClientInterface) {
55
            throw new OAuthException('unauthorized_client',
56
                'The authenticated client is not authorized to use this authorization grant type. 
57
                The client credentials grant type MUST only be used by confidential clients.',
58
                'https://tools.ietf.org/html/rfc6749#section-4.4');
59
        }
60
61
        $scopes = $this->scopePolicyManager->getScopes($tokenEndpoint->getClient(), $requestData['scope'] ?? null);
62
        $this->scopePolicyManager->verifyScopes($tokenEndpoint->getClient(), $scopes);
63
64
        return $this->issueAccessToken(implode(' ', $scopes), $tokenEndpoint->getClient()->getIdentifier(), null);
65
    }
66
67
    public function handleAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, array $requestData): array
68
    {
69
        throw new \BadMethodCallException();
70
    }
71
72
    public function verifyAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, array $requestData)
73
    {
74
        throw new \BadMethodCallException();
75
    }
76
77
    public function getDefaultResponseMode(): string
78
    {
79
        throw new \BadMethodCallException();
80
    }
81
82
    public function getUnsupportedResponseModes(): array
83
    {
84
        throw new \BadMethodCallException();
85
    }
86
}