<commit_before><?php

namespace Likipe\HMACBundle\NonceValidator;

use Memcached as MemcachedObject;

use Likipe\HMACBundle\NonceValidatorInterface;

/**
 * This nonce-validator utilizes memcached as a short-time value storage to prevent replay attacks,
 * the first part of the nonce is the number of milliseconds since the last epoch as the nonces
 * are supposed to be client-generated for this nonce-validator.
 * 
 * NOTE:
 *     This validator does NOT check if the nonce was issued by this server, NOR the age of
 *     the key for the macId.
 * 
 * TODO: Make a variant of this validator which checks the age of the mac-key used (macId)
 *       and then a controller + service which generates these keys.
 */
class MemcachedNonceValidator extends AbstractNonceValidator
{
	protected $memcached = null;
	protected $keyPrefix = '';
	protected $maxAge    = 10000; /* 10 seconds */
	
	/**
	 * @param  int  max age in milliseconds
	 */
	public function __construct(MemcachedObject $memcached, $keyPrefix, $maxAge = 10000  /* 10 seconds */)
	{
		$this->memcached = $memcached;
		$this->keyPrefix = $keyPrefix;
		$this->maxAge    = $maxAge;
		$this->expires   = 2 * max(ceil($maxAge / 1000), 1);
	}
	
	public function nonceIsValid($macId, $timestamp, $random)
	{
		if((floor(microtime(true) * 1000) - $timestamp) > $this->maxAge) {
			return false;
		}
		
		$key = $this->keyPrefix.'__'.base64_encode($macId.'__'.$random);
		
		if($this->memcached->get($key)) {
			return false;<commit_msg>MemcachedNonceValidator: Does now use absolute value of time-difference for validation<commit_after><?php

namespace Likipe\HMACBundle\NonceValidator;

use Memcached as MemcachedObject;

use Likipe\HMACBundle\NonceValidatorInterface;

/**
 * This nonce-validator utilizes memcached as a short-time value storage to prevent replay attacks,
 * the first part of the nonce is the number of milliseconds since the last epoch as the nonces
 * are supposed to be client-generated for this nonce-validator.
 * 
 * NOTE:
 *     This validator does NOT check if the nonce was issued by this server, NOR the age of
 *     the key for the macId.
 * 
 * TODO: Make a variant of this validator which checks the age of the mac-key used (macId)
 *       and then a controller + service which generates these keys.
 */
class MemcachedNonceValidator extends AbstractNonceValidator
{
	protected $memcached = null;
	protected $keyPrefix = '';
	protected $maxAge    = 10000; /* 10 seconds */
	
	/**
	 * @param  int  max age in milliseconds
	 */
	public function __construct(MemcachedObject $memcached, $keyPrefix, $maxAge = 10000  /* 10 seconds */)
	{
		$this->memcached = $memcached;
		$this->keyPrefix = $keyPrefix;
		$this->maxAge    = $maxAge;
		$this->expires   = 2 * max(ceil($maxAge / 1000), 1);
	}
	
	public function nonceIsValid($macId, $timestamp, $random)
	{
		if(abs(floor(microtime(true) * 1000) - $timestamp) > $this->maxAge) {
			return false;
		}
		
		$key = $this->keyPrefix.'__'.base64_encode($macId.'__'.$random);
		
		if($this->memcached->get($key)) {
			return false;
{'fraction_non_alphanumeric': 0.10915032679738562, 'fraction_numerical': 0.01699346405228758, 'mean_word_length': 3.7902973395931143, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5551854', 'n_tokens_mistral': 1044, 'n_tokens_neox': 919, 'n_words': 312}