Skip to content

Commit

Permalink
Added sign() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
IlCallo committed Feb 9, 2018
1 parent ab8244f commit 94decab
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-cloudfront-url-signer` will be documented in this file.

## 0.1.0 - 2018-02-08
## 0.1.1 - 2018-02-09

- Pre-release
- Added `sign()` helper

## 0.1.0 - 2018-02-09

- Initial release
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
"autoload": {
"psr-4": {
"Dreamonkey\\CloudFrontUrlSigner\\": "src"
}
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/CloudFrontUrlSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct(CloudFrontClient $cloudFrontClient, string $privateK
* @return string
* @throws \Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidExpiration
*/
public function sign($url, $expiration)
public function sign(string $url, $expiration): string
{
$resourceKey = Http::createFromString($url);

Expand All @@ -85,7 +85,7 @@ public function sign($url, $expiration)
*
* @return bool
*/
protected function isFuture($timestamp)
protected function isFuture(int $timestamp): bool
{
return ((int)$timestamp) >= (new DateTime())->getTimestamp();
}
Expand All @@ -100,7 +100,7 @@ protected function isFuture($timestamp)
* @return string
* @throws \Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidExpiration
*/
protected function getExpirationTimestamp($expiration)
protected function getExpirationTimestamp($expiration): string
{
if (is_int($expiration)) {
$expiration = (new DateTime())->modify((int)$expiration . ' days');
Expand Down
2 changes: 1 addition & 1 deletion src/UrlSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ interface UrlSigner
*
* @return string
*/
public function sign($url, $expiration);
public function sign(string $url, $expiration): string;
}
18 changes: 18 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Dreamonkey\CloudFrontUrlSigner\UrlSigner;

if (!function_exists('sign')) {
/**
* A helper method to sign an URL using a CloudFront canned policy.
*
* @param string $url
* @param \DateTime|int $expiration
*
* @return string
*/
function sign(string $url, $expiration): string
{
return app(UrlSigner::class)->sign($url, $expiration);
}
}
6 changes: 3 additions & 3 deletions tests/SignatureGenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

class SignatureGenerationTest extends TestCase
{
private $dummyUrl = 'http://myapp.com';
private $dummyPrivateKeyPath = 'dummy/path/key.pem';
private $dummyKeyPairId = 'dummyKeyPairId';
private $dummyUrl = 'http://myapp.com';

private $mockCloudFrontClient;

Expand Down Expand Up @@ -54,7 +54,7 @@ public function it_will_throw_an_exception_for_an_empty_private_key_path()
}

/** @test */
public function it_can_sign_a_signed_url_that_expires_at_a_certain_time()
public function it_can_sign_an_url_that_expires_at_a_certain_time()
{
$expiration = DateTime::createFromFormat('d/m/Y H:i:s', '10/08/2115 18:15:44',
new DateTimeZone('Europe/Brussels'));
Expand All @@ -70,7 +70,7 @@ public function it_can_sign_a_signed_url_that_expires_at_a_certain_time()
}

/** @test */
public function it_can_sign_a_signed_url_that_expires_after_a_relative_amount_of_days()
public function it_can_sign_an_url_that_expires_after_a_relative_amount_of_days()
{
$expiration = 30;

Expand Down

0 comments on commit 94decab

Please sign in to comment.