1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\Common\Storage; |
4
|
|
|
|
5
|
|
|
use OAuth\Common\Token\TokenInterface; |
6
|
|
|
use OAuth\Common\Storage\Exception\TokenNotFoundException; |
7
|
|
|
use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException; |
8
|
|
|
/* |
9
|
|
|
* Stores a token in a Memcached server. |
10
|
|
|
*/ |
11
|
|
|
class Memcached implements TokenStorageInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $key; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $stateKey; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var object|\Memcached |
25
|
|
|
*/ |
26
|
|
|
protected $memcached; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var object|TokenInterface |
30
|
|
|
*/ |
31
|
|
|
protected $cachedTokens; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var object |
35
|
|
|
*/ |
36
|
|
|
protected $cachedStates; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param \Memcached $memcache An instantiated and connected Memcached client |
40
|
|
|
* @param string $key The key to store the token under in Memcached |
41
|
|
|
* @param string $stateKey The key to store the state under in Memcached. |
42
|
|
|
*/ |
43
|
|
|
public function __construct(\Memcached $memcache, $key, $stateKey) |
44
|
|
|
{ |
45
|
|
|
$this->memcached = $memcache; |
46
|
|
|
$this->key = $key; |
47
|
|
|
$this->stateKey = $stateKey; |
48
|
|
|
$this->cachedTokens = array(); |
|
|
|
|
49
|
|
|
$this->cachedStates = array(); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
|
|
public function retrieveAccessToken($service) |
56
|
|
|
{ |
57
|
|
|
if (!$this->hasAccessToken($service)) { |
58
|
|
|
throw new TokenNotFoundException('Token not found in Memcached'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (isset($this->cachedTokens[$service])) { |
62
|
|
|
return $this->cachedTokens[$service]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$val = unserialize($this->memcached->get($this->key)); |
66
|
|
|
$val = $val[$service]; |
67
|
|
|
|
68
|
|
|
return $this->cachedTokens[$service] = $val; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
*/ |
74
|
|
|
public function storeAccessToken($service, TokenInterface $token) |
75
|
|
|
{ |
76
|
|
|
// (over)write the token |
77
|
|
|
$all_tokens = $this->memcached->get($this->key); |
78
|
|
|
|
79
|
|
|
if ($this->memcached->getResultCode() == \Memcached::RES_NOTFOUND) { |
80
|
|
|
$all_tokens = array(); |
81
|
|
|
} else { |
82
|
|
|
$all_tokens = unserialize($all_tokens); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$all_tokens[$service] = $token; |
86
|
|
|
$this->memcached->set($this->key, serialize($all_tokens)); |
87
|
|
|
|
88
|
|
|
$this->cachedTokens[$service] = $token; |
89
|
|
|
|
90
|
|
|
// allow chaining |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
|
|
public function hasAccessToken($service) |
98
|
|
|
{ |
99
|
|
|
if (isset($this->cachedTokens[$service]) |
100
|
|
|
&& $this->cachedTokens[$service] instanceof TokenInterface |
101
|
|
|
) { |
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$all_tokens = unserialize($this->memcached->get($this->key)); |
106
|
|
|
|
107
|
|
|
return (bool)$all_tokens[$service]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritDoc} |
112
|
|
|
*/ |
113
|
|
|
public function clearToken($service) |
114
|
|
|
{ |
115
|
|
|
$all_tokens = unserialize($this->memcached->get($this->key)); |
116
|
|
|
unset($all_tokens[$service]); |
117
|
|
|
|
118
|
|
|
if (empty($all_tokens)) { |
119
|
|
|
$this->memcached->delete($this->key); |
120
|
|
|
} else { |
121
|
|
|
$this->memcached->set($this->key, serialize($all_tokens)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
unset($this->cachedTokens[$service]); |
125
|
|
|
|
126
|
|
|
// allow chaining |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritDoc} |
132
|
|
|
*/ |
133
|
|
|
public function clearAllTokens() |
134
|
|
|
{ |
135
|
|
|
// memory |
136
|
|
|
$this->cachedTokens = array(); |
|
|
|
|
137
|
|
|
|
138
|
|
|
// memcached |
139
|
|
|
$this->memcached->delete($this->key); |
140
|
|
|
|
141
|
|
|
// allow chaining |
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritDoc} |
147
|
|
|
*/ |
148
|
|
|
public function retrieveAuthorizationState($service) |
149
|
|
|
{ |
150
|
|
|
if (!$this->hasAuthorizationState($service)) { |
151
|
|
|
throw new AuthorizationStateNotFoundException('State not found in Memcached'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (isset($this->cachedStates[$service])) { |
155
|
|
|
return $this->cachedStates[$service]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$val = unserialize($this->memcached->get($this->stateKey)); |
159
|
|
|
$val = $val[$service]; |
160
|
|
|
|
161
|
|
|
return $this->cachedStates[$service] = $val; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritDoc} |
166
|
|
|
*/ |
167
|
|
|
public function storeAuthorizationState($service, $state) |
168
|
|
|
{ |
169
|
|
|
// (over)write the token |
170
|
|
|
$all_states = $this->memcached->get($this->key); |
171
|
|
|
|
172
|
|
|
if ($this->memcached->getResultCode() == \Memcached::RES_NOTFOUND) { |
173
|
|
|
$all_states = array(); |
174
|
|
|
} else { |
175
|
|
|
$all_states = unserialize($all_states); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$all_states[$service] = $state; |
179
|
|
|
$this->memcached->set($this->stateKey, serialize($all_states)); |
180
|
|
|
|
181
|
|
|
// allow chaining |
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritDoc} |
187
|
|
|
*/ |
188
|
|
|
public function hasAuthorizationState($service) |
189
|
|
|
{ |
190
|
|
|
if (isset($this->cachedStates[$service]) |
191
|
|
|
&& null !== $this->cachedStates[$service] |
192
|
|
|
) { |
193
|
|
|
return true; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$all_states = unserialize($this->memcached->get($this->stateKey)); |
197
|
|
|
|
198
|
|
|
return (bool)$all_states[$service]; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritDoc} |
203
|
|
|
*/ |
204
|
|
|
public function clearAuthorizationState($service) |
205
|
|
|
{ |
206
|
|
|
$all_states = unserialize($this->memcached->get($this->stateKey)); |
207
|
|
|
unset($all_states[$service]); |
208
|
|
|
|
209
|
|
|
if (empty($all_states)) { |
210
|
|
|
$this->memcached->delete($this->stateKey); |
211
|
|
|
} else { |
212
|
|
|
$this->memcached->set($this->stateKey, serialize($all_states)); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
unset($this->cachedTokens[$service]); |
216
|
|
|
|
217
|
|
|
// allow chaining |
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* {@inheritDoc} |
223
|
|
|
*/ |
224
|
|
|
public function clearAllAuthorizationStates() |
225
|
|
|
{ |
226
|
|
|
// memory |
227
|
|
|
$this->cachedStates = array(); |
|
|
|
|
228
|
|
|
|
229
|
|
|
// memcached |
230
|
|
|
$this->memcached->delete($this->stateKey); |
231
|
|
|
|
232
|
|
|
// allow chaining |
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return \Memcached $memcached |
238
|
|
|
*/ |
239
|
|
|
public function getMemcached() |
240
|
|
|
{ |
241
|
|
|
return $this->memcached; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return string $key |
246
|
|
|
*/ |
247
|
|
|
public function getKey() |
248
|
|
|
{ |
249
|
|
|
return $this->key; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..