GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( ef6996...428169 )
by François
02:05
created

OAuth2Client::getAuthorizationRequestUri()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 22
rs 9.2
cc 2
eloc 14
nc 1
nop 2
1
<?php
2
/**
3
 * Copyright 2016 François Kooman <[email protected]>.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace fkooman\OAuth\Client;
19
20
use fkooman\OAuth\Client\Exception\OAuthException;
21
22
class OAuth2Client
23
{
24
    /** @var Provider */
25
    private $provider;
26
27
    /** @var HttpClientInterface */
28
    private $httpClient;
29
30
    /** @var RandomInterface */
31
    private $random;
32
33
    public function __construct(Provider $provider, HttpClientInterface $httpClient, RandomInterface $random = null)
34
    {
35
        $this->provider = $provider;
36
        $this->httpClient = $httpClient;
37
        if (is_null($random)) {
38
            $random = new Random();
39
        }
40
        $this->random = $random;
41
    }
42
43
    public function getAuthorizationRequestUri($scope, $redirectUri)
44
    {
45
        $state = $this->random->get();
46
47
        $queryParams = http_build_query(
48
            [
49
                'client_id' => $this->provider->getId(),
50
                'redirect_uri' => $redirectUri,
51
                'scope' => $scope,
52
                'state' => $state,
53
                'response_type' => 'code',
54
            ],
55
            '&'
56
        );
57
58
        return sprintf(
59
            '%s%s%s',
60
            $this->provider->getAuthorizationEndpoint(),
61
            false === strpos($this->provider->getAuthorizationEndpoint(), '?') ? '?' : '&',
62
            $queryParams
63
        );
64
    }
65
66
    public function getAccessToken($authorizationRequestUri, $authorizationResponseCode, $authorizationResponseState)
67
    {
68
        // parse our authorizationRequestUri to extract the state
69
        if (false === strpos($authorizationRequestUri, '?')) {
70
            throw new OAuthException('invalid authorizationRequestUri');
71
        }
72
73
        parse_str(explode('?', $authorizationRequestUri)[1], $queryParams);
74
75
        if (!isset($queryParams['state'])) {
76
            throw new OAuthException('state missing from authorizationRequestUri');
77
        }
78
79
        if (!isset($queryParams['redirect_uri'])) {
80
            throw new OAuthException('redirect_uri missing from authorizationRequestUri');
81
        }
82
83
        if ($authorizationResponseState !== $queryParams['state']) {
84
            throw new OAuthException('state from authorizationRequestUri MUST match authorizationResponseState');
85
        }
86
87
        // prepare access_token request
88
        $tokenRequestData = [
89
            'client_id' => $this->provider->getId(),
90
            'grant_type' => 'authorization_code',
91
            'code' => $authorizationResponseCode,
92
            'redirect_uri' => $queryParams['redirect_uri'],
93
        ];
94
95
        $responseData = $this->httpClient->post(
96
            $this->provider,
97
            $tokenRequestData
98
        );
99
100
        if (!isset($responseData['access_token'])) {
101
            throw new OAuthException('no access_token received from token endpoint');
102
        }
103
104
        if (!isset($responseData['token_type'])) {
105
            throw new OAuthException('no token_type received from token endpoint');
106
        }
107
108
        $scope = null;
109
        if (isset($responseData['scope'])) {
110
            $scope = $responseData['scope'];
111
        }
112
113
        $expiresIn = null;
114
        if (isset($responseData['expires_in'])) {
115
            $expiresIn = $responseData['expires_in'];
116
        }
117
118
        return new AccessToken(
119
            $responseData['access_token'],
120
            $responseData['token_type'],
121
            $scope,
122
            $expiresIn
123
        );
124
    }
125
}
126