Skip to content

Commit

Permalink
remove Yii dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
RC4347 committed Dec 2, 2022
1 parent 0b2a44f commit f907779
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 69 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor
.idea
composer.lock
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"type": "package",
"require": {
"php": ">=7.4.0",
"yiisoft/yii2": "^2.0.13",
"aws/aws-sdk-php": "^3.246",
"league/flysystem-aws-s3-v3": "*"
"aws/aws-sdk-php": "^3.246"
},
"autoload": {
"psr-4": {
Expand Down
74 changes: 39 additions & 35 deletions src/SignedCookieService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,78 @@

namespace RC4347\CloudFrontSigner;

use Yii;
use yii\base\Model;
use Aws\CloudFront\CloudFrontClient;
use Aws\Exception\AwsException;
use yii\web\NotFoundHttpException;
use RC4347\CloudFrontSigner\credentials\AccessConfig;
use RC4347\CloudFrontSigner\credentials\ClientConfig;
use RC4347\CloudFrontSigner\credentials\ExpireConfig;

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

public ?string $policy;

public ClientConfig $clientConfig;
public AccessConfig $accessConfig;
public ExpireConfig $config;

/**
* @throws NotFoundHttpException
* @param string|null $policy
* @param ClientConfig $clientConfig
* @param AccessConfig $accessConfig
* @param ExpireConfig $config
*/
public function __construct($config = [])
public function __construct(ClientConfig $clientConfig, AccessConfig $accessConfig, ExpireConfig $config, ?string $policy = null)
{
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");
}
$this->policy = $policy;
$this->clientConfig = $clientConfig;
$this->accessConfig = $accessConfig;
$this->config = $config;
}

/**
* @return array|string
*/
public function run()
{
$cloudFrontClient = new CloudFrontClient([
'profile' => 'default',
'version' => 'latest',
'region' => env('S3_REGION')
'profile' => $this->clientConfig->profile ?? 'default',
'version' => $this->clientConfig->version ?? 'latest',
'region' => $this->clientConfig->region
]);

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

protected function getSignedCookie($cloudFrontClient)
/**
* @param CloudFrontClient $cloudFrontClient
* @return array|string
*/
protected function getSignedCookie(CloudFrontClient $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')
'policy' => $this->policy ?? $this->defaultPolicy(),
'private_key' => $this->accessConfig->privateKey,
'key_pair_id' => $this->accessConfig->keyPairId
]);
} 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 string
*/
protected function defaultPolicy(): string
{
return <<<POLICY
{
"Statement": [
{
"Resource": "{$this->url}",
"Resource": "{$this->config->resourceKey}",
"Condition": {
"DateLessThan": {"AWS:EpochTime": {$this->expires}}
"DateLessThan": {"AWS:EpochTime": {$this->config->expires}}
}
}
]
Expand Down
53 changes: 30 additions & 23 deletions src/SignedUrlService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,54 @@

use Aws\CloudFront\CloudFrontClient;
use Aws\Exception\AwsException;
use Yii;
use yii\base\Model;
use yii\web\NotFoundHttpException;
use RC4347\CloudFrontSigner\credentials\AccessConfig;
use RC4347\CloudFrontSigner\credentials\ClientConfig;
use RC4347\CloudFrontSigner\credentials\ExpireConfig;

class SignedUrlService extends Model
class SignedUrlService
{
const DEFAULT_DURATION = 300;
public string $resourceKey;
private int $expires;
public ExpireConfig $config;
public ClientConfig $clientConfig;
public AccessConfig $accessConfig;

/**
* @throws NotFoundHttpException
* @param ExpireConfig $config
* @param ClientConfig $clientConfig
* @param AccessConfig $accessConfig
*/
public function __construct($config = [])
public function __construct(ExpireConfig $config, ClientConfig $clientConfig, AccessConfig $accessConfig)
{
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");
}
$this->config = $config;
$this->clientConfig = $clientConfig;
$this->accessConfig = $accessConfig;
}

public function run()
/**
* @return string
*/
public function run(): string
{
$cloudFrontClient = new CloudFrontClient([
'profile' => 'default',
'version' => 'latest',
'region' => env('S3_REGION')
'profile' => $this->clientConfig->profile ?? 'default',
'version' => $this->clientConfig->version ?? 'latest',
'region' => $this->clientConfig->region
]);

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

protected function getSignedUrl($cloudFrontClient)
/**
* @param CloudFrontClient $cloudFrontClient
* @return string
*/
protected function getSignedUrl(CloudFrontClient $cloudFrontClient): string
{
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')
'url' => $this->config->resourceKey,
'expires' => $this->config->expires,
'private_key' => $this->accessConfig->privateKey,
'key_pair_id' => $this->accessConfig->keyPairId
]);

} catch (AwsException $e) {
Expand Down
20 changes: 20 additions & 0 deletions src/credentials/AccessConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace RC4347\CloudFrontSigner\credentials;

class AccessConfig
{
public $privateKey;
public $keyPairId;

/**
* @param $privateKey
* @param $keyPairId
*/
public function __construct($privateKey, $keyPairId)
{
$this->privateKey = $privateKey;
$this->keyPairId = $keyPairId;
}

}
17 changes: 17 additions & 0 deletions src/credentials/ClientConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace RC4347\CloudFrontSigner\credentials;

class ClientConfig
{
public string $version;
public string $profile;
public string $region;

public function __construct(string $version, string $profile, string $region)
{
$this->version = $version;
$this->profile = $profile;
$this->region = $region;
}
}
20 changes: 20 additions & 0 deletions src/credentials/ExpireConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace RC4347\CloudFrontSigner\credentials;

class ExpireConfig
{
public string $resourceKey;
public int $expires;

/**
* @param string $resourceKey
* @param int $expires
*/
public function __construct(string $resourceKey, int $expires)
{
$this->resourceKey = $resourceKey;
$this->expires = $expires;
}

}

0 comments on commit f907779

Please sign in to comment.