forked from magnetikonline/php-google-spreadsheet-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleapi.php
128 lines (97 loc) · 3.24 KB
/
googleapi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace OAuth2;
class GoogleAPI extends Token {
const OAUTH2_TOKEN_EXPIRY_WINDOW = 300; // if token expiry is within this time window (seconds) then re-token
private $clientID;
private $clientSecret;
private $accessToken = false;
private $tokenType = false;
private $expiresAt = false;
private $refreshToken = false;
private $refreshTokenHandler = null;
public function __construct($tokenURL,$redirectURL,$clientID,$clientSecret) {
parent::__construct($tokenURL,$redirectURL);
// Google OAuth2 has the addition of 'client id' and 'client secret'
$this->clientID = $clientID;
$this->clientSecret = $clientSecret;
}
public function setTokenData($access,$type,$expiresAt,$refresh = false) {
$this->accessToken = $access;
$this->tokenType = $type;
$this->expiresAt = intval($expiresAt);
$this->refreshToken = $refresh;
}
public function setTokenRefreshHandler(callable $handler) {
$this->refreshTokenHandler = $handler;
}
public function getAuthHTTPHeader() {
// ensure we have the right bits of OAuth2 data available/previously set
if (
($this->accessToken === false) ||
($this->tokenType === false) ||
($this->expiresAt === false)
) {
// missing data
throw new \Exception ('Unable to build header - missing OAuth2 token information');
}
if (time() >= ($this->expiresAt - self::OAUTH2_TOKEN_EXPIRY_WINDOW)) {
// token is considered expired
if ($this->refreshToken === false) {
// we don't have a refresh token - can't build OAuth2 HTTP header
return false;
}
// get new access token (will be stored in $this->accessToken)
$tokenData = $this->getAccessTokenFromRefreshToken($this->refreshToken);
// if callback handler defined for token refresh events call it now
if ($this->refreshTokenHandler !== null) {
// include the refresh token in call to handler
$handler = $this->refreshTokenHandler;
$handler($tokenData + ['refreshToken' => $this->refreshToken]);
}
}
// return OAuth2 HTTP header as a name/value pair
return [
'Authorization',
sprintf('%s %s',$this->tokenType,$this->accessToken)
];
}
public function getAccessTokenFromAuthCode($code) {
return $this->storeTokenData(
parent::requestAccessTokenFromAuthCode(
$code,
$this->getAuthCredentialList()
),
true
);
}
public function getAccessTokenFromRefreshToken($token) {
return $this->storeTokenData(
parent::requestAccessTokenFromRefreshToken(
$token,
$this->getAuthCredentialList()
)
);
}
private function getAuthCredentialList() {
// all Google OAuth2 API requests need 'client id' and 'client secret' in their payloads
return [
'client_id' => $this->clientID,
'client_secret' => $this->clientSecret
];
}
private function storeTokenData(array $data,$hasRefreshKey = false) {
// save values - false for any key(s) that don't exist
$getValue = function($key) use ($data) {
return (isset($data[$key])) ? $data[$key] : false;
};
$this->accessToken = $getValue('accessToken');
$this->tokenType = $getValue('tokenType');
$this->expiresAt = $getValue('expiresAt');
if ($hasRefreshKey) {
// only save refresh token if expecting it
$this->refreshToken = $getValue('refreshToken');
}
// return $data for chaining
return $data;
}
}