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 ( c428a4...8cb3bd )
by Jared
12:57
created

OAuth::requestAccessToken()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
c 0
b 0
f 0
rs 8.8571
cc 2
eloc 17
nc 2
nop 0
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
            $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
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 = JCRequest::get($keyFile)->json();
58
        }
59
60
        return static::fromJson($jsonString, $lifeTime);
61
    }
62
63
    protected function requestAccessToken()
64
    {
65
        $currentTimestamp = time();
66
        $this->expireTimestamp = $currentTimestamp + $this->tokenLifeTime;
67
        $jsonToken = array(
68
            "iss" => $this->iss,
69
            "scope" => "https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email",
70
            "aud" => "https://www.googleapis.com/oauth2/v4/token",
71
            "exp" => $this->expireTimestamp,
72
            "iat" => $currentTimestamp
73
        );
74
        $jwt = JWT::encode($jsonToken, $this->key, 'RS256');
75
76
        $OAuthResponse = JCRequest::post('https://www.googleapis.com/oauth2/v4/token', array(
77
            'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
78
            'assertion' => $jwt
79
        ));
80
81
        if ($OAuthResponse->status() == 200) {
82
            $this->accessToken = json_decode($OAuthResponse->body())->access_token;
83
84
            return true;
85
        }
86
87
        return false;
88
    }
89
90
    public function getAccessToken()
91
    {
92
        $currentTime = time();
93
        if ($this->expireTimestamp < $currentTime) {
94
            $startTime = time();
95
            $this->requestAccessToken();
96
            $endTime = time();
97
            $this->expireTimestamp -= ($endTime - $startTime);
98
        }
99
100
        return $this->accessToken;
101
    }
102
}