‪TYPO3CMS  ‪main
NonceTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
27 final class ‪NonceTest extends UnitTestCase
28 {
29  public static function ‪nonceIsCreatedDataProvider(): \Generator
30  {
31  yield [0, 40];
32  yield [20, 40];
33  yield [40, 40];
34  yield [60, 60];
35  }
36 
37  #[DataProvider('nonceIsCreatedDataProvider')]
38  #[Test]
39  public function ‪isCreated(int $length, int $expectedLength): void
40  {
41  $nonce = ‪Nonce::create($length);
42  self::assertSame($expectedLength, strlen($nonce->binary));
43  self::assertSame($nonce->b64, ‪StringUtility::base64urlEncode($nonce->binary));
44  }
45 
46  #[Test]
47  public function ‪isCreatedWithProperties(): void
48  {
49  $binary = random_bytes(40);
50  $time = $this->‪createRandomTime();
51  $nonce = new ‪Nonce($binary, $time);
52  self::assertSame($binary, $nonce->binary);
53  self::assertEquals($time, $nonce->time);
54  }
55 
56  #[Test]
57  public function ‪isEncodedAndDecoded(): void
58  {
59  $nonce = ‪Nonce::create();
60  $recodedNonce = ‪Nonce::fromHashSignedJwt($nonce->toHashSignedJwt());
61  self::assertEquals($recodedNonce, $nonce);
62  }
63 
64  #[Test]
65  public function ‪invalidJwtThrowsException(): void
66  {
67  $this->expectException(NonceException::class);
68  $this->expectExceptionCode(1651771351);
69  ‪Nonce::fromHashSignedJwt('no-jwt-at-all');
70  }
71 
72  private function ‪createRandomTime(): \DateTimeImmutable
73  {
74  // drop microtime, second is the minimum date-interval here
75  $now = \DateTimeImmutable::createFromFormat(
76  \DateTimeImmutable::RFC3339,
77  (new \DateTimeImmutable())->format(\DateTimeImmutable::RFC3339)
78  );
79  $delta = random_int(-7200, 7200);
80  $interval = new \DateInterval(sprintf('PT%dS', abs($delta)));
81  return $delta < 0 ? $now->sub($interval) : $now->add($interval);
82  }
83 }
‪TYPO3\CMS\Core\Tests\Unit\Security\NonceTest\invalidJwtThrowsException
‪invalidJwtThrowsException()
Definition: NonceTest.php:65
‪TYPO3\CMS\Core\Utility\StringUtility\base64urlEncode
‪static string base64urlEncode(string $value)
Definition: StringUtility.php:176
‪TYPO3\CMS\Core\Tests\Unit\Security\NonceTest\createRandomTime
‪createRandomTime()
Definition: NonceTest.php:72
‪TYPO3\CMS\Core\Tests\Unit\Security\NonceTest\isCreated
‪isCreated(int $length, int $expectedLength)
Definition: NonceTest.php:39
‪TYPO3\CMS\Core\Security\Nonce\create
‪static create(int $length=self::MIN_BYTES)
Definition: Nonce.php:37
‪TYPO3\CMS\Core\Security\Nonce\fromHashSignedJwt
‪static fromHashSignedJwt(string $jwt)
Definition: Nonce.php:42
‪TYPO3\CMS\Core\Security\NonceException
Definition: NonceException.php:25
‪TYPO3\CMS\Core\Security\Nonce
Definition: Nonce.php:29
‪TYPO3\CMS\Core\Tests\Unit\Security\NonceTest\isEncodedAndDecoded
‪isEncodedAndDecoded()
Definition: NonceTest.php:57
‪TYPO3\CMS\Core\Tests\Unit\Security\NonceTest\isCreatedWithProperties
‪isCreatedWithProperties()
Definition: NonceTest.php:47
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Core\Tests\Unit\Security\NonceTest\nonceIsCreatedDataProvider
‪static nonceIsCreatedDataProvider()
Definition: NonceTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Security
‪TYPO3\CMS\Core\Tests\Unit\Security\NonceTest
Definition: NonceTest.php:28