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 ( 2d8138...e2bbf4 )
by Jared
12:51
created

OAuth::getClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 1
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jaredchu
5
 * Date: 12/2/16
6
 * Time: 4:25 PM
7
 */
8
9
namespace JC\Firebase;
10
11
use Firebase\JWT\JWT;
12
use JC\JCRequest;
13
14
class OAuth
15
{
16
    /*
17
     * token life time in second
18
     * */
19
    public $tokenLifeTime;
20
    public $key;
21
    public $iss;
22
23
    protected $expireTimestamp;
24
    protected $accessToken;
25
26
    /**
27
     * OAuth constructor.
28
     *
29
     * @param $key
30
     * @param $iss
31
     * @param $lifeTime
32
     */
33
    public function __construct($key, $iss, $lifeTime = 3600)
34
    {
35
        $this->key = $key;
36
        $this->iss = $iss;
37
        $this->tokenLifeTime = $lifeTime;
38
    }
39
40
    public static function fromJson($jsonString, $lifeTime = 3600)
41
    {
42
        if ($jsonString) {
43
            $privateKey = $jsonString->private_key;
44 1
            $serviceAccount = $jsonString->client_email;
45
46
            return new static($privateKey, $serviceAccount, $lifeTime);
47
        } else {
48
            throw new \Exception("can't get data from key file");
49
        }
50
    }
51 1
52
    public static function fromKeyFile($keyFile, $lifeTime = 3600)
53
    {
54
        try {
55
            $jsonString = json_decode(file_get_contents($keyFile));
56
        } catch (\Exception $exception) {
57
            $jsonString = static::getClient()->get($keyFile)->json();
58
        }
59
60
        return static::fromJson($jsonString, $lifeTime);
61
    }
62
63
    public static function getClient(){
64
        return Client::getClient();
65
    }
66
67 2
    protected function requestAccessToken()
68
    {
69 2
        $currentTimestamp = time();
70 2
        $this->expireTimestamp = $currentTimestamp + $this->tokenLifeTime;
71
        $jsonToken = array(
72 2
            "iss" => $this->iss,
73 2
            "scope" => "https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email",
74 2
            "aud" => "https://www.googleapis.com/oauth2/v4/token",
75 2
            "exp" => $this->expireTimestamp,
76
            "iat" => $currentTimestamp
77 2
        );
78 2
        $jwt = JWT::encode($jsonToken, $this->key, 'RS256');
79
80 2
        $OAuthResponse = static::getClient()->post('https://www.googleapis.com/oauth2/v4/token', array(
81 2
            'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
82
            'assertion' => $jwt
83 2
        ));
84
85 2
        if ($OAuthResponse->status() == 200) {
86 2
            $this->accessToken = json_decode($OAuthResponse->body())->access_token;
87
88 2
            return true;
89
        }
90
91
        return false;
92
    }
93
94 14
    public function getAccessToken()
95
    {
96 14
        $currentTime = time();
97 14
        if ($this->expireTimestamp < $currentTime) {
98 2
            $startTime = time();
99 2
            $this->requestAccessToken();
100 2
            $endTime = time();
101 2
            $this->expireTimestamp -= ($endTime - $startTime);
102 2
        }
103
104 14
        return $this->accessToken;
105
    }
106
}