‪TYPO3CMS  ‪main
RequestToken.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 
21 
26 {
27  use ‪JwtTrait;
28 
29  public const ‪PARAM_NAME = '__RequestToken';
30  public const ‪HEADER_NAME = 'X-TYPO3-RequestToken';
31 
32  public readonly string ‪$scope;
33  public readonly \DateTimeImmutable ‪$time;
37  public readonly array ‪$params;
38 
42  private ?SecretIdentifier ‪$signingSecretIdentifier = null;
43 
44  public static function ‪create(string ‪$scope): self
45  {
46  return GeneralUtility::makeInstance(self::class, ‪$scope);
47  }
48 
49  public static function ‪fromHashSignedJwt(string $jwt, ‪SigningSecretInterface|‪SigningSecretResolver $secret): self
50  {
51  // invokes resolver to retrieve corresponding secret
52  // a hint was stored in the `kid` (keyId) property of the JWT header
53  if ($secret instanceof ‪SigningSecretResolver) {
54  try {
55  $kid = (string)self::decodeJwtHeader($jwt, 'kid');
56  ‪$identifier = SecretIdentifier::fromJson($kid);
57  $secret = $secret->‪findByIdentifier(‪$identifier);
58  } catch (\Throwable $t) {
59  throw new ‪RequestTokenException('Could not reconstitute request token', 1664202134, $t);
60  }
61  if ($secret === null) {
62  throw new ‪RequestTokenException('Could not reconstitute request token', 1664202135);
63  }
64  }
65 
66  try {
67  $payload = ‪self::decodeJwt($jwt, self::createSigningSecret($secret, RequestToken::class), true);
68  $subject = GeneralUtility::makeInstance(
69  self::class,
70  $payload['scope'] ?? '',
71  \DateTimeImmutable::createFromFormat(\DateTimeImmutable::RFC3339, $payload['time'] ?? null),
72  $payload['params'] ?? []
73  );
74  $subject->signingSecretIdentifier = $secret->getSigningIdentifier();
75  return $subject;
76  } catch (\Throwable $t) {
77  throw new ‪RequestTokenException('Could not reconstitute request token', 1651771352, $t);
78  }
79  }
80 
81  public function ‪__construct(string ‪$scope, \DateTimeImmutable ‪$time = null, array ‪$params = [])
82  {
83  $this->scope = ‪$scope;
84  // drop microtime, second is the minimum date-interval
85  $this->time = \DateTimeImmutable::createFromFormat(
86  \DateTimeImmutable::RFC3339,
87  (‪$time ?? new \DateTimeImmutable())->format(\DateTimeImmutable::RFC3339)
88  );
89  $this->params = ‪$params;
90  }
91 
92  public function ‪toHashSignedJwt(‪SigningSecretInterface $secret): string
93  {
94  $payload = [
95  'scope' => ‪$this->scope,
96  'time' => $this->time->format(\DateTimeImmutable::RFC3339),
97  'params' => ‪$this->params,
98  ];
100  $payload,
101  self::createSigningSecret($secret, RequestToken::class),
102  $secret->‪getSigningIdentifier()
103  );
104  }
105 
106  public function ‪withParams(array ‪$params): self
107  {
108  return GeneralUtility::makeInstance(self::class, $this->scope, $this->time, ‪$params);
109  }
110 
111  public function ‪withMergedParams(array ‪$params): self
112  {
113  return $this->‪withParams(array_merge_recursive($this->params, ‪$params));
114  }
115 
116  public function ‪getSigningSecretIdentifier(): ?SecretIdentifier
117  {
119  }
120 }
‪TYPO3\CMS\Core\Security\SigningSecretResolver
Definition: SigningSecretResolver.php:26
‪TYPO3\CMS\Core\Security\RequestToken\withMergedParams
‪withMergedParams(array $params)
Definition: RequestToken.php:110
‪TYPO3\CMS\Core\Security\JwtTrait\decodeJwt
‪static decodeJwt(string $jwt, Key $key, bool $associative=false)
Definition: JwtTrait.php:63
‪TYPO3\CMS\Core\Security\RequestToken\$params
‪readonly array $params
Definition: RequestToken.php:36
‪TYPO3\CMS\Core\Security\RequestToken\HEADER_NAME
‪const HEADER_NAME
Definition: RequestToken.php:29
‪TYPO3\CMS\Core\Security\JwtTrait
Definition: JwtTrait.php:32
‪TYPO3\CMS\Core\Security\RequestTokenException
Definition: RequestTokenException.php:25
‪TYPO3\CMS\Core\Security\RequestToken\$scope
‪readonly string $scope
Definition: RequestToken.php:31
‪TYPO3\CMS\Core\Security\RequestToken\$signingSecretIdentifier
‪SecretIdentifier $signingSecretIdentifier
Definition: RequestToken.php:41
‪TYPO3\CMS\Core\Security\SigningSecretInterface
Definition: SigningSecretInterface.php:26
‪TYPO3\CMS\Core\Security\RequestToken
Definition: RequestToken.php:26
‪TYPO3\CMS\Core\Security\SigningSecretResolver\findByIdentifier
‪findByIdentifier(SecretIdentifier $identifier)
Definition: SigningSecretResolver.php:52
‪TYPO3\CMS\Core\Security\JwtTrait\encodeHashSignedJwt
‪static encodeHashSignedJwt(array $payload, Key $key, SecretIdentifier $identifier=null)
Definition: JwtTrait.php:57
‪TYPO3\CMS\Core\Security\RequestToken\getSigningSecretIdentifier
‪getSigningSecretIdentifier()
Definition: RequestToken.php:115
‪TYPO3\CMS\Core\Security\RequestToken\__construct
‪__construct(string $scope, \DateTimeImmutable $time=null, array $params=[])
Definition: RequestToken.php:80
‪TYPO3\CMS\Core\Security\RequestToken\PARAM_NAME
‪const PARAM_NAME
Definition: RequestToken.php:28
‪TYPO3\CMS\Core\Security\RequestToken\toHashSignedJwt
‪toHashSignedJwt(SigningSecretInterface $secret)
Definition: RequestToken.php:91
‪TYPO3\CMS\Core\Security\RequestToken\withParams
‪withParams(array $params)
Definition: RequestToken.php:105
‪TYPO3\CMS\Core\Security\RequestToken\create
‪static create(string $scope)
Definition: RequestToken.php:43
‪TYPO3\CMS\Core\Security\RequestToken\$time
‪readonly DateTimeImmutable $time
Definition: RequestToken.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Security
Definition: BlockSerializationTrait.php:18
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Core\Security\RequestToken\fromHashSignedJwt
‪static fromHashSignedJwt(string $jwt, SigningSecretInterface|SigningSecretResolver $secret)
Definition: RequestToken.php:48
‪TYPO3\CMS\Core\Security\SigningSecretInterface\getSigningIdentifier
‪getSigningIdentifier()