Skip to content

Commit 7d104e4

Browse files
committed
Used UrlSigner instead of CloudFrontClient etc
1 parent 8726998 commit 7d104e4

File tree

4 files changed

+11
-72
lines changed

4 files changed

+11
-72
lines changed

src/CloudFrontUrlSigner.php

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,25 @@
22

33
namespace Dreamonkey\CloudFrontUrlSigner;
44

5-
use Aws\CloudFront\CloudFrontClient;
65
use DateTime;
76
use Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidExpiration;
8-
use Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidKeyPairId;
9-
use Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidPrivateKeyPath;
107
use League\Uri\Http;
118

129
class CloudFrontUrlSigner implements UrlSigner
1310
{
1411
/**
1512
* CloudFront client object.
1613
*
17-
* @var \Aws\CloudFront\CloudFrontClient
14+
* @var \Aws\CloudFront\UrlSigner
1815
*/
19-
private $cloudFrontClient;
16+
private $urlSigner;
2017

2118
/**
22-
* Path where to find the private key of the trusted signer.
23-
*
24-
* @var string
19+
* @param \Aws\CloudFront\UrlSigner $urlSigner
2520
*/
26-
private $privateKeyPath;
27-
28-
/**
29-
* Identifier of the CloudFront Key Pair associated to the trusted signer.
30-
*
31-
* @var string
32-
*/
33-
private $keyPairId;
34-
35-
/**
36-
* @param \Aws\CloudFront\CloudFrontClient $cloudFrontClient
37-
* @param string $privateKeyPath
38-
* @param string $keyPairId
39-
*
40-
* @throws \Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidPrivateKeyPath
41-
* @throws \Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidKeyPairId
42-
*/
43-
public function __construct(CloudFrontClient $cloudFrontClient, string $privateKeyPath, string $keyPairId)
21+
public function __construct(\Aws\CloudFront\UrlSigner $urlSigner)
4422
{
45-
if ($privateKeyPath == '') {
46-
throw new InvalidPrivateKeyPath('Private key path cannot be empty');
47-
}
48-
49-
if ($keyPairId == '') {
50-
throw new InvalidKeyPairId('Key pair id cannot be empty');
51-
}
52-
53-
$this->cloudFrontClient = $cloudFrontClient;
54-
$this->privateKeyPath = $privateKeyPath;
55-
$this->keyPairId = $keyPairId;
23+
$this->urlSigner = $urlSigner;
5624
}
5725

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

74-
return $this->cloudFrontClient->getSignedUrl([
75-
'url' => $resourceKey,
76-
'expires' => $expiration,
77-
'private_key' => $this->privateKeyPath,
78-
'key_pair_id' => $this->keyPairId,
79-
]);
42+
return $this->urlSigner->getSignedUrl($resourceKey, $expiration);
8043
}
8144

8245
/**

src/CloudFrontUrlSignerServiceProvider.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace Dreamonkey\CloudFrontUrlSigner;
44

5-
use Aws\CloudFront\CloudFrontClient;
6-
use Illuminate\Contracts\Foundation\Application;
5+
use Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidKeyPairId;
76
use Illuminate\Support\ServiceProvider;
87

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

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

36-
return new CloudFrontUrlSigner($cloudFrontClient, $config['private_key_path'], $config['key_pair_id']);
32+
return new CloudFrontUrlSigner(new \Aws\CloudFront\UrlSigner($config['key_pair_id'], $config['private_key_path']));
3733
});
3834

3935
$this->app->alias(UrlSigner::class, 'cloudfront-url-signer');

src/Exceptions/InvalidPrivateKeyPath.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/SignatureGenerationTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,6 @@ public function it_will_throw_an_exception_for_an_empty_key_pair_id()
4242
sign($this->dummyUrl);
4343
}
4444

45-
/**
46-
* @test
47-
*
48-
* @expectedException \Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidPrivateKeyPath
49-
*/
50-
public function it_will_throw_an_exception_for_an_empty_private_key_path()
51-
{
52-
config(['cloudfront-url-signer.private_key_path' => '']);
53-
54-
/** @noinspection PhpUnhandledExceptionInspection */
55-
sign($this->dummyUrl);
56-
}
57-
5845
/** @test */
5946
public function it_can_sign_an_url_that_expires_at_a_certain_time()
6047
{

0 commit comments

Comments
 (0)