@@ -208,6 +208,7 @@ the ``PasswordDigest`` header value matches with the user's password.
208
208
// src/AppBundle/Security/Authentication/Provider/WsseProvider.php
209
209
namespace AppBundle\Security\Authentication\Provider;
210
210
211
+ use Psr\Cache\CacheItemPoolInterface;
211
212
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
212
213
use Symfony\Component\Security\Core\User\UserProviderInterface;
213
214
use Symfony\Component\Security\Core\Exception\AuthenticationException;
@@ -218,12 +219,12 @@ the ``PasswordDigest`` header value matches with the user's password.
218
219
class WsseProvider implements AuthenticationProviderInterface
219
220
{
220
221
private $userProvider;
221
- private $cacheDir ;
222
+ private $cachePool ;
222
223
223
- public function __construct(UserProviderInterface $userProvider, $cacheDir )
224
+ public function __construct(UserProviderInterface $userProvider, CacheItemPoolInterface $cachePool )
224
225
{
225
226
$this->userProvider = $userProvider;
226
- $this->cacheDir = $cacheDir ;
227
+ $this->cachePool = $cachePool ;
227
228
}
228
229
229
230
public function authenticate(TokenInterface $token)
@@ -258,19 +259,18 @@ the ``PasswordDigest`` header value matches with the user's password.
258
259
return false;
259
260
}
260
261
261
- // Validate that the nonce is *not* used in the last 5 minutes
262
- // if it has, this could be a replay attack
263
- if (
264
- file_exists($this->cacheDir.'/'.md5($ nonce))
265
- && file_get_contents($ this->cacheDir.'/'.md5($nonce)) + 300 > time()
266
- ) {
262
+ // Try to fetch the cache item from pool
263
+ $cacheItem = $ this->cachePool->getItem(md5($nonce));
264
+
265
+ // Validate that the nonce is *not* in cache
266
+ // if it is, this could be a replay attack
267
+ if ($cacheItem->isHit() ) {
267
268
throw new NonceExpiredException('Previously used nonce detected');
268
269
}
269
- // If cache directory does not exist we create it
270
- if (!is_dir($this->cacheDir)) {
271
- mkdir($this->cacheDir, 0777, true);
272
- }
273
- file_put_contents($this->cacheDir.'/'.md5($nonce), time());
270
+
271
+ // Store the item in cache for 5 minutes
272
+ $cacheItem->set(null)->expiresAfter(300);
273
+ $this->cachePool->save($cacheItem);
274
274
275
275
// Validate Secret
276
276
$expected = base64_encode(sha1(base64_decode($nonce).$created.$secret, true));
@@ -411,7 +411,7 @@ to service ids that do not exist yet: ``wsse.security.authentication.provider``
411
411
class : AppBundle\Security\Authentication\Provider\WsseProvider
412
412
arguments :
413
413
- ' ' # User Provider
414
- - ' %kernel.cache_dir%/security/nonces '
414
+ - ' @cache.app '
415
415
public : false
416
416
417
417
wsse.security.authentication.listener :
@@ -433,7 +433,7 @@ to service ids that do not exist yet: ``wsse.security.authentication.provider``
433
433
public =" false"
434
434
>
435
435
<argument /> <!-- User Provider -->
436
- <argument >%kernel.cache_dir%/security/nonces </argument >
436
+ <argument type = " service " id = " cache.app " > </argument >
437
437
</service >
438
438
439
439
<service id =" wsse.security.authentication.listener"
@@ -456,7 +456,7 @@ to service ids that do not exist yet: ``wsse.security.authentication.provider``
456
456
'AppBundle\Security\Authentication\Provider\WsseProvider',
457
457
array(
458
458
'', // User Provider
459
- '%kernel.cache_dir%/security/nonces' ,
459
+ new Reference('cache.app') ,
460
460
)
461
461
);
462
462
$definition->setPublic(false);
0 commit comments