diff --git a/src/StatelessCSRF.php b/src/StatelessCSRF.php index 2f7494d..182ddb6 100644 --- a/src/StatelessCSRF.php +++ b/src/StatelessCSRF.php @@ -2,6 +2,8 @@ namespace Ayesh\StatelessCSRF; +use Random\RandomException; + use function base64_decode; use function base64_encode; use function count; @@ -40,14 +42,19 @@ public function resetGlue(): void { $this->data = []; } + /** + * @throws \JsonException + */ public function getToken(string $identifier, int $expiration = null): string { $seed = $this->getRandomSeed(); $hash = $this->generateHash($identifier, $seed, $expiration, $this->data); return $this->urlSafeBase64Encode($seed . '|' . $expiration . '|' . $hash); } + /** + * @throws RandomException + */ private function getRandomSeed(): string { - /** @noinspection PhpUnhandledExceptionInspection */ return $this->urlSafeBase64Encode(random_bytes(8)); } diff --git a/tests/StatelessCSRFTest.php b/tests/StatelessCSRFTest.php index b2b6a96..f947750 100644 --- a/tests/StatelessCSRFTest.php +++ b/tests/StatelessCSRFTest.php @@ -1,9 +1,12 @@ -assertInstanceOf(StatelessCSRF::class, $instance); } + /** + * @throws RandomException + */ public function testStatelessNoGlue(): void { $key = bin2hex(random_bytes(8)); $generator = new StatelessCSRF($key); @@ -57,7 +63,7 @@ public static function getValidationDataSet(): array { * @param string $value * @param string $id * - * @throws \Exception + * @throws Exception */ public function testSeparateInstanceValidation(string $key, string $value, string $id): void { $secret_key = bin2hex(random_bytes(8)); @@ -92,7 +98,7 @@ public function testSeparateInstanceValidation(string $key, string $value, strin * @param string $value * @param string $id * - * @throws \Exception + * @throws Exception */ public function testTokenExpiration(string $key, string $value, string $id): void { $secret_key = bin2hex(random_bytes(8)); @@ -110,6 +116,9 @@ public function testTokenExpiration(string $key, string $value, string $id): voi $this->assertTrue($validator->validate($id, $token, $time + 3600)); } + /** + * @throws RandomException + */ public function testDebugInfoLeakNoSecret(): void { $secret_key = bin2hex(random_bytes(8)); $generator = new StatelessCSRF($secret_key);