Skip to content

Commit

Permalink
Used UrlSigner instead of CloudFrontClient etc
Browse files Browse the repository at this point in the history
  • Loading branch information
IlCallo committed Feb 13, 2018
1 parent 8726998 commit 7d104e4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 72 deletions.
49 changes: 6 additions & 43 deletions src/CloudFrontUrlSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,25 @@

namespace Dreamonkey\CloudFrontUrlSigner;

use Aws\CloudFront\CloudFrontClient;
use DateTime;
use Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidExpiration;
use Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidKeyPairId;
use Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidPrivateKeyPath;
use League\Uri\Http;

class CloudFrontUrlSigner implements UrlSigner
{
/**
* CloudFront client object.
*
* @var \Aws\CloudFront\CloudFrontClient
* @var \Aws\CloudFront\UrlSigner
*/
private $cloudFrontClient;
private $urlSigner;

/**
* Path where to find the private key of the trusted signer.
*
* @var string
* @param \Aws\CloudFront\UrlSigner $urlSigner
*/
private $privateKeyPath;

/**
* Identifier of the CloudFront Key Pair associated to the trusted signer.
*
* @var string
*/
private $keyPairId;

/**
* @param \Aws\CloudFront\CloudFrontClient $cloudFrontClient
* @param string $privateKeyPath
* @param string $keyPairId
*
* @throws \Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidPrivateKeyPath
* @throws \Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidKeyPairId
*/
public function __construct(CloudFrontClient $cloudFrontClient, string $privateKeyPath, string $keyPairId)
public function __construct(\Aws\CloudFront\UrlSigner $urlSigner)
{
if ($privateKeyPath == '') {
throw new InvalidPrivateKeyPath('Private key path cannot be empty');
}

if ($keyPairId == '') {
throw new InvalidKeyPairId('Key pair id cannot be empty');
}

$this->cloudFrontClient = $cloudFrontClient;
$this->privateKeyPath = $privateKeyPath;
$this->keyPairId = $keyPairId;
$this->urlSigner = $urlSigner;
}

/**
Expand All @@ -71,12 +39,7 @@ public function sign(string $url, $expiration = null): string
$expiration = $this->getExpirationTimestamp($expiration ??
config('cloudfront-url-signer.default_expiration_time_in_days'));

return $this->cloudFrontClient->getSignedUrl([
'url' => $resourceKey,
'expires' => $expiration,
'private_key' => $this->privateKeyPath,
'key_pair_id' => $this->keyPairId,
]);
return $this->urlSigner->getSignedUrl($resourceKey, $expiration);
}

/**
Expand Down
14 changes: 5 additions & 9 deletions src/CloudFrontUrlSignerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Dreamonkey\CloudFrontUrlSigner;

use Aws\CloudFront\CloudFrontClient;
use Illuminate\Contracts\Foundation\Application;
use Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidKeyPairId;
use Illuminate\Support\ServiceProvider;

class CloudFrontUrlSignerServiceProvider extends ServiceProvider
Expand All @@ -26,14 +25,11 @@ public function register()
$this->app->singleton(UrlSigner::class, function () {
$config = config('cloudfront-url-signer');

$cloudFrontClient = new CloudFrontClient([
// CloudFront is global, us-east-1 region must be used
// See https://docs.aws.amazon.com/general/latest/gr/rande.html?shortFooter=true#cf_region
'region' => 'us-east-1',
'version' => $config['version']
]);
if ($config['key_pair_id'] === '') {
throw new InvalidKeyPairId('Key pair id cannot be empty');
}

return new CloudFrontUrlSigner($cloudFrontClient, $config['private_key_path'], $config['key_pair_id']);
return new CloudFrontUrlSigner(new \Aws\CloudFront\UrlSigner($config['key_pair_id'], $config['private_key_path']));
});

$this->app->alias(UrlSigner::class, 'cloudfront-url-signer');
Expand Down
7 changes: 0 additions & 7 deletions src/Exceptions/InvalidPrivateKeyPath.php

This file was deleted.

13 changes: 0 additions & 13 deletions tests/SignatureGenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,6 @@ public function it_will_throw_an_exception_for_an_empty_key_pair_id()
sign($this->dummyUrl);
}

/**
* @test
*
* @expectedException \Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidPrivateKeyPath
*/
public function it_will_throw_an_exception_for_an_empty_private_key_path()
{
config(['cloudfront-url-signer.private_key_path' => '']);

/** @noinspection PhpUnhandledExceptionInspection */
sign($this->dummyUrl);
}

/** @test */
public function it_can_sign_an_url_that_expires_at_a_certain_time()
{
Expand Down

0 comments on commit 7d104e4

Please sign in to comment.