Skip to content

Commit f19328b

Browse files
committed
feature #6617 [WSSE] - Using a PSR6 cache instead of file cache (Nyholm)
This PR was submitted for the 3.0 branch but it was merged into the 3.1 branch instead (closes #6617). Discussion ---------- [WSSE] - Using a PSR6 cache instead of file cache I rewrote the WSSE example to use a PSR-6 cache instead. I did this because Symfony got a fancy new CacheComponent now and because using file cache will eventually fill up your inodes. (I just removed 2 million inodes from my server.) Note: Im not sure about the service name for a cache pool. Is it just `cache`? Commits ------- 1d6e267 [WSSE] - Using a PSR6 cache instead of file cache
2 parents 57f7ca3 + 1d6e267 commit f19328b

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

cookbook/security/custom_authentication_provider.rst

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ the ``PasswordDigest`` header value matches with the user's password.
208208
// src/AppBundle/Security/Authentication/Provider/WsseProvider.php
209209
namespace AppBundle\Security\Authentication\Provider;
210210
211+
use Psr\Cache\CacheItemPoolInterface;
211212
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
212213
use Symfony\Component\Security\Core\User\UserProviderInterface;
213214
use Symfony\Component\Security\Core\Exception\AuthenticationException;
@@ -218,12 +219,12 @@ the ``PasswordDigest`` header value matches with the user's password.
218219
class WsseProvider implements AuthenticationProviderInterface
219220
{
220221
private $userProvider;
221-
private $cacheDir;
222+
private $cachePool;
222223
223-
public function __construct(UserProviderInterface $userProvider, $cacheDir)
224+
public function __construct(UserProviderInterface $userProvider, CacheItemPoolInterface $cachePool)
224225
{
225226
$this->userProvider = $userProvider;
226-
$this->cacheDir = $cacheDir;
227+
$this->cachePool = $cachePool;
227228
}
228229
229230
public function authenticate(TokenInterface $token)
@@ -258,19 +259,18 @@ the ``PasswordDigest`` header value matches with the user's password.
258259
return false;
259260
}
260261
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()) {
267268
throw new NonceExpiredException('Previously used nonce detected');
268269
}
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);
274274
275275
// Validate Secret
276276
$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``
411411
class: AppBundle\Security\Authentication\Provider\WsseProvider
412412
arguments:
413413
- '' # User Provider
414-
- '%kernel.cache_dir%/security/nonces'
414+
- '@cache.app'
415415
public: false
416416
417417
wsse.security.authentication.listener:
@@ -433,7 +433,7 @@ to service ids that do not exist yet: ``wsse.security.authentication.provider``
433433
public="false"
434434
>
435435
<argument /> <!-- User Provider -->
436-
<argument>%kernel.cache_dir%/security/nonces</argument>
436+
<argument type="service" id="cache.app"></argument>
437437
</service>
438438
439439
<service id="wsse.security.authentication.listener"
@@ -456,7 +456,7 @@ to service ids that do not exist yet: ``wsse.security.authentication.provider``
456456
'AppBundle\Security\Authentication\Provider\WsseProvider',
457457
array(
458458
'', // User Provider
459-
'%kernel.cache_dir%/security/nonces',
459+
new Reference('cache.app'),
460460
)
461461
);
462462
$definition->setPublic(false);

0 commit comments

Comments
 (0)