-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0b2a44f
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |