| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\CloudFront; |
| 4 |
|
| 5 |
class CookieSigner |
| 6 |
{ |
| 7 |
/** @var Signer */ |
| 8 |
private $signer; |
| 9 |
private static $schemes = ['http' => \true, 'https' => \true]; |
| 10 |
/** |
| 11 |
* @param $keyPairId string ID of the key pair |
| 12 |
* @param $privateKey string Path to the private key used for signing |
| 13 |
* |
| 14 |
* @throws \RuntimeException if the openssl extension is missing |
| 15 |
* @throws \InvalidArgumentException if the private key cannot be found. |
| 16 |
*/ |
| 17 |
public function __construct($keyPairId, $privateKey) |
| 18 |
{ |
| 19 |
$this->signer = new Signer($keyPairId, $privateKey); |
| 20 |
} |
| 21 |
/** |
| 22 |
* Create a signed Amazon CloudFront Cookie. |
| 23 |
* |
| 24 |
* @param string $url URL to sign (can include query string |
| 25 |
* and wildcards). Not required |
| 26 |
* when passing a custom $policy. |
| 27 |
* @param string|integer|null $expires UTC Unix timestamp used when signing |
| 28 |
* with a canned policy. Not required |
| 29 |
* when passing a custom $policy. |
| 30 |
* @param string $policy JSON policy. Use this option when |
| 31 |
* creating a signed cookie for a custom |
| 32 |
* policy. |
| 33 |
* |
| 34 |
* @return array The authenticated cookie parameters |
| 35 |
* @throws \InvalidArgumentException if the URL provided is invalid |
| 36 |
* @link http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-signed-cookies.html |
| 37 |
*/ |
| 38 |
public function getSignedCookie($url = null, $expires = null, $policy = null) |
| 39 |
{ |
| 40 |
if ($url) { |
| 41 |
$this->validateUrl($url); |
| 42 |
} |
| 43 |
$cookieParameters = []; |
| 44 |
$signature = $this->signer->getSignature($url, $expires, $policy); |
| 45 |
foreach ($signature as $key => $value) { |
| 46 |
$cookieParameters["CloudFront-{$key}"] = $value; |
| 47 |
} |
| 48 |
return $cookieParameters; |
| 49 |
} |
| 50 |
private function validateUrl($url) |
| 51 |
{ |
| 52 |
$scheme = \str_replace('*', '', \explode('://', $url)[0]); |
| 53 |
if (empty(self::$schemes[\strtolower($scheme)])) { |
| 54 |
throw new \InvalidArgumentException('Invalid or missing URI scheme'); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|