Implement TempUrlLocalStorageGenerator and its tests

Added the full implementation for TempUrlLocalStorageGenerator, including methods to generate signed URLs and POST requests with expiration and signature logic. Introduced corresponding unit tests to validate functionality using mocked dependencies.
This commit is contained in:
2024-12-16 23:20:08 +01:00
parent 3a2548ed89
commit 1f6de3cb11
2 changed files with 126 additions and 2 deletions

View File

@@ -14,16 +14,62 @@ namespace Chill\DocStoreBundle\AsyncUpload\Driver\LocalStorage;
use Chill\DocStoreBundle\AsyncUpload\SignedUrl;
use Chill\DocStoreBundle\AsyncUpload\SignedUrlPost;
use Chill\DocStoreBundle\AsyncUpload\TempUrlGeneratorInterface;
use Chill\DocStoreBundle\Entity\StoredObject;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class TempUrlLocalStorageGenerator implements TempUrlGeneratorInterface
{
private const SIGNATURE_DURATION = 180;
public function __construct(
private readonly string $secret,
private readonly ClockInterface $clock,
private readonly UrlGeneratorInterface $urlGenerator,
) {}
public function generate(string $method, string $object_name, ?int $expire_delay = null): SignedUrl
{
// TODO: Implement generate() method.
$expiration = $this->clock->now()->getTimestamp() + min($expire_delay ?? self::SIGNATURE_DURATION, self::SIGNATURE_DURATION);
return new SignedUrl(
$method,
$this->urlGenerator->generate('chill_docstore_stored_object_operate', [
'object_name' => $object_name,
'exp' => $expiration,
'sig' => $this->sign($method, $object_name, $expiration),
], UrlGeneratorInterface::ABSOLUTE_URL),
\DateTimeImmutable::createFromFormat('U', (string) $expiration),
$object_name,
);
}
public function generatePost(?int $expire_delay = null, ?int $submit_delay = null, int $max_file_count = 1, ?string $object_name = null): SignedUrlPost
{
// TODO: Implement generatePost() method.
$submitDelayComputed = min($submit_delay ?? self::SIGNATURE_DURATION, self::SIGNATURE_DURATION);
$expireDelayComputed = min($expire_delay ?? self::SIGNATURE_DURATION, self::SIGNATURE_DURATION);
$objectNameComputed = $object_name ?? StoredObject::generatePrefix();
$expiration = $this->clock->now()->getTimestamp() + $expireDelayComputed + $submitDelayComputed;
return new SignedUrlPost(
$this->urlGenerator->generate(
'chill_docstore_storedobject_post',
['prefix' => $objectNameComputed],
UrlGeneratorInterface::ABSOLUTE_URL
),
\DateTimeImmutable::createFromFormat('U', (string) $expiration),
$objectNameComputed,
15_000_000,
1,
$submitDelayComputed,
'',
$objectNameComputed,
$this->sign('POST', $object_name, $expiration),
);
}
private function sign(string $method, string $object_name, int $expiration): string
{
return hash_hmac('sha512', $method, sprintf('%s.%s.%s.%d', $method, $this->secret, $object_name, $expiration));
}
}