ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
DataSigner.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\FileDelivery\Token;
22 
29 use ILIAS\FileDelivery\Token\Signer\Key\DigestMethod\Concat as NoneDigest;
45 
49 final class DataSigner
50 {
51  private Signer $signer;
58 
59  public function __construct(
60  SecretKeyRotation $key_rotation
61  ) {
62  $this->salt_factory = new Factory();
63  $compression = new DeflateCompression();
64  $transport = new URLSafeTransport();
65  $algorithm = new SHA1();
66 
67  $this->signing_serializer = new SigningSerializer(
69  $key_rotation,
70  new HMACSigner(
71  $algorithm
72  ),
74  $algorithm
75  )
76  ),
77  new JSONSerializer(),
78  $compression,
79  $transport
80  );
81 
82  $this->payload_builder = new Builder();
83  }
84 
85  public function getSignedStreamToken(
86  FileStream $stream,
87  string $filename,
88  Disposition $disposition,
89  int $user_id,
90  \DateTimeImmutable $until = null
91  ): string {
92  $payload = $this->payload_builder->shortFile(
93  $stream,
94  $filename,
95  $disposition
96  );
97 
98  if ($until !== null) {
99  $payload->setUntil($until->getTimestamp());
100  }
101 
102  return $this->signing_serializer->sign(
103  $payload,
104  $this->salt_factory->create('stream')
105  );
106  }
107 
108  public function verifyStreamToken(string $token): ?Payload
109  {
110  $data = $this->verify($token, 'stream');
111  if ($data === null) {
112  return null;
113  }
114  return $this->payload_builder->shortFileFromRaw($data);
115  }
116 
117  public function sign(
118  array $data,
119  string $salt,
120  \DateTimeImmutable $until = null
121  ): string {
122  $payload = new StructuredPayload($data);
123 
124  if ($until !== null) {
125  $payload->setUntil($until->getTimestamp());
126  }
127 
128  return $this->signing_serializer->sign(
129  $payload,
130  $this->salt_factory->create($salt)
131  );
132  }
133 
134  public function verify(
135  string $token,
136  string $salt
137  ): ?array {
138  return $this->signing_serializer->verify(
139  $token,
140  $this->salt_factory->create($salt)
141  )?->get();
142  }
143 }
getSignedStreamToken(FileStream $stream, string $filename, Disposition $disposition, int $user_id, \DateTimeImmutable $until=null)
Definition: DataSigner.php:85
SigningSerializer $signing_serializer
Definition: DataSigner.php:53
verify(string $token, string $salt)
Definition: DataSigner.php:134
if(count($parts) !=3) $payload
Definition: ltitoken.php:70
$token
Definition: xapitoken.php:70
$filename
Definition: buildRTE.php:78
sign(array $data, string $salt, \DateTimeImmutable $until=null)
Definition: DataSigner.php:117
__construct(SecretKeyRotation $key_rotation)
Definition: DataSigner.php:59
The base interface for all filesystem streams.
Definition: FileStream.php:31
Key rotation can provide an extra layer of mitigation against an attacker discovering a secret key...