1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SocialConnect project |
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace SocialConnect\OAuth2; |
9
|
|
|
|
10
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
11
|
|
|
use SocialConnect\Provider\Exception\InvalidAccessToken; |
12
|
|
|
|
13
|
|
|
class AccessToken implements AccessTokenInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $token; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string|null |
22
|
|
|
*/ |
23
|
|
|
protected $refreshToken; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int|null |
27
|
|
|
*/ |
28
|
|
|
protected $expires; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string|null |
32
|
|
|
*/ |
33
|
|
|
protected $uid; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string|null |
37
|
|
|
*/ |
38
|
|
|
protected $email; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $token |
42
|
|
|
* @throws InvalidAccessToken |
43
|
|
|
*/ |
44
|
103 |
|
public function __construct(array $token) |
45
|
|
|
{ |
46
|
103 |
|
if (!isset($token['access_token'])) { |
47
|
1 |
|
throw new InvalidAccessToken( |
48
|
|
|
'API returned data without access_token field' |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
102 |
|
$this->token = $token['access_token']; |
53
|
|
|
|
54
|
|
|
// Show preference to 'expires_in' since it is defined in RFC6749 Section 5.1. |
55
|
|
|
// Defer to 'expires' if it is provided instead. |
56
|
102 |
|
if (isset($token['expires_in'])) { |
57
|
1 |
|
if (!is_numeric($token['expires_in'])) { |
58
|
|
|
throw new InvalidAccessToken('expires_in value must be an integer'); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
$this->expires = $token['expires_in'] != 0 ? time() + $token['expires_in'] : 0; |
62
|
101 |
|
} elseif (!empty($token['expires'])) { |
63
|
|
|
// Some providers supply the seconds until expiration rather than |
64
|
|
|
// the exact timestamp. Take a best guess at which we received. |
65
|
3 |
|
$expires = $token['expires']; |
66
|
3 |
|
if (!$this->isExpirationTimestamp($expires)) { |
67
|
|
|
$expires += time(); |
68
|
|
|
} |
69
|
|
|
|
70
|
3 |
|
$this->expires = $expires; |
71
|
|
|
} |
72
|
|
|
|
73
|
102 |
|
if (isset($token['user_id'])) { |
74
|
25 |
|
$this->uid = (string) $token['user_id']; |
75
|
|
|
} |
76
|
|
|
|
77
|
102 |
|
if (isset($token['refresh_token'])) { |
78
|
|
|
$this->refreshToken = $token['refresh_token']; |
79
|
|
|
} |
80
|
|
|
|
81
|
102 |
|
if (isset($token['email'])) { |
82
|
|
|
$this->email = $token['email']; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Check if a value is an expiration timestamp or second value. |
88
|
|
|
* |
89
|
|
|
* @param integer $value |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
3 |
|
protected function isExpirationTimestamp($value) |
93
|
|
|
{ |
94
|
|
|
// If the given value is larger than the original OAuth 2 draft date, |
95
|
|
|
// assume that it is meant to be a (possible expired) timestamp. |
96
|
3 |
|
$oauth2InceptionDate = 1349067600; // 2012-10-01 |
97
|
3 |
|
return ($value > $oauth2InceptionDate); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
100 |
|
public function getToken() |
104
|
|
|
{ |
105
|
100 |
|
return $this->token; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $uid |
110
|
|
|
*/ |
111
|
3 |
|
public function setUserId(string $uid) |
112
|
|
|
{ |
113
|
3 |
|
$this->uid = $uid; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $email |
118
|
|
|
*/ |
119
|
|
|
public function setEmail(string $email) |
120
|
|
|
{ |
121
|
|
|
$this->email = $email; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return string|null |
126
|
|
|
*/ |
127
|
31 |
|
public function getUserId() |
128
|
|
|
{ |
129
|
31 |
|
return $this->uid; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return int|null |
134
|
|
|
*/ |
135
|
4 |
|
public function getExpires() |
136
|
|
|
{ |
137
|
4 |
|
return $this->expires; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string|null |
142
|
|
|
*/ |
143
|
|
|
public function getRefreshToken(): ?string |
144
|
|
|
{ |
145
|
|
|
return $this->refreshToken; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return string|null |
150
|
|
|
*/ |
151
|
|
|
public function getEmail(): ?string |
152
|
|
|
{ |
153
|
|
|
return $this->email; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|