-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathAccessToken.php
263 lines (236 loc) · 5.92 KB
/
AccessToken.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/**
* linkedin-client
* AccessToken.php
*
* PHP Version 5
*
* @category Production
* @package Default
* @author Philipp Tkachev <[email protected]>
* @date 8/17/17 22:55
* @license http://www.zoonman.com/projects/linkedin-client/license.txt linkedin-client License
* @version GIT: 1.0
* @link http://www.zoonman.com/projects/linkedin-client/
*/
namespace LinkedIn;
/**
* Class AccessToken
*
* @package LinkedIn
*/
class AccessToken implements \JsonSerializable
{
/**
* @var string
*/
protected $token;
/**
* When token will expire.
*
* Please, pay attention that LinkedIn API always returns "expires in" time,
* which is amount of seconds before token will expire since now.
* If you are going to store token somewhere, you have to keep "expires at"
* or two values - "expires in" and "token created".
* Using "expires at" approach lets you have efficient queries to find
* tokens will soon expire and be proactive with regards to your
* B2C communication.
*
* @var int
*/
protected $expiresAt;
/**
* Get token string
*
* @return string
*/
/**
* @var string
*/
protected $refreshToken;
/**
* @var int
*/
protected $refreshTokenExpiresAt;
public function getToken()
{
return $this->token;
}
public function getRefreshToken()
{
return $this->refreshToken;
}
/**
* Set token string
*
* @param string $token
*
* @return AccessToken
*/
public function setToken($token)
{
$this->token = $token;
return $this;
}
/**
* Set refresh token string
*
* @param string $refreshToken
*
* @return AccessToken
*/
public function setRefreshToken($refreshToken)
{
$this->refreshToken = $refreshToken;
return $this;
}
/**
* The number of seconds remaining, from the time it was requested, before the token will expire.
*
* @return int seconds
*/
public function getExpiresIn()
{
return $this->expiresAt - time();
}
/**
* AccessToken constructor.
*
* @param string $token
* @param string $token
* @param int $expiresAt
*/
public function __construct($token = '', $expiresAt = 0,$refreshToken = '', $refreshTokenExpiresAt = 0 )
{
$this->setToken($token);
$this->setExpiresAt($expiresAt);
$this->setRefreshToken($refreshToken);
$this->setRefreshTokenExpiresAt($refreshTokenExpiresAt);
}
/**
* Set token expiration time
*
* @param int $expiresIn amount of seconds before expiration
*
* @return AccessToken
*/
public function setExpiresIn($expiresIn)
{
$this->expiresAt = $expiresIn + time();
return $this;
}
/**
* Dynamically typecast token object into string
*
* @return string
*/
public function __toString()
{
return $this->getToken();
}
/**
* Get Unix epoch time when token will expire
*
* @return int
*/
public function getExpiresAt()
{
return $this->expiresAt;
}
/**
* Get Unix epoch time when refresh token will expire
*
* @return int
*/
public function getRefreshTokenExpiresAt()
{
return $this->refreshTokenExpiresAt;
}
/**
* Set Unix epoch time when token will expire
*
* @param int $expiresAt seconds, unix time
*
* @return AccessToken
*/
public function setExpiresAt($expiresAt)
{
$this->expiresAt = $expiresAt;
}
/**
* Set Unix epoch time when token will expire
*
* @param int $refreshTokenExpiresAt seconds, unix time
*
*/
public function setRefreshTokenExpiresAt($refreshTokenExpiresAt)
{
$this->refreshTokenExpiresAt = $refreshTokenExpiresAt;
}
/**
* Convert API response into AccessToken
*
* @param \Psr\Http\Message\ResponseInterface $response
*
* @return self
*/
public static function fromResponse($response)
{
return static::fromResponseArray(
Client::responseToArray($response)
);
}
/**
* Instantiate access token object
*
* @param $responseArray
*
* @return \LinkedIn\AccessToken
*/
public static function fromResponseArray($responseArray)
{
if (!is_array($responseArray)) {
throw new \InvalidArgumentException(
'Argument is not array'
);
}
if (!isset($responseArray['access_token'])) {
throw new \InvalidArgumentException(
'Access token is not available'
);
}
if (!isset($responseArray['expires_in'])) {
throw new \InvalidArgumentException(
'Access token expiration date is not specified'
);
}
if (!isset($responseArray['refresh_token'])) {
throw new \InvalidArgumentException(
'Refresh token is not available'
);
}
if (!isset($responseArray['refresh_token_expires_in'])) {
throw new \InvalidArgumentException(
'Refresh token expiration date is not specified'
);
}
return new static(
$responseArray['access_token'],
$responseArray['expires_in'] + time(),
$responseArray['refresh_token'],
$responseArray['refresh_token_expires_in'] + time()
);
}
/**
* Specify data format for json_encode()
*/
public function jsonSerialize()
{
return [
'token' => $this->getToken(),
'expiresAt' => $this->getExpiresAt(),
'refreshToken' => $this->getRefreshToken(),
'refreshTokenExpiresAt' => $this->getRefreshTokenExpiresAt()
];
}
}