Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
Redis.php
001 <?php
002
003 namespace OAuth\Common\Storage;
004
005 use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException;
006 use OAuth\Common\Storage\Exception\TokenNotFoundException;
007 use OAuth\Common\Token\TokenInterface;
008 use Predis\Client as Predis;
009
010 // Stores a token in a Redis server. Requires the Predis library available at https://github.com/nrk/predis
011 class Redis implements TokenStorageInterface
012 {
013 /**
014 * @var string
015 */
016 protected $key;
017
018 protected $stateKey;
019
020 /**
021 * @var object|\Redis
022 */
023 protected $redis;
024
025 /**
026 * @var object|TokenInterface
027 */
028 protected $cachedTokens;
029
030 /**
031 * @var object
032 */
033 protected $cachedStates;
034
035 /**
036 * @param Predis $redis An instantiated and connected redis client
037 * @param string $key The key to store the token under in redis
038 * @param string $stateKey the key to store the state under in redis
039 */
040 public function __construct(Predis $redis, $key, $stateKey)
041 {
042 $this->redis = $redis;
043 $this->key = $key;
044 $this->stateKey = $stateKey;
045 $this->cachedTokens = [];
046 $this->cachedStates = [];
047 }
048
049 /**
050 * {@inheritdoc}
051 */
052 public function retrieveAccessToken($service)
053 {
054 if (!$this->hasAccessToken($service)) {
055 throw new TokenNotFoundException('Token not found in redis');
056 }
057
058 if (isset($this->cachedTokens[$service])) {
059 return $this->cachedTokens[$service];
060 }
061
062 $val = $this->redis->hget($this->key, $service);
063
064 return $this->cachedTokens[$service] = unserialize($val);
065 }
066
067 /**
068 * {@inheritdoc}
069 */
070 public function storeAccessToken($service, TokenInterface $token)
071 {
072 // (over)write the token
073 $this->redis->hset($this->key, $service, serialize($token));
074 $this->cachedTokens[$service] = $token;
075
076 // allow chaining
077 return $this;
078 }
079
080 /**
081 * {@inheritdoc}
082 */
083 public function hasAccessToken($service)
084 {
085 if (isset($this->cachedTokens[$service])
086 && $this->cachedTokens[$service] instanceof TokenInterface
087 ) {
088 return true;
089 }
090
091 return $this->redis->hexists($this->key, $service);
092 }
093
094 /**
095 * {@inheritdoc}
096 */
097 public function clearToken($service)
098 {
099 $this->redis->hdel($this->key, $service);
100 unset($this->cachedTokens[$service]);
101
102 // allow chaining
103 return $this;
104 }
105
106 /**
107 * {@inheritdoc}
108 */
109 public function clearAllTokens()
110 {
111 // memory
112 $this->cachedTokens = [];
113
114 // redis
115 $keys = $this->redis->hkeys($this->key);
116 $me = $this; // 5.3 compat
117
118 // pipeline for performance
119 $this->redis->pipeline(
120 function ($pipe) use ($keys, $me): void {
121 foreach ($keys as $k) {
122 $pipe->hdel($me->getKey(), $k);
123 }
124 }
125 );
126
127 // allow chaining
128 return $this;
129 }
130
131 /**
132 * {@inheritdoc}
133 */
134 public function retrieveAuthorizationState($service)
135 {
136 if (!$this->hasAuthorizationState($service)) {
137 throw new AuthorizationStateNotFoundException('State not found in redis');
138 }
139
140 if (isset($this->cachedStates[$service])) {
141 return $this->cachedStates[$service];
142 }
143
144 $val = $this->redis->hget($this->stateKey, $service);
145
146 return $this->cachedStates[$service] = $val;
147 }
148
149 /**
150 * {@inheritdoc}
151 */
152 public function storeAuthorizationState($service, $state)
153 {
154 // (over)write the token
155 $this->redis->hset($this->stateKey, $service, $state);
156 $this->cachedStates[$service] = $state;
157
158 // allow chaining
159 return $this;
160 }
161
162 /**
163 * {@inheritdoc}
164 */
165 public function hasAuthorizationState($service)
166 {
167 if (isset($this->cachedStates[$service])
168 && null !== $this->cachedStates[$service]
169 ) {
170 return true;
171 }
172
173 return $this->redis->hexists($this->stateKey, $service);
174 }
175
176 /**
177 * {@inheritdoc}
178 */
179 public function clearAuthorizationState($service)
180 {
181 $this->redis->hdel($this->stateKey, $service);
182 unset($this->cachedStates[$service]);
183
184 // allow chaining
185 return $this;
186 }
187
188 /**
189 * {@inheritdoc}
190 */
191 public function clearAllAuthorizationStates()
192 {
193 // memory
194 $this->cachedStates = [];
195
196 // redis
197 $keys = $this->redis->hkeys($this->stateKey);
198 $me = $this; // 5.3 compat
199
200 // pipeline for performance
201 $this->redis->pipeline(
202 function ($pipe) use ($keys, $me): void {
203 foreach ($keys as $k) {
204 $pipe->hdel($me->getKey(), $k);
205 }
206 }
207 );
208
209 // allow chaining
210 return $this;
211 }
212
213 /**
214 * @return Predis $redis
215 */
216 public function getRedis()
217 {
218 return $this->redis;
219 }
220
221 /**
222 * @return string $key
223 */
224 public function getKey()
225 {
226 return $this->key;
227 }
228 }
229