{
return pack('Nn2', time(), mt_rand(), mt_rand());
}
+
+ /**
+ * Validate a nonce.
+ *
+ * @param string $nonce The nonce that should be validate.
+ * @param float $timeout The nonce should be invalid after this amount of time.
+ *
+ * @return boolean True if the nonce is still valid.
+ */
+ public function isValid($nonce, $timeout)
+ {
+ $timestamp = unpack('N', substr($nonce, 0, 4));
+ if (array_pop($timestamp) < (time() - $timeout)) {
+ return false;
+ }
+ return true;
+ }
}
{
$this->given('the default nonce setup')
->when('retrieving a nonce')
- ->and('waiting for the tenth of a second')
- ->then('the nonce is invalid given a timeout of a twentieth of a second');
+ ->and('waiting for two seconds')
+ ->then('the nonce is invalid given a timeout of one second');
}
}
\ No newline at end of file
case 'retrieving a nonce':
$world['nonce'] = $world['nonce_handler']->get();
break;
+ case 'waiting for two seconds':
+ sleep(2);
+ break;
default:
return $this->notImplemented($action);
}
case 'the nonce has a length of 8 bytes':
$this->assertEquals(8, strlen($world['nonce']));
break;
+ case 'the nonce is invalid given a timeout of one second':
+ $this->assertFalse($world['nonce_handler']->isValid($world['nonce'], 1));
+ break;
default:
return $this->notImplemented($action);
}