|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: jaredchu |
|
5
|
|
|
* Date: 12/2/16 |
|
6
|
|
|
* Time: 4:25 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace JCFirebase; |
|
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
|
|
|
protected $cache; |
|
27
|
|
|
const CACHE_KEY = 'firebase-oauth'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* OAuth constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param $key |
|
33
|
|
|
* @param $iss |
|
34
|
|
|
* @param $lifeTime |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($key, $iss, $lifeTime = 3600, $cache = false) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->key = $key; |
|
39
|
|
|
$this->iss = $iss; |
|
40
|
|
|
$this->tokenLifeTime = $lifeTime; |
|
41
|
|
|
$this->cache = $cache; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
public static function fromJson($jsonString, $lifeTime = 3600, $cache = false) |
|
45
|
|
|
{ |
|
46
|
|
|
if ($jsonString) { |
|
47
|
|
|
$privateKey = $jsonString->private_key; |
|
48
|
|
|
$serviceAccount = $jsonString->client_email; |
|
49
|
|
|
|
|
50
|
|
|
return new static($privateKey, $serviceAccount, $lifeTime, $cache); |
|
51
|
1 |
|
} else { |
|
52
|
|
|
throw new \Exception("can't get data from key file"); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public static function fromKeyFile($keyFile, $lifeTime = 3600, $cache = false) |
|
57
|
|
|
{ |
|
58
|
|
|
try { |
|
59
|
|
|
$jsonString = json_decode(file_get_contents($keyFile)); |
|
60
|
|
|
} catch (\Exception $exception) { |
|
61
|
|
|
$jsonString = json_decode(JCRequest::get($keyFile)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return static::fromJson($jsonString, $lifeTime, $cache); |
|
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 = JCRequest::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
|
13 |
|
public function getAccessToken() |
|
95
|
|
|
{ |
|
96
|
13 |
|
$currentTime = time(); |
|
97
|
13 |
|
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
|
13 |
|
return $this->accessToken; |
|
105
|
|
|
} |
|
106
|
|
|
} |