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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
<?php
namespace OAuth\Common\Storage;
use OAuth\Common\Token\TokenInterface;
use OAuth\Common\Storage\Exception\TokenNotFoundException;
use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException;
use Predis\Client as Predis;
/*
* Stores a token in a Redis server. Requires the Predis library available at https://github.com/nrk/predis
*/
class Redis implements TokenStorageInterface
{
/**
* @var string
*/
protected $key;
protected $stateKey;
/**
* @var object|\Redis
*/
protected $redis;
/**
* @var object|TokenInterface
*/
protected $cachedTokens;
/**
* @var object
*/
protected $cachedStates;
/**
* @param Predis $redis An instantiated and connected redis client
* @param string $key The key to store the token under in redis
* @param string $stateKey The key to store the state under in redis.
*/
public function __construct(Predis $redis, $key, $stateKey)
{
$this->redis = $redis;
$this->key = $key;
$this->stateKey = $stateKey;
$this->cachedTokens = array();
$this->cachedStates = array();
}
/**
* {@inheritDoc}
*/
public function retrieveAccessToken($service)
{
if (!$this->hasAccessToken($service)) {
throw new TokenNotFoundException('Token not found in redis');
}
if (isset($this->cachedTokens[$service])) {
return $this->cachedTokens[$service];
}
$val = $this->redis->hget($this->key, $service);
return $this->cachedTokens[$service] = unserialize($val);
}
/**
* {@inheritDoc}
*/
public function storeAccessToken($service, TokenInterface $token)
{
// (over)write the token
$this->redis->hset($this->key, $service, serialize($token));
$this->cachedTokens[$service] = $token;
// allow chaining
return $this;
}
/**
* {@inheritDoc}
*/
public function hasAccessToken($service)
{
if (isset($this->cachedTokens[$service])
&& $this->cachedTokens[$service] instanceof TokenInterface
) {
return true;
}
return $this->redis->hexists($this->key, $service);
}
/**
* {@inheritDoc}
*/
public function clearToken($service)
{
$this->redis->hdel($this->key, $service);
unset($this->cachedTokens[$service]);
// allow chaining
return $this;
}
/**
* {@inheritDoc}
*/
public function clearAllTokens()
{
// memory
$this->cachedTokens = array();
// redis
$keys = $this->redis->hkeys($this->key);
$me = $this; // 5.3 compat
// pipeline for performance
$this->redis->pipeline(
function ($pipe) use ($keys, $me) {
foreach ($keys as $k) {
$pipe->hdel($me->getKey(), $k);
}
}
);
// allow chaining
return $this;
}
/**
* {@inheritDoc}
*/
public function retrieveAuthorizationState($service)
{
if (!$this->hasAuthorizationState($service)) {
throw new AuthorizationStateNotFoundException('State not found in redis');
}
if (isset($this->cachedStates[$service])) {
return $this->cachedStates[$service];
}
$val = $this->redis->hget($this->stateKey, $service);
return $this->cachedStates[$service] = unserialize($val);
}
/**
* {@inheritDoc}
*/
public function storeAuthorizationState($service, $state)
{
// (over)write the token
$this->redis->hset($this->stateKey, $service, $state);
$this->cachedStates[$service] = $state;
// allow chaining
return $this;
}
/**
* {@inheritDoc}
*/
public function hasAuthorizationState($service)
{
if (isset($this->cachedStates[$service])
&& null !== $this->cachedStates[$service]
) {
return true;
}
return $this->redis->hexists($this->stateKey, $service);
}
/**
* {@inheritDoc}
*/
public function clearAuthorizationState($service)
{
$this->redis->hdel($this->stateKey, $service);
unset($this->cachedStates[$service]);
// allow chaining
return $this;
}
/**
* {@inheritDoc}
*/
public function clearAllAuthorizationStates()
{
// memory
$this->cachedStates = array();
// redis
$keys = $this->redis->hkeys($this->stateKey);
$me = $this; // 5.3 compat
// pipeline for performance
$this->redis->pipeline(
function ($pipe) use ($keys, $me) {
foreach ($keys as $k) {
$pipe->hdel($me->getKey(), $k);
}
}
);
// allow chaining
return $this;
}
/**
* @return Predis $redis
*/
public function getRedis()
{
return $this->redis;
}
/**
* @return string $key
*/
public function getKey()
{
return $this->key;
}
}
|