Skip to content

Commit

Permalink
feat: allow to use a date/time string in default expiration time conf…
Browse files Browse the repository at this point in the history
…iguration (#19)

Co-authored-by: Paul-joseph Krogulec <[email protected]>
  • Loading branch information
pauljosephkrogulec and Paul-joseph Krogulec authored Oct 18, 2023
1 parent bb272ee commit f4bc112
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 2.2.0

* Allow to use a date/time string in default expiration time configuration

## 2.1.0

* Minor **BC break**: use the new classes from `spatie/url-signer` 2.1.0 (`DateTimeInterface` instead of `DateTime` in `sign`)
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Compiler/SignerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private function getSigners(ContainerBuilder $container): array
$signerServiceDefinition = $container->getDefinition($signerServiceId);
$signerServiceDefinition->setBindings([
'string $signatureKey' => '%url_signer.signature_key%',
'int $defaultExpiration' => '%url_signer.default_expiration%',
'$defaultExpiration' => '%url_signer.default_expiration%',
'string $expiresParameter' => '%url_signer.expires_parameter%',
'string $signatureParameter' => '%url_signer.signature_parameter%',
]);
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,24 @@ coop_tilleuls_url_signer:
signer: 'md5' # 'sha256' by default
```

The default expiration time (in seconds) can be changed too:
The default expiration time can be changed too.

In seconds:

```yml
# config/packages/url_signer.yaml
coop_tilleuls_url_signer:
default_expiration: 3600 # 86400 by default
```

With a date/time string:

```yml
# config/packages/url_signer.yaml
coop_tilleuls_url_signer:
default_expiration: '1 day'
```

You can also customize the URL parameter names:

```yml
Expand Down
8 changes: 4 additions & 4 deletions UrlSigner/AbstractUrlSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@

abstract class AbstractUrlSigner extends SpatieAbstractUrlSigner implements UrlSignerInterface
{
private int $defaultExpiration;
private \DateTimeInterface|int $defaultExpiration;

public function __construct(string $signatureKey, int $defaultExpiration, string $expiresParameter, string $signatureParameter)
public function __construct(string $signatureKey, int|string $defaultExpiration, string $expiresParameter, string $signatureParameter)
{
parent::__construct($signatureKey, $expiresParameter, $signatureParameter);

$this->defaultExpiration = $defaultExpiration;
$this->defaultExpiration = \is_string($defaultExpiration) ? new \DateTimeImmutable($defaultExpiration) : $defaultExpiration;
}

public function sign(string $url, int|\DateTimeInterface $expiration = null, string $signatureKey = null): string
public function sign(string $url, \DateTimeInterface|int $expiration = null, string $signatureKey = null): string
{
return parent::sign($url, $expiration ?? $this->defaultExpiration, $signatureKey);
}
Expand Down
2 changes: 1 addition & 1 deletion UrlSigner/UrlSignerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

interface UrlSignerInterface extends UrlSigner
{
public function sign(string $url, int|\DateTimeInterface $expiration, string $signatureKey = null): string;
public function sign(string $url, \DateTimeInterface|int $expiration, string $signatureKey = null): string;

public static function getName(): string;
}
1 change: 1 addition & 0 deletions features/app/config/packages/url_signer.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
coop_tilleuls_url_signer:
signature_key: '%env(string:SIGNATURE_KEY)%'
default_expiration: '2 days'
2 changes: 1 addition & 1 deletion tests/DependencyInjection/Compiler/SignerPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testProcess(): void
]);
$bindings = [
'string $signatureKey' => '%url_signer.signature_key%',
'int $defaultExpiration' => '%url_signer.default_expiration%',
'$defaultExpiration' => '%url_signer.default_expiration%',
'string $expiresParameter' => '%url_signer.expires_parameter%',
'string $signatureParameter' => '%url_signer.signature_parameter%',
];
Expand Down
51 changes: 35 additions & 16 deletions tests/UrlSigner/AbstractUrlSignerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,12 @@
final class AbstractUrlSignerTest extends TestCase
{
private AbstractUrlSigner $signer;
private AbstractUrlSigner $signerDateTime;

protected function setUp(): void
{
$this->signer = new class('secret', 5, 'exp', 'sign') extends AbstractUrlSigner {
public static function getName(): string
{
return 'abstract';
}

protected function createSignature(string $url, string $expiration, string $signatureKey): string
{
return "{$url}::{$expiration}::{$signatureKey}";
}

protected function getExpirationTimestamp(int|\DateTimeInterface $expirationInSeconds): string
{
return $expirationInSeconds instanceof \DateTimeInterface ? 'datetime' : (string) $expirationInSeconds;
}
};
$this->signer = new Signer('secret', 5, 'exp', 'sign');
$this->signerDateTime = new Signer('secret', '2023-09-14', 'exp', 'sign');
}

public function testSignDefaultExpiration(): void
Expand All @@ -58,4 +45,36 @@ public function testSignWithExpiration(): void

self::assertSame('http://test.org/valid-signature?exp=7&sign=http%3A%2F%2Ftest.org%2Fvalid-signature%3A%3A7%3A%3Asecret', $signedUrl);
}

public function testSignDefaultExpirationWithDateTime(): void
{
$signedUrl = $this->signerDateTime->sign('http://test.org/valid-signature');

self::assertSame('http://test.org/valid-signature?exp=1694649600&sign=http%3A%2F%2Ftest.org%2Fvalid-signature%3A%3A1694649600%3A%3Asecret', $signedUrl);
}

public function testSignWithExpirationWithDateTime(): void
{
$signedUrl = $this->signerDateTime->sign('http://test.org/valid-signature', 7);

self::assertSame('http://test.org/valid-signature?exp=7&sign=http%3A%2F%2Ftest.org%2Fvalid-signature%3A%3A7%3A%3Asecret', $signedUrl);
}
}

final class Signer extends AbstractUrlSigner
{
public static function getName(): string
{
return 'abstract';
}

protected function createSignature(string $url, string $expiration, string $signatureKey): string
{
return "{$url}::{$expiration}::{$signatureKey}";
}

protected function getExpirationTimestamp(\DateTimeInterface|int $expirationInSeconds): string
{
return (string) ($expirationInSeconds instanceof \DateTimeInterface ? $expirationInSeconds->getTimestamp() : $expirationInSeconds);
}
}

0 comments on commit f4bc112

Please sign in to comment.