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 ( 3d0127...22375b )
by François
18:26
created

AccessToken::getToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * Copyright (c) 2016, 2017 François Kooman <[email protected]>.
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in all
14
 * copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
25
namespace fkooman\OAuth\Client;
26
27
use DateTime;
28
use RuntimeException;
29
30
/**
31
 * AccessToken object containing the response from the OAuth 2.0 provider's
32
 * token response.
33
 */
34
class AccessToken
35
{
36
    /** @var string */
37
    private $accessToken;
38
39
    /** @var string */
40
    private $tokenType;
41
42
    /** @var string */
43
    private $scope;
44
45
    /** @var string|null */
46
    private $refreshToken;
47
48
    /** @var \DateTime */
49
    private $expiresAt;
50
51
    public function __construct($accessToken, $tokenType, $scope, $refreshToken, DateTime $expiresAt)
52
    {
53
        $this->accessToken = $accessToken;
54
        $this->tokenType = $tokenType;
55
        $this->scope = $scope;
56
        $this->refreshToken = $refreshToken;
57
        $this->expiresAt = $expiresAt;
58
    }
59
60
    /**
61
     * Get the access token.
62
     *
63
     * @return string the access token
64
     *
65
     * @see https://tools.ietf.org/html/rfc6749#section-5.1
66
     */
67
    public function getToken()
68
    {
69
        return $this->accessToken;
70
    }
71
72
    /**
73
     * Get the token type.
74
     *
75
     * @return string the token type
76
     *
77
     * @see https://tools.ietf.org/html/rfc6749#section-7.1
78
     */
79
    public function getTokenType()
80
    {
81
        return $this->tokenType;
82
    }
83
84
    /**
85
     * Get the scope.
86
     *
87
     * @return string the scope
88
     *
89
     * @see https://tools.ietf.org/html/rfc6749#section-3.3
90
     */
91
    public function getScope()
92
    {
93
        return $this->scope;
94
    }
95
96
    /**
97
     * Get the refresh token.
98
     *
99
     * @return string|null the refresh token
100
     *
101
     * @see https://tools.ietf.org/html/rfc6749#section-1.5
102
     */
103
    public function getRefreshToken()
104
    {
105
        return $this->refreshToken;
106
    }
107
108
    /**
109
     * @return DateTime
110
     */
111
    public function getExpiresAt()
112
    {
113
        return $this->expiresAt;
114
    }
115
116
    /**
117
     * @param DateTime $dateTime
118
     *
119
     * @return bool
120
     */
121
    public function isExpired(DateTime $dateTime)
122
    {
123
        return $dateTime >= $this->expiresAt;
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function json()
130
    {
131
        return json_encode(
132
            [
133
                'access_token' => $this->getToken(),
134
                'token_type' => $this->getTokenType(),
135
                'scope' => $this->getScope(),
136
                'refresh_token' => $this->getRefreshToken(),
137
                'expires_at' => $this->getExpiresAt()->format('Y-m-d H:i:s'),
138
            ]
139
        );
140
    }
141
142
    /**
143
     * @param string $jsonData
144
     *
145
     * @return AccessToken
146
     */
147
    public static function fromJson($jsonData)
148
    {
149
        $tokenData = json_decode($jsonData, true);
150
        if (is_null($tokenData) && JSON_ERROR_NONE !== json_last_error()) {
151
            $errorMsg = function_exists('json_last_error_msg') ? json_last_error_msg() : json_last_error();
152
            throw new RuntimeException(sprintf('unable to decode JSON: %s', $errorMsg));
153
        }
154
155
        if (!is_array($tokenData)) {
156
            throw new RuntimeException('JSON data MUST be an array');
157
        }
158
159
        $requiredKeys = ['access_token', 'token_type', 'scope', 'refresh_token', 'expires_at'];
160
        foreach ($requiredKeys as $key) {
161
            if (!array_key_exists($key, $tokenData)) {
162
                throw new RuntimeException(sprintf('missing key "%s" in JSON data', $key));
163
            }
164
        }
165
166
        return new self(
167
            $tokenData['access_token'],
168
            $tokenData['token_type'],
169
            $tokenData['scope'],
170
            $tokenData['refresh_token'],
171
            new DateTime($tokenData['expires_at'])
172
        );
173
    }
174
}
175