From 0b2a44fee93bd432dca38f87f4280842934dc6ca Mon Sep 17 00:00:00 2001 From: islamirgashev0 Date: Thu, 1 Dec 2022 23:09:35 +0500 Subject: [PATCH] Initial commit --- .idea/.gitignore | 8 ++++ composer.json | 16 ++++++++ src/SignedCookieService.php | 79 +++++++++++++++++++++++++++++++++++++ src/SignedUrlService.php | 55 ++++++++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 composer.json create mode 100644 src/SignedCookieService.php create mode 100644 src/SignedUrlService.php diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..e663070 --- /dev/null +++ b/composer.json @@ -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" + } + } +} \ No newline at end of file diff --git a/src/SignedCookieService.php b/src/SignedCookieService.php new file mode 100644 index 0000000..e753b41 --- /dev/null +++ b/src/SignedCookieService.php @@ -0,0 +1,79 @@ +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 <<url}", + "Condition": { + "DateLessThan": {"AWS:EpochTime": {$this->expires}} + } + } + ] + } + POLICY; + } +} \ No newline at end of file diff --git a/src/SignedUrlService.php b/src/SignedUrlService.php new file mode 100644 index 0000000..f7275c8 --- /dev/null +++ b/src/SignedUrlService.php @@ -0,0 +1,55 @@ +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(); + } + } + +} \ No newline at end of file