‪TYPO3CMS  ‪main
Nonce.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 
22 
29 {
30  use ‪JwtTrait;
31 
32  protected const ‪MIN_BYTES = 40;
33 
34  public readonly string ‪$b64;
35  public readonly \DateTimeImmutable ‪$time;
36 
37  public static function ‪create(int $length = self::MIN_BYTES): self
38  {
39  return GeneralUtility::makeInstance(self::class, random_bytes(max(self::MIN_BYTES, $length)));
40  }
41 
42  public static function ‪fromHashSignedJwt(string $jwt): self
43  {
44  try {
45  $payload = self::decodeJwt($jwt, self::createSigningKeyFromEncryptionKey(Nonce::class), true);
46  return GeneralUtility::makeInstance(
47  self::class,
48  ‪StringUtility::base64urlDecode($payload['nonce'] ?? '', true),
49  \DateTimeImmutable::createFromFormat(\DateTimeImmutable::RFC3339, $payload['time'] ?? null)
50  );
51  } catch (\Throwable $t) {
52  throw new ‪NonceException('Could not reconstitute nonce', 1651771351, $t);
53  }
54  }
55 
56  public function ‪__construct(public readonly string $binary, \DateTimeImmutable ‪$time = null)
57  {
58  if (strlen($this->binary) < self::MIN_BYTES) {
59  throw new \LogicException(
60  sprintf('Value must have at least %d bytes', self::MIN_BYTES),
61  1651785134
62  );
63  }
64  $this->b64 = ‪StringUtility::base64urlEncode($this->binary);
65  // drop microtime, second is the minimum date-interval
66  $this->time = \DateTimeImmutable::createFromFormat(
67  \DateTimeImmutable::RFC3339,
68  (‪$time ?? new \DateTimeImmutable())->format(\DateTimeImmutable::RFC3339)
69  );
70  }
71 
72  public function ‪getSigningIdentifier(): SecretIdentifier
73  {
74  return new SecretIdentifier('nonce', ‪StringUtility::base64urlEncode(md5($this->binary, true)));
75  }
76 
77  public function ‪getSigningSecret(): string
78  {
79  return hash('sha256', $this->binary);
80  }
81 
82  public function ‪toHashSignedJwt(): string
83  {
84  $payload = [
85  'nonce' => ‪$this->b64,
86  'time' => $this->time->format(\DateTimeImmutable::RFC3339),
87  ];
88  return self::encodeHashSignedJwt($payload, self::createSigningKeyFromEncryptionKey(Nonce::class));
89  }
90 }
‪TYPO3\CMS\Core\Security\Nonce\getSigningIdentifier
‪getSigningIdentifier()
Definition: Nonce.php:72
‪TYPO3\CMS\Core\Utility\StringUtility\base64urlEncode
‪static string base64urlEncode(string $value)
Definition: StringUtility.php:176
‪TYPO3\CMS\Core\Security\JwtTrait
Definition: JwtTrait.php:32
‪TYPO3\CMS\Core\Security\SigningSecretInterface
Definition: SigningSecretInterface.php:26
‪TYPO3\CMS\Core\Security\Nonce\create
‪static create(int $length=self::MIN_BYTES)
Definition: Nonce.php:37
‪TYPO3\CMS\Core\Security\Nonce\$b64
‪readonly string $b64
Definition: Nonce.php:34
‪TYPO3\CMS\Core\Security\Nonce\__construct
‪__construct(public readonly string $binary, \DateTimeImmutable $time=null)
Definition: Nonce.php:56
‪TYPO3\CMS\Core\Security\Nonce\MIN_BYTES
‪const MIN_BYTES
Definition: Nonce.php:32
‪TYPO3\CMS\Core\Security\Nonce\$time
‪readonly DateTimeImmutable $time
Definition: Nonce.php:35
‪TYPO3\CMS\Core\Security\Nonce\fromHashSignedJwt
‪static fromHashSignedJwt(string $jwt)
Definition: Nonce.php:42
‪TYPO3\CMS\Core\Security\Nonce\getSigningSecret
‪getSigningSecret()
Definition: Nonce.php:77
‪TYPO3\CMS\Core\Security\NonceException
Definition: NonceException.php:25
‪TYPO3\CMS\Core\Security\Nonce
Definition: Nonce.php:29
‪TYPO3\CMS\Core\Security\Nonce\toHashSignedJwt
‪toHashSignedJwt()
Definition: Nonce.php:82
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Core\Utility\StringUtility\base64urlDecode
‪static string false base64urlDecode(string $value, bool $strict=false)
Definition: StringUtility.php:195
‪TYPO3\CMS\Core\Security
Definition: BlockSerializationTrait.php:18