|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright 2015 Alexey Maslov <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace alxmsl\Google\OAuth2\Response; |
|
19
|
|
|
|
|
20
|
|
|
use alxmsl\Google\InitializationInterface; |
|
21
|
|
|
use OutOfBoundsException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Authorized token object |
|
25
|
|
|
* @author alxmsl |
|
26
|
|
|
* @date 2/4/13 |
|
27
|
|
|
*/ |
|
28
|
|
|
final class Token implements InitializationInterface { |
|
29
|
|
|
/** |
|
30
|
|
|
* Token type constants |
|
31
|
|
|
*/ |
|
32
|
|
|
const TYPE_UNKNOWN = 0, |
|
33
|
|
|
TYPE_BEARER = 1; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string access token |
|
37
|
|
|
*/ |
|
38
|
|
|
private $accessToken = ''; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var int token type |
|
42
|
|
|
*/ |
|
43
|
|
|
private $tokenType = self::TYPE_BEARER; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var int access token expires in |
|
47
|
|
|
*/ |
|
48
|
|
|
private $expiresIn = 0; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string id token |
|
52
|
|
|
*/ |
|
53
|
|
|
private $idToken = ''; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string refresh token |
|
57
|
|
|
*/ |
|
58
|
|
|
private $refreshToken = ''; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var bool online or offline access token |
|
62
|
|
|
*/ |
|
63
|
|
|
private $online = false; |
|
64
|
|
|
|
|
65
|
|
|
private function __construct() {} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Setter for access token |
|
69
|
|
|
* @param string $accessToken access token |
|
70
|
|
|
* @return Token self |
|
71
|
|
|
*/ |
|
72
|
5 |
|
private function setAccessToken($accessToken) { |
|
73
|
5 |
|
$this->accessToken = (string) $accessToken; |
|
74
|
5 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Getter for access token |
|
79
|
|
|
* @return string access token |
|
80
|
|
|
*/ |
|
81
|
3 |
|
public function getAccessToken() { |
|
82
|
3 |
|
return $this->accessToken; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Setter for access token expires in |
|
87
|
|
|
* @param int $expiresIn access token expires in |
|
88
|
|
|
* @return Token self |
|
89
|
|
|
*/ |
|
90
|
5 |
|
public function setExpiresIn($expiresIn) { |
|
91
|
5 |
|
$this->expiresIn = (int) $expiresIn; |
|
92
|
5 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Getter for access token expires in |
|
97
|
|
|
* @return int access token expires in |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function getExpiresIn() { |
|
100
|
2 |
|
return $this->expiresIn; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Setter for id token |
|
105
|
|
|
* @param string $idToken id token |
|
106
|
|
|
* @return Token self |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function setIdToken($idToken) { |
|
109
|
1 |
|
$this->idToken = (string) $idToken; |
|
110
|
1 |
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Getter for id token |
|
115
|
|
|
* @return string id token |
|
116
|
|
|
*/ |
|
117
|
3 |
|
public function getIdToken() { |
|
118
|
3 |
|
return $this->idToken; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Setter for refresh token |
|
123
|
|
|
* @param string $refreshToken refresh token |
|
124
|
|
|
* @return Token self |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function setRefreshToken($refreshToken) { |
|
127
|
1 |
|
$this->refreshToken = (string) $refreshToken; |
|
128
|
1 |
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Getter for refresh token |
|
133
|
|
|
* @return string refresh token |
|
134
|
|
|
* @throws OutOfBoundsException for online access tokens |
|
135
|
|
|
*/ |
|
136
|
2 |
|
public function getRefreshToken() { |
|
137
|
2 |
|
if ($this->isOnline()) { |
|
138
|
1 |
|
throw new OutOfBoundsException('online tokens has not refresh tokens'); |
|
139
|
|
|
} |
|
140
|
1 |
|
return $this->refreshToken; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Setter for token type |
|
145
|
|
|
* @param string $tokenType token type string |
|
146
|
|
|
* @return Token self |
|
147
|
|
|
*/ |
|
148
|
5 |
|
public function setTokenType($tokenType) { |
|
149
|
|
|
switch ($tokenType) { |
|
150
|
5 |
|
case 'Bearer': |
|
151
|
4 |
|
$this->tokenType = self::TYPE_BEARER; |
|
152
|
4 |
|
break; |
|
153
|
1 |
|
default: |
|
154
|
1 |
|
$this->tokenType = self::TYPE_UNKNOWN; |
|
155
|
1 |
|
} |
|
156
|
5 |
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Getter for token type code |
|
161
|
|
|
* @return int token type code |
|
162
|
|
|
*/ |
|
163
|
3 |
|
public function getTokenType() { |
|
164
|
3 |
|
return $this->tokenType; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Setter for online flag value |
|
169
|
|
|
* @param bool $online online flag value |
|
170
|
|
|
*/ |
|
171
|
5 |
|
private function setOnline($online) { |
|
172
|
5 |
|
$this->online = !!$online; |
|
173
|
5 |
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Getter for online flag |
|
177
|
|
|
* @return bool online flag value |
|
178
|
|
|
*/ |
|
179
|
5 |
|
public function isOnline() { |
|
180
|
5 |
|
return $this->online; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Method for object initialization by the string |
|
185
|
|
|
* @param string $string response string |
|
186
|
|
|
* @return Token response object |
|
187
|
|
|
*/ |
|
188
|
5 |
|
public static function initializeByString($string) { |
|
189
|
5 |
|
$object = json_decode($string); |
|
190
|
5 |
|
$Response = new self(); |
|
191
|
5 |
|
$Response->setAccessToken($object->access_token) |
|
192
|
5 |
|
->setExpiresIn($object->expires_in) |
|
193
|
5 |
|
->setTokenType($object->token_type); |
|
194
|
|
|
|
|
195
|
5 |
|
if (isset($object->id_token)) { |
|
196
|
1 |
|
$Response->setIdToken($object->id_token); |
|
197
|
1 |
|
} |
|
198
|
|
|
|
|
199
|
5 |
|
$Response->setOnline(!isset($object->refresh_token)); |
|
200
|
5 |
|
if (!$Response->isOnline()) { |
|
201
|
1 |
|
$Response->setRefreshToken($object->refresh_token); |
|
202
|
1 |
|
} |
|
203
|
5 |
|
return $Response; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @inheritdoc |
|
208
|
|
|
*/ |
|
209
|
2 |
|
public function __toString() { |
|
210
|
|
|
$format = <<<'EOD' |
|
211
|
|
|
access token: %s |
|
212
|
|
|
expires in: %s |
|
213
|
|
|
id token: %s |
|
214
|
|
|
refresh token: %s |
|
215
|
|
|
token type: %s |
|
216
|
2 |
|
EOD; |
|
217
|
|
|
try { |
|
218
|
2 |
|
$refreshToken = $this->getRefreshToken(); |
|
219
|
2 |
|
} catch (OutOfBoundsException $Ex) { |
|
220
|
1 |
|
$refreshToken = 'not available for online type'; |
|
221
|
|
|
} |
|
222
|
2 |
|
return sprintf($format |
|
223
|
2 |
|
, $this->getAccessToken() |
|
224
|
2 |
|
, $this->getExpiresIn() |
|
225
|
2 |
|
, $this->getIdToken() ?: '-' |
|
226
|
2 |
|
, $refreshToken |
|
227
|
2 |
|
, $this->getTokenType()); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|