-
Notifications
You must be signed in to change notification settings - Fork 744
/
AccessToken.php
executable file
·82 lines (67 loc) · 2.32 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
<?php
namespace League\OAuth2\Client\Token;
use InvalidArgumentException;
class AccessToken
{
/**
* @var string accessToken
*/
public $accessToken;
/**
* @var int expires
*/
public $expires;
/**
* @var string refreshToken
*/
public $refreshToken;
/**
* @var string uid
*/
public $uid;
/**
* Sets the token, expiry, etc values.
*
* @param array $options token options
* @return void
*/
public function __construct(array $options = null)
{
if (! isset($options['access_token'])) {
throw new \InvalidArgumentException(
'Required option not passed: access_token'. PHP_EOL
. print_r($options, true)
);
}
$this->accessToken = $options['access_token'];
// Some providers (not many) give the uid here, so lets take it
isset($options['uid']) and $this->uid = $options['uid'];
// Vkontakte uses user_id instead of uid
isset($options['user_id']) and $this->uid = $options['user_id'];
// Mailru uses x_mailru_vid instead of uid
isset($options['x_mailru_vid']) and $this->uid = $options['x_mailru_vid'];
//Battle.net uses accountId instead of uid
isset($options['accountId']) and $this->uid = $options['accountId'];
// We need to know when the token expires. Show preference to
// 'expires_in' since it is defined in RFC6749 Section 5.1.
// Defer to 'expires' if it is provided instead.
if (!empty($options['expires_in'])) {
$this->expires = time() + ((int) $options['expires_in']);
} elseif (!empty($options['expires'])) {
// Some providers supply the seconds until expiration rather than
// the exact timestamp. Take a best guess at which we received.
$this->expires = ($options['expires'] > time()) ? $options['expires'] : time() + ((int) $options['expires']);
}
// Grab a refresh token so we can update access tokens when they expires
isset($options['refresh_token']) and $this->refreshToken = $options['refresh_token'];
}
/**
* Returns the token key.
*
* @return string
*/
public function __toString()
{
return (string) $this->accessToken;
}
}