Skip to content

Commit

Permalink
Add expiration in the query string of the resulting URL
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Oct 8, 2023
1 parent b77a71e commit d56a053
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Add a Stimulus controller to disable expired links.
* Add `temporary_url_autoexpire` Twig function.
* Add expiration in the query string of the resulting URL.

## 1.2.1

Expand Down
6 changes: 4 additions & 2 deletions src/Internal/TemporaryUrlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function createTicket(
object $resource,
null|int|\DateInterval $ttl = null,
?string $sessionId = null,
): string {
): TemporaryUrlResult {
$resource = $this->resourceTransformer->transform($resource);

if (!$this->isObjectValid($resource)) {
Expand All @@ -62,6 +62,8 @@ public function createTicket(
? (int) $ttl->format('%s')
: ($ttl ?? $this->defaultTtl);

$expiration = \time() + $ttl - 10;

$ticketid = \bin2hex(\random_bytes(16));
$temporaryUrlData = new TemporaryUrlParameters($resource, $ttl, $sessionId);

Expand All @@ -71,7 +73,7 @@ public function createTicket(
$ttl,
);

return $ticketid;
return new TemporaryUrlResult($ticketid, $expiration);
}

private function isObjectValid(object $resource): bool
Expand Down
38 changes: 38 additions & 0 deletions src/Internal/TemporaryUrlResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/temporary-url-bundle package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\TemporaryUrl\Internal;

/**
* Represents the result of the request of a temporary URL
*
* @internal
*/
class TemporaryUrlResult
{
public function __construct(
private string $ticketId,
private int $expiration,
) {
}

public function getTicketId(): string
{
return $this->ticketId;
}

public function getExpiration(): int
{
return $this->expiration;
}
}
5 changes: 3 additions & 2 deletions src/TemporaryUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ public function generateUrl(
$sessionId = null;
}

$ticket = $this->temporaryUrlManager
$temporaryUrlResult = $this->temporaryUrlManager
->createTicket($resource, $ttl, $sessionId);

return $this->urlGenerator->generate(
'rekalogika_temporary_url',
[
'ticketid' => $ticket,
'ticketid' => $temporaryUrlResult->getTicketId(),
'expiration' => $temporaryUrlResult->getExpiration(),
],
$referenceType
);
Expand Down

0 comments on commit d56a053

Please sign in to comment.