Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
RC4347 committed Dec 1, 2022
0 parents commit 0b2a44f
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

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

16 changes: 16 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "rc4347/cloudfront-signer",
"minimum-stability": "dev",
"type": "package",
"require": {
"php": ">=7.4.0",
"yiisoft/yii2": "^2.0.13",
"aws/aws-sdk-php": "^3.246",
"league/flysystem-aws-s3-v3": "*"
},
"autoload": {
"psr-4": {
"RC4347\\CloudFrontSigner\\": "src"
}
}
}
79 changes: 79 additions & 0 deletions src/SignedCookieService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace RC4347\CloudFrontSigner;

use Yii;
use yii\base\Model;
use Aws\CloudFront\CloudFrontClient;
use Aws\Exception\AwsException;
use yii\web\NotFoundHttpException;

class SignedCookieService extends Model
{
const DEFAULT_DURATION = 300;
public string $resourceKey;
private int $expires;
private string $url;

/**
* @throws NotFoundHttpException
*/
public function __construct($config = [])
{
parent::__construct($config);
$this->expires = time() + self::DEFAULT_DURATION;
if (!isset(Yii::$app->extensions['s3']['privateKey'])) {
throw new NotFoundHttpException("Private Key not found in config extension");
}
}

public function run()
{
$cloudFrontClient = new CloudFrontClient([
'profile' => 'default',
'version' => 'latest',
'region' => env('S3_REGION')
]);

return $this->getSignedCookie($cloudFrontClient);
}

protected function getSignedCookie($cloudFrontClient)
{
$this->url = $this->generateUrl($this->resourceKey);

try {
return $cloudFrontClient->getSignedCookie([
'policy' => $this->generatePolicy(),
'private_key' => Yii::$app->extensions['s3']['privateKey'],
'key_pair_id' => env('S3_KEY_PAIR_ID')
]);
} catch (AwsException $e) {
return 'Error : ' . $e->getAwsErrorMessage();
}
}

protected function generateUrl($resourceKey)
{
$splited = explode('/',$resourceKey);
$removeKey = count($splited) - 1;
unset($splited[$removeKey]);
return implode('/', $splited) . '/*';
}

protected function generatePolicy()
{
return <<<POLICY
{
"Statement": [
{
"Resource": "{$this->url}",
"Condition": {
"DateLessThan": {"AWS:EpochTime": {$this->expires}}
}
}
]
}
POLICY;
}
}
55 changes: 55 additions & 0 deletions src/SignedUrlService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace RC4347\CloudFrontSigner;

use Aws\CloudFront\CloudFrontClient;
use Aws\Exception\AwsException;
use Yii;
use yii\base\Model;
use yii\web\NotFoundHttpException;

class SignedUrlService extends Model
{
const DEFAULT_DURATION = 300;
public string $resourceKey;
private int $expires;

/**
* @throws NotFoundHttpException
*/
public function __construct($config = [])
{
parent::__construct($config);
$this->expires = time() + self::DEFAULT_DURATION;
if (!isset(Yii::$app->extensions['s3']['privateKey'])) {
throw new NotFoundHttpException("Private Key not found in config extension");
}
}

public function run()
{
$cloudFrontClient = new CloudFrontClient([
'profile' => 'default',
'version' => 'latest',
'region' => env('S3_REGION')
]);

return $this->getSignedUrl($cloudFrontClient);
}

protected function getSignedUrl($cloudFrontClient)
{
try {
return $cloudFrontClient->getSignedUrl([
'url' => $this->resourceKey,
'expires' => $this->expires,
'private_key' => Yii::$app->extensions['s3']['privateKey'],
'key_pair_id' => env('S3_KEY_PAIR_ID')
]);

} catch (AwsException $e) {
return 'Error: ' . $e->getAwsErrorMessage();
}
}

}

0 comments on commit 0b2a44f

Please sign in to comment.