1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\AuthClient; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Token represents OAuth token. |
9
|
|
|
*/ |
10
|
|
|
final class OAuthToken |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string key in {@see params} array, which stores token key. |
14
|
|
|
*/ |
15
|
|
|
private string $tokenParamKey = 'oauth_token'; |
16
|
|
|
/** |
17
|
|
|
* @var string key in {@see params} array, which stores token secret key. |
18
|
|
|
*/ |
19
|
|
|
private string $tokenSecretParamKey = 'oauth_token_secret'; |
20
|
|
|
/** |
21
|
|
|
* @var int object creation timestamp. |
22
|
|
|
*/ |
23
|
|
|
private int $createTimestamp; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string|null key in {@see params} array, which stores token expiration duration. |
27
|
|
|
* If not set will attempt to fetch its value automatically. |
28
|
|
|
*/ |
29
|
|
|
private ?string $expireDurationParamKey = null; |
30
|
|
|
/** |
31
|
|
|
* @var array token parameters. |
32
|
|
|
*/ |
33
|
|
|
private array $params = []; |
34
|
|
|
|
35
|
9 |
|
public function __construct() |
36
|
|
|
{ |
37
|
9 |
|
$this->createTimestamp = time(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Sets token value. |
42
|
|
|
* |
43
|
|
|
* @param string $token token value. |
44
|
|
|
*/ |
45
|
3 |
|
public function setToken(string $token): void |
46
|
|
|
{ |
47
|
3 |
|
$this->setParam($this->tokenParamKey, $token); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Sets param by name. |
52
|
|
|
* |
53
|
|
|
* @param string $name param name. |
54
|
|
|
* @param mixed $value param value, |
55
|
|
|
*/ |
56
|
5 |
|
public function setParam(string $name, $value): void |
57
|
|
|
{ |
58
|
5 |
|
$this->params[$name] = $value; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Sets the token secret value. |
63
|
|
|
* |
64
|
|
|
* @param string $tokenSecret token secret. |
65
|
|
|
*/ |
66
|
1 |
|
public function setTokenSecret(string $tokenSecret): void |
67
|
|
|
{ |
68
|
1 |
|
$this->setParam($this->tokenSecretParamKey, $tokenSecret); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns the token secret value. |
73
|
|
|
* |
74
|
|
|
* @return string token secret value. |
75
|
|
|
*/ |
76
|
1 |
|
public function getTokenSecret(): string |
77
|
|
|
{ |
78
|
1 |
|
return $this->getParam($this->tokenSecretParamKey); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns param by name. |
83
|
|
|
* |
84
|
|
|
* @param string $name param name. |
85
|
|
|
* |
86
|
|
|
* @return mixed param value. |
87
|
|
|
*/ |
88
|
9 |
|
public function getParam(string $name) |
89
|
|
|
{ |
90
|
9 |
|
return $this->params[$name] ?? null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sets token expire duration. |
95
|
|
|
* |
96
|
|
|
* @param int $expireDuration token expiration duration. |
97
|
|
|
*/ |
98
|
3 |
|
public function setExpireDuration(int $expireDuration): void |
99
|
|
|
{ |
100
|
3 |
|
$this->setParam($this->getExpireDurationParamKey(), $expireDuration); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return string expire duration param key. |
105
|
|
|
*/ |
106
|
7 |
|
public function getExpireDurationParamKey(): string |
107
|
|
|
{ |
108
|
7 |
|
if ($this->expireDurationParamKey === null) { |
109
|
7 |
|
$this->expireDurationParamKey = $this->defaultExpireDurationParamKey(); |
110
|
|
|
} |
111
|
|
|
|
112
|
7 |
|
return $this->expireDurationParamKey; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $expireDurationParamKey expire duration param key. |
117
|
|
|
*/ |
118
|
|
|
public function setExpireDurationParamKey(string $expireDurationParamKey): void |
119
|
|
|
{ |
120
|
|
|
$this->expireDurationParamKey = $expireDurationParamKey; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Fetches default expire duration param key. |
125
|
|
|
* |
126
|
|
|
* @return string expire duration param key. |
127
|
|
|
*/ |
128
|
7 |
|
protected function defaultExpireDurationParamKey(): string |
129
|
|
|
{ |
130
|
7 |
|
$expireDurationParamKey = 'expires_in'; |
131
|
7 |
|
foreach ($this->getParams() as $name => $value) { |
132
|
5 |
|
if (strpos($name, 'expir') !== false) { |
133
|
4 |
|
$expireDurationParamKey = $name; |
134
|
4 |
|
break; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
7 |
|
return $expireDurationParamKey; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
8 |
|
public function getParams(): array |
145
|
|
|
{ |
146
|
8 |
|
return $this->params; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param array $params |
151
|
|
|
*/ |
152
|
5 |
|
public function setParams(array $params): void |
153
|
|
|
{ |
154
|
5 |
|
$this->params = $params; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Checks if token is valid. |
159
|
|
|
* |
160
|
|
|
* @return bool is token valid. |
161
|
|
|
*/ |
162
|
1 |
|
public function getIsValid(): bool |
163
|
|
|
{ |
164
|
1 |
|
$token = $this->getToken(); |
165
|
|
|
|
166
|
1 |
|
return !empty($token) && !$this->getIsExpired(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns token value. |
171
|
|
|
* |
172
|
|
|
* @return string token value. |
173
|
|
|
*/ |
174
|
3 |
|
public function getToken(): ?string |
175
|
|
|
{ |
176
|
3 |
|
return $this->getParam($this->tokenParamKey); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Checks if token has expired. |
181
|
|
|
* |
182
|
|
|
* @return bool is token expired. |
183
|
|
|
*/ |
184
|
2 |
|
public function getIsExpired(): bool |
185
|
|
|
{ |
186
|
2 |
|
$expirationDuration = $this->getExpireDuration(); |
187
|
2 |
|
if ($expirationDuration === null) { |
188
|
|
|
return false; |
189
|
|
|
} |
190
|
|
|
|
191
|
2 |
|
return time() >= ($this->createTimestamp + $expirationDuration); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Returns the token expiration duration. |
196
|
|
|
* |
197
|
|
|
* @return int|null token expiration duration. |
198
|
|
|
*/ |
199
|
7 |
|
public function getExpireDuration(): ?int |
200
|
|
|
{ |
201
|
7 |
|
return $this->getParam($this->getExpireDurationParamKey()); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function getCreateTimestamp(): int |
205
|
|
|
{ |
206
|
|
|
return $this->createTimestamp; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|