Passed
Push — master ( fd01cd...a8d522 )
by Alexandre
02:26
created

AuthorizationCodeFlow::handleAccessTokenRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Alexandre
5
 * Date: 07/03/2018
6
 * Time: 23:43
7
 */
8
9
namespace OAuth2\Extensions\PKCE\Flows;
10
11
12
use OAuth2\Endpoints\AuthorizationEndpoint;
13
use OAuth2\Endpoints\TokenEndpoint;
14
use OAuth2\Exceptions\OAuthException;
15
use OAuth2\Extensions\PKCE\Credentials\CodeChallenge;
16
use OAuth2\Extensions\PKCE\Storages\AuthorizationCodeStorageInterface;
17
use OAuth2\Roles\Clients\PublicClient;
18
19
/**
20
 * Class AuthorizationCodeFlow
21
 * @package OAuth2\Extensions\PKCE\Flows
22
 * @rfc https://tools.ietf.org/html/rfc7636
23
 */
24
class AuthorizationCodeFlow extends \OAuth2\Flows\AuthorizationCodeFlow
25
{
26
    /**
27
     * AuthorizationCodeFlow constructor.
28
     * @param AuthorizationCodeStorageInterface $authorizationCodeStorage
29
     */
30
    public function __construct(AuthorizationCodeStorageInterface $authorizationCodeStorage)
31
    {
32
        parent::__construct($authorizationCodeStorage);
33
        $this->authorizationCodeStorage = $authorizationCodeStorage;
34
    }
35
36
    /**
37
     * @param AuthorizationEndpoint $authorizationEndpoint
38
     * @param array $requestData
39
     * @return array
40
     * @throws OAuthException
41
     */
42
    function handleAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, array $requestData): array
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
43
    {
44
        $authorizationCode = $this->authorizationCodeStorage->create(
45
            implode(' ', $authorizationEndpoint->getScopes()),
46
            $authorizationEndpoint->getClient()->getIdentifier(),
47
            $authorizationEndpoint->getResourceOwner()->getIdentifier(),
48
            $requestData['scope'] ?? null,
49
            $requestData['redirect_uri'] ?? null
50
        );
51
52
        if (empty($requestData['code_challenge'])) {
53
            if ($authorizationEndpoint->getClient() instanceof PublicClient) {
54
                throw new OAuthException('invalid_request',
55
                    'The request is missing the required parameter code_challenge for public clients',
56
                    'https://tools.ietf.org/html/rfc7636#section-4.4');
57
            }
58
        } else {
59
            $codeChallengeMethod = empty($requestData['code_challenge_method']) ? 'plain' : $requestData['code_challenge_method'];
60
            if(!in_array($codeChallengeMethod, ['plain', 'S256'])) {
61
                throw new OAuthException('invalid_request',
62
                    'The request includes the invalid parameter code_challenge_method. Supported : plain, S256',
63
                    'https://tools.ietf.org/html/rfc7636#section-4');
64
            }
65
66
            $codeChallenge = new CodeChallenge($requestData['code_challenge'], $codeChallengeMethod);
67
            $this->authorizationCodeStorage->associate($codeChallenge, $authorizationCode);
0 ignored issues
show
Bug introduced by
The method associate() does not exist on OAuth2\Storages\AuthorizationCodeStorageInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to OAuth2\Storages\AuthorizationCodeStorageInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
            $this->authorizationCodeStorage->/** @scrutinizer ignore-call */ 
68
                                             associate($codeChallenge, $authorizationCode);
Loading history...
68
        }
69
70
        $this->authorizationCodeStorage->save($authorizationCode);
71
72
        return ['code' => $authorizationCode->getCode()];
73
    }
74
75
    public function handleAccessTokenRequest(TokenEndpoint $tokenEndpoint, array $requestData): array
76
    {
77
        // TODO https://tools.ietf.org/html/rfc7636#section-4.2
78
79
        return parent::handleAccessTokenRequest($tokenEndpoint, $requestData);
80
    }
81
82
}