-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccessToken.php
130 lines (111 loc) · 2.99 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
<?php
/**
* SocialConnect project
* @author: Patsura Dmitry https://github.com/ovr <talk@dmtry.me>
*/
declare(strict_types=1);
namespace SocialConnect\OAuth2;
use SocialConnect\Provider\AccessTokenInterface;
use SocialConnect\Provider\Exception\InvalidAccessToken;
class AccessToken implements AccessTokenInterface
{
/**
* @var string
*/
protected $token;
/**
* @var string|null
*/
protected $refreshToken;
/**
* @var int|null
*/
protected $expires;
/**
* @var string|null
*/
protected $uid;
/**
* @param array $token
* @throws InvalidAccessToken
*/
public function __construct(array $token)
{
if (!isset($token['access_token'])) {
throw new InvalidAccessToken(
'API returned data without access_token field'
);
}
$this->token = $token['access_token'];
// Show preference to 'expires_in' since it is defined in RFC6749 Section 5.1.
// Defer to 'expires' if it is provided instead.
if (isset($token['expires_in'])) {
if (!is_numeric($token['expires_in'])) {
throw new InvalidAccessToken('expires_in value must be an integer');
}
$this->expires = $token['expires_in'] != 0 ? time() + $token['expires_in'] : 0;
} elseif (!empty($token['expires'])) {
// Some providers supply the seconds until expiration rather than
// the exact timestamp. Take a best guess at which we received.
$expires = $token['expires'];
if (!$this->isExpirationTimestamp($expires)) {
$expires += time();
}
$this->expires = $expires;
}
if (isset($token['user_id'])) {
$this->uid = (string) $token['user_id'];
}
if (isset($token['refresh_token'])) {
$this->refreshToken = $token['refresh_token'];
}
}
/**
* Check if a value is an expiration timestamp or second value.
*
* @param integer $value
* @return bool
*/
protected function isExpirationTimestamp($value)
{
// If the given value is larger than the original OAuth 2 draft date,
// assume that it is meant to be a (possible expired) timestamp.
$oauth2InceptionDate = 1349067600; // 2012-10-01
return ($value > $oauth2InceptionDate);
}
/**
* @return string
*/
public function getToken()
{
return $this->token;
}
/**
* @param string $uid
*/
public function setUserId(string $uid)
{
$this->uid = $uid;
}
/**
* @return string|null
*/
public function getUserId()
{
return $this->uid;
}
/**
* @return int|null
*/
public function getExpires()
{
return $this->expires;
}
/**
* @return string|null
*/
public function getRefreshToken(): ?string
{
return $this->refreshToken;
}
}