Skip to content

Commit

Permalink
create cache functions (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Coutadeur committed Sep 13, 2024
1 parent 2fc2bef commit 79b161b
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"require": {
"php": ">=7.4",
"ext-ldap": ">=7.4",
"phpmailer/phpmailer": "^6.5.0"
"phpmailer/phpmailer": "^6.5.0",
"symfony/cache": "v5.4.42"
},
"require-dev": {
"phpunit/phpunit": ">=8",
Expand Down
66 changes: 66 additions & 0 deletions src/Ltb/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php namespace Ltb;

use Symfony\Component\Cache\Adapter\FilesystemAdapter;

class Cache {

// symfony cache instance
public $sspCache = null;

public function __construct(
$namespace = 'sspCache',
$defaultLifetime = 0,
$directory = null
)
{

$this->sspCache = new FilesystemAdapter(
$namespace,
$defaultLifetime,
$directory
);

// Clean cache from expired entries
$this->sspCache->prune();

}

# Generate a cache entry containing a token,
# expiring after $cache_form_expiration seconds
function generate_form_token($cache_form_expiration)
{
$formtoken = hash('sha256', bin2hex(random_bytes(16)));
$cachedToken = $this->sspCache->getItem($formtoken);
$cachedToken->set($formtoken);
$cachedToken->expiresAfter($cache_form_expiration);
$this->sspCache->save($cachedToken);
error_log("generated form token: " .
$formtoken .
" valid for $cache_form_expiration s");
return $formtoken;
}

# Verify that give token exist in cache
# and if it exists, remove it from cache
function verify_form_token($formtoken)
{
$result = "";
$cachedToken = $this->sspCache->getItem($formtoken);
if( $cachedToken->isHit() && $cachedToken->get() == $formtoken )
{
# Remove token from cache entry
$this->sspCache->deleteItem($formtoken);
}
else
{
error_log("Invalid form token: sent: $formtoken, stored: " .
$cachedToken->get());
$result = "invalidformtoken";
}
return $result;
}


}

?>
133 changes: 133 additions & 0 deletions tests/Ltb/CacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

require __DIR__ . '/../../vendor/autoload.php';

final class CacheTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
{

public function test_construct(): void
{
$cacheInstance = new \Ltb\Cache(
"testCache",
0,
null
);
$this->assertTrue($cacheInstance->sspCache instanceof Symfony\Component\Cache\Adapter\FilesystemAdapter, "Error while initializing sspCache object");
}


public function test_generate_form_token(): void
{

$cacheInstance = new \Ltb\Cache(
"testCache",
0,
null
);

$generated_token = "";

$cacheInstance->sspCache = Mockery::mock('FilesystemAdapter');
$cacheInstance->sspCache->shouldreceive('getItem')
->andReturnUsing(
function($token) use (&$generated_token){
$generated_token = $token;

$cacheItem = Mockery::mock('CacheItem');

$cacheItem->shouldreceive('set')
->andReturnUsing(
function($formtoken) use (&$token) {
$this->assertEquals($formtoken,
$token,
"Error: received token in set ($formtoken) is different from received token in getItem ($token)");
}
);

$cacheItem->shouldreceive('expiresAfter')
->with(120);

return $cacheItem;
}
);

$cacheInstance->sspCache->shouldreceive('save');

$receivedToken = $cacheInstance->generate_form_token(120);
$this->assertEquals("$receivedToken",
$generated_token,
"Error: received token in generate_form_token ($receivedToken) is different from received token in getItem ($generated_token)");

}

public function test_verify_form_token_ok(): void
{

$generated_token = "e712b08e55f8977e2b9ecad35d5180ed24345e76607413411e90df66b9538fa1";

$cacheInstance = new \Ltb\Cache(
"testCache",
0,
null
);

$cacheInstance->sspCache = Mockery::mock('FilesystemAdapter');
$cacheInstance->sspCache->shouldreceive('getItem')
->andReturnUsing(
function($token) use(&$generated_token) {
$cacheItem = Mockery::mock('CacheItem');

$cacheItem->shouldreceive('isHit')
->andReturn(true);

$cacheItem->shouldreceive('get')
->andReturn($generated_token);

return $cacheItem;
}
);

$cacheInstance->sspCache->shouldreceive('deleteItem')
->with($generated_token);

$result = $cacheInstance->verify_form_token($generated_token);
$this->assertEquals("",
$result,
"Error: invalid result: '$result' sent by verify_form_token");

}

public function test_verify_form_token_ko(): void
{

$generated_token = "e712b08e55f8977e2b9ecad35d5180ed24345e76607413411e90df66b9538fa1";

$cacheInstance = new \Ltb\Cache(
"testCache",
0,
null
);

$cacheInstance->sspCache = Mockery::mock('FilesystemAdapter');
$cacheInstance->sspCache->shouldreceive('getItem')
->andReturnUsing(
function($token) use(&$generated_token) {
$cacheItem = Mockery::mock('CacheItem');

$cacheItem->shouldreceive('isHit')
->andReturn(false);

$cacheItem->shouldreceive('get')
->andReturn(null);

return $cacheItem;
}
);

$result = $cacheInstance->verify_form_token($generated_token);
$this->assertEquals("invalidformtoken",
$result,
"Error: expected 'invalidformtoken', but received result: '$result' in verify_form_token");

}
}

0 comments on commit 79b161b

Please sign in to comment.