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 ( 015291...4b7930 )
by François
04:19
created

BearerClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
/**
3
 *  Copyright (C) 2017 François Kooman <[email protected]>.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace fkooman\OAuth\Client\Http;
20
21
use fkooman\OAuth\Client\AccessToken;
22
use fkooman\OAuth\Client\Exception\OAuthServerException;
23
use fkooman\OAuth\Client\OAuth2Client;
24
25
class BearerClient
26
{
27
    /** @var \fkooman\OAuth\Client\OAuth2Client */
28
    private $oauthClient;
29
30
    /** @var callable */
31
    private $setToken;
32
33
    /** @var callable */
34
    private $deleteToken;
35
36
    public function __construct(OAuth2Client $oauthClient, callable $setToken, callable $deleteToken)
37
    {
38
        $this->oauthClient = $oauthClient;
39
        $this->setToken = $setToken;
40
        $this->deleteToken = $deleteToken;
41
    }
42
43
    /**
44
     * @return Response|false
45
     */
46
    public function get(AccessToken $accessToken, $requestUri, array $requestHeaders = [])
47
    {
48
        $refreshedToken = false;
49
        if ($accessToken->isExpired()) {
50
            error_log('access_token expired');
51
            // access_token is expired, try to refresh it
52
            if (is_null($accessToken->getRefreshToken())) {
53
                error_log('no refresh_token available, delete access_token');
54
                // we do not have a refresh_token, delete this access token, it
55
                // is useless now...
56
                call_user_func($this->deleteToken, $accessToken);
57
58
                return false;
59
            }
60
61
            error_log('attempting to refresh access_token');
62
            // deal with possibly revoked authorization! XXX
63
            try {
64
                $accessToken = $this->oauthClient->refreshAccessToken($accessToken);
65
            } catch (OAuthServerException $e) {
66
                error_log(sprintf('unable to use refresh_token %s', $e->getMessage()));
67
68
                // delete the access_token, the refresh_token could not be used
69
70
                call_user_func($this->deleteToken, $accessToken);
71
72
                return false;
73
            }
74
75
            // maybe delete old accesstoken here? XXX
76
            error_log('access_token refreshed');
77
            $refreshedToken = true;
78
        }
79
80
        // add Authorization header to the request headers
81
        $requestHeaders['Authorization'] = sprintf('Bearer %s', $accessToken->getToken());
82
83
        $response = $this->oauthClient->getHttpClient()->get($requestUri, $requestHeaders);
84
        if (401 === $response->getStatusCode()) {
85
            error_log('access_token appears to be invalid, delete access_token');
86
            // this indicates an invalid access_token
87
            call_user_func($this->deleteToken, $accessToken);
88
89
            return false;
90
        }
91
92
        error_log('access_token was valid, call succeeded');
93
94
        if ($refreshedToken) {
95
            error_log('access_token was refreshed, so store it now for future use');
96
            // if we refreshed the token, and it was successful, i.e. not a 401,
97
            // update the stored AccessToken
98
            call_user_func($this->setToken, $accessToken);
99
        }
100
101
        return $response;
102
    }
103
104
    public function post(AccessToken $accessToken, $requestUri, array $postData = [], array $requestHeaders = [])
0 ignored issues
show
Unused Code introduced by
The parameter $accessToken is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $requestUri is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $postData is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $requestHeaders is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
    {
106
    }
107
}
108