spaces.php

This commit is contained in:
Devang Srivastava 2017-12-07 21:23:18 +05:30
commit eefa32741e
845 changed files with 50409 additions and 0 deletions

View file

@ -0,0 +1,154 @@
<?php
namespace Aws\CloudFront;
use Aws\AwsClient;
/**
* This client is used to interact with the **Amazon CloudFront** service.
*
* @method \Aws\Result createCloudFrontOriginAccessIdentity(array $args = [])
* @method \GuzzleHttp\Promise\Promise createCloudFrontOriginAccessIdentityAsync(array $args = [])
* @method \Aws\Result createDistribution(array $args = [])
* @method \GuzzleHttp\Promise\Promise createDistributionAsync(array $args = [])
* @method \Aws\Result createInvalidation(array $args = [])
* @method \GuzzleHttp\Promise\Promise createInvalidationAsync(array $args = [])
* @method \Aws\Result createStreamingDistribution(array $args = [])
* @method \GuzzleHttp\Promise\Promise createStreamingDistributionAsync(array $args = [])
* @method \Aws\Result deleteCloudFrontOriginAccessIdentity(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteCloudFrontOriginAccessIdentityAsync(array $args = [])
* @method \Aws\Result deleteDistribution(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteDistributionAsync(array $args = [])
* @method \Aws\Result deleteStreamingDistribution(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteStreamingDistributionAsync(array $args = [])
* @method \Aws\Result getCloudFrontOriginAccessIdentity(array $args = [])
* @method \GuzzleHttp\Promise\Promise getCloudFrontOriginAccessIdentityAsync(array $args = [])
* @method \Aws\Result getCloudFrontOriginAccessIdentityConfig(array $args = [])
* @method \GuzzleHttp\Promise\Promise getCloudFrontOriginAccessIdentityConfigAsync(array $args = [])
* @method \Aws\Result getDistribution(array $args = [])
* @method \GuzzleHttp\Promise\Promise getDistributionAsync(array $args = [])
* @method \Aws\Result getDistributionConfig(array $args = [])
* @method \GuzzleHttp\Promise\Promise getDistributionConfigAsync(array $args = [])
* @method \Aws\Result getInvalidation(array $args = [])
* @method \GuzzleHttp\Promise\Promise getInvalidationAsync(array $args = [])
* @method \Aws\Result getStreamingDistribution(array $args = [])
* @method \GuzzleHttp\Promise\Promise getStreamingDistributionAsync(array $args = [])
* @method \Aws\Result getStreamingDistributionConfig(array $args = [])
* @method \GuzzleHttp\Promise\Promise getStreamingDistributionConfigAsync(array $args = [])
* @method \Aws\Result listCloudFrontOriginAccessIdentities(array $args = [])
* @method \GuzzleHttp\Promise\Promise listCloudFrontOriginAccessIdentitiesAsync(array $args = [])
* @method \Aws\Result listDistributions(array $args = [])
* @method \GuzzleHttp\Promise\Promise listDistributionsAsync(array $args = [])
* @method \Aws\Result listDistributionsByWebACLId(array $args = [])
* @method \GuzzleHttp\Promise\Promise listDistributionsByWebACLIdAsync(array $args = [])
* @method \Aws\Result listInvalidations(array $args = [])
* @method \GuzzleHttp\Promise\Promise listInvalidationsAsync(array $args = [])
* @method \Aws\Result listStreamingDistributions(array $args = [])
* @method \GuzzleHttp\Promise\Promise listStreamingDistributionsAsync(array $args = [])
* @method \Aws\Result updateCloudFrontOriginAccessIdentity(array $args = [])
* @method \GuzzleHttp\Promise\Promise updateCloudFrontOriginAccessIdentityAsync(array $args = [])
* @method \Aws\Result updateDistribution(array $args = [])
* @method \GuzzleHttp\Promise\Promise updateDistributionAsync(array $args = [])
* @method \Aws\Result updateStreamingDistribution(array $args = [])
* @method \GuzzleHttp\Promise\Promise updateStreamingDistributionAsync(array $args = [])
* @method \Aws\Result createDistributionWithTags(array $args = []) (supported in versions 2016-08-01, 2016-08-20, 2016-09-07, 2016-09-29, 2016-11-25, 2017-03-25)
* @method \GuzzleHttp\Promise\Promise createDistributionWithTagsAsync(array $args = []) (supported in versions 2016-08-01, 2016-08-20, 2016-09-07, 2016-09-29, 2016-11-25, 2017-03-25)
* @method \Aws\Result createStreamingDistributionWithTags(array $args = []) (supported in versions 2016-08-01, 2016-08-20, 2016-09-07, 2016-09-29, 2016-11-25, 2017-03-25)
* @method \GuzzleHttp\Promise\Promise createStreamingDistributionWithTagsAsync(array $args = []) (supported in versions 2016-08-01, 2016-08-20, 2016-09-07, 2016-09-29, 2016-11-25, 2017-03-25)
* @method \Aws\Result listTagsForResource(array $args = []) (supported in versions 2016-08-01, 2016-08-20, 2016-09-07, 2016-09-29, 2016-11-25, 2017-03-25)
* @method \GuzzleHttp\Promise\Promise listTagsForResourceAsync(array $args = []) (supported in versions 2016-08-01, 2016-08-20, 2016-09-07, 2016-09-29, 2016-11-25, 2017-03-25)
* @method \Aws\Result tagResource(array $args = []) (supported in versions 2016-08-01, 2016-08-20, 2016-09-07, 2016-09-29, 2016-11-25, 2017-03-25)
* @method \GuzzleHttp\Promise\Promise tagResourceAsync(array $args = []) (supported in versions 2016-08-01, 2016-08-20, 2016-09-07, 2016-09-29, 2016-11-25, 2017-03-25)
* @method \Aws\Result untagResource(array $args = []) (supported in versions 2016-08-01, 2016-08-20, 2016-09-07, 2016-09-29, 2016-11-25, 2017-03-25)
* @method \GuzzleHttp\Promise\Promise untagResourceAsync(array $args = []) (supported in versions 2016-08-01, 2016-08-20, 2016-09-07, 2016-09-29, 2016-11-25, 2017-03-25)
* @method \Aws\Result deleteServiceLinkedRole(array $args = []) (supported in versions 2017-03-25)
* @method \GuzzleHttp\Promise\Promise deleteServiceLinkedRoleAsync(array $args = []) (supported in versions 2017-03-25)
*/
class CloudFrontClient extends AwsClient
{
/**
* Create a signed Amazon CloudFront URL.
*
* This method accepts an array of configuration options:
*
* - url: (string) URL of the resource being signed (can include query
* string and wildcards). For example: rtmp://s5c39gqb8ow64r.cloudfront.net/videos/mp3_name.mp3
* http://d111111abcdef8.cloudfront.net/images/horizon.jpg?size=large&license=yes
* - policy: (string) JSON policy. Use this option when creating a signed
* URL for a custom policy.
* - expires: (int) UTC Unix timestamp used when signing with a canned
* policy. Not required when passing a custom 'policy' option.
* - key_pair_id: (string) The ID of the key pair used to sign CloudFront
* URLs for private distributions.
* - private_key: (string) The filepath ot the private key used to sign
* CloudFront URLs for private distributions.
*
* @param array $options Array of configuration options used when signing
*
* @return string Signed URL with authentication parameters
* @throws \InvalidArgumentException if url, key_pair_id, or private_key
* were not specified.
* @link http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/WorkingWithStreamingDistributions.html
*/
public function getSignedUrl(array $options)
{
foreach (['url', 'key_pair_id', 'private_key'] as $required) {
if (!isset($options[$required])) {
throw new \InvalidArgumentException("$required is required");
}
}
$urlSigner = new UrlSigner(
$options['key_pair_id'],
$options['private_key']
);
return $urlSigner->getSignedUrl(
$options['url'],
isset($options['expires']) ? $options['expires'] : null,
isset($options['policy']) ? $options['policy'] : null
);
}
/**
* Create a signed Amazon CloudFront cookie.
*
* This method accepts an array of configuration options:
*
* - url: (string) URL of the resource being signed (can include query
* string and wildcards). For example: http://d111111abcdef8.cloudfront.net/images/horizon.jpg?size=large&license=yes
* - policy: (string) JSON policy. Use this option when creating a signed
* URL for a custom policy.
* - expires: (int) UTC Unix timestamp used when signing with a canned
* policy. Not required when passing a custom 'policy' option.
* - key_pair_id: (string) The ID of the key pair used to sign CloudFront
* URLs for private distributions.
* - private_key: (string) The filepath ot the private key used to sign
* CloudFront URLs for private distributions.
*
* @param array $options Array of configuration options used when signing
*
* @return array Key => value pairs of signed cookies to set
* @throws \InvalidArgumentException if url, key_pair_id, or private_key
* were not specified.
* @link http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/WorkingWithStreamingDistributions.html
*/
public function getSignedCookie(array $options)
{
foreach (['key_pair_id', 'private_key'] as $required) {
if (!isset($options[$required])) {
throw new \InvalidArgumentException("$required is required");
}
}
$cookieSigner = new CookieSigner(
$options['key_pair_id'],
$options['private_key']
);
return $cookieSigner->getSignedCookie(
isset($options['url']) ? $options['url'] : null,
isset($options['expires']) ? $options['expires'] : null,
isset($options['policy']) ? $options['policy'] : null
);
}
}

View file

@ -0,0 +1,65 @@
<?php
namespace Aws\CloudFront;
class CookieSigner
{
/** @var Signer */
private $signer;
private static $schemes = [
'http' => true,
'https' => true,
];
/**
* @param $keyPairId string ID of the key pair
* @param $privateKey string Path to the private key used for signing
*
* @throws \RuntimeException if the openssl extension is missing
* @throws \InvalidArgumentException if the private key cannot be found.
*/
public function __construct($keyPairId, $privateKey)
{
$this->signer = new Signer($keyPairId, $privateKey);
}
/**
* Create a signed Amazon CloudFront Cookie.
*
* @param string $url URL to sign (can include query string
* and wildcards). Not required
* when passing a custom $policy.
* @param string|integer|null $expires UTC Unix timestamp used when signing
* with a canned policy. Not required
* when passing a custom $policy.
* @param string $policy JSON policy. Use this option when
* creating a signed cookie for a custom
* policy.
*
* @return array The authenticated cookie parameters
* @throws \InvalidArgumentException if the URL provided is invalid
* @link http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-signed-cookies.html
*/
public function getSignedCookie($url = null, $expires = null, $policy = null)
{
if ($url) {
$this->validateUrl($url);
}
$cookieParameters = [];
$signature = $this->signer->getSignature($url, $expires, $policy);
foreach ($signature as $key => $value) {
$cookieParameters["CloudFront-$key"] = $value;
}
return $cookieParameters;
}
private function validateUrl($url)
{
$scheme = str_replace('*', '', explode('://', $url)[0]);
if (empty(self::$schemes[strtolower($scheme)])) {
throw new \InvalidArgumentException('Invalid or missing URI scheme');
}
}
}

View file

@ -0,0 +1,9 @@
<?php
namespace Aws\CloudFront\Exception;
use Aws\Exception\AwsException;
/**
* Represents an error interacting with the Amazon CloudFront service.
*/
class CloudFrontException extends AwsException {}

View file

@ -0,0 +1,108 @@
<?php
namespace Aws\CloudFront;
/**
* @internal
*/
class Signer
{
private $keyPairId;
private $pk;
/**
* A signer for creating the signature values used in CloudFront signed URLs
* and signed cookies.
*
* @param $keyPairId string ID of the key pair
* @param $privateKey string Path to the private key used for signing
*
* @throws \RuntimeException if the openssl extension is missing
* @throws \InvalidArgumentException if the private key cannot be found.
*/
public function __construct($keyPairId, $privateKey)
{
if (!extension_loaded('openssl')) {
//@codeCoverageIgnoreStart
throw new \RuntimeException('The openssl extension is required to '
. 'sign CloudFront urls.');
//@codeCoverageIgnoreEnd
}
$this->keyPairId = $keyPairId;
if (!file_exists($privateKey)) {
throw new \InvalidArgumentException("PK file not found: $privateKey");
}
$this->pk = file_get_contents($privateKey);
}
/**
* Create the values used to construct signed URLs and cookies.
*
* @param string $resource The CloudFront resource to which
* this signature will grant access.
* Not used when a custom policy is
* provided.
* @param string|integer|null $expires UTC Unix timestamp used when
* signing with a canned policy.
* Not required when passing a
* custom $policy.
* @param string $policy JSON policy. Use this option when
* creating a signature for a custom
* policy.
*
* @return array The values needed to construct a signed URL or cookie
* @throws \InvalidArgumentException when not provided either a policy or a
* resource and a expires
*
* @link http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-signed-cookies.html
*/
public function getSignature($resource = null, $expires = null, $policy = null)
{
$signatureHash = [];
if ($policy) {
$policy = preg_replace('/\s/s', '', $policy);
$signatureHash['Policy'] = $this->encode($policy);
} elseif ($resource && $expires) {
$policy = $this->createCannedPolicy($resource, $expires);
$signatureHash['Expires'] = $expires;
} else {
throw new \InvalidArgumentException('Either a policy or a resource'
. ' and an expiration time must be provided.');
}
$signatureHash['Signature'] = $this->encode($this->sign($policy));
$signatureHash['Key-Pair-Id'] = $this->keyPairId;
return $signatureHash;
}
private function createCannedPolicy($resource, $expiration)
{
return json_encode([
'Statement' => [
[
'Resource' => $resource,
'Condition' => [
'DateLessThan' => ['AWS:EpochTime' => $expiration],
],
],
],
], JSON_UNESCAPED_SLASHES);
}
private function sign($policy)
{
$signature = '';
openssl_sign($policy, $signature, $this->pk);
return $signature;
}
private function encode($policy)
{
return strtr(base64_encode($policy), '+=/', '-_~');
}
}

View file

@ -0,0 +1,119 @@
<?php
namespace Aws\CloudFront;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface;
/**
* Creates signed URLs for Amazon CloudFront resources.
*/
class UrlSigner
{
private $signer;
/**
* @param $keyPairId string ID of the key pair
* @param $privateKey string Path to the private key used for signing
*
* @throws \RuntimeException if the openssl extension is missing
* @throws \InvalidArgumentException if the private key cannot be found.
*/
public function __construct($keyPairId, $privateKey)
{
$this->signer = new Signer($keyPairId, $privateKey);
}
/**
* Create a signed Amazon CloudFront URL.
*
* Keep in mind that URLs meant for use in media/flash players may have
* different requirements for URL formats (e.g. some require that the
* extension be removed, some require the file name to be prefixed
* - mp4:<path>, some require you to add "/cfx/st" into your URL).
*
* @param string $url URL to sign (can include query
* string string and wildcards)
* @param string|integer|null $expires UTC Unix timestamp used when signing
* with a canned policy. Not required
* when passing a custom $policy.
* @param string $policy JSON policy. Use this option when
* creating a signed URL for a custom
* policy.
*
* @return string The file URL with authentication parameters
* @throws \InvalidArgumentException if the URL provided is invalid
* @link http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/WorkingWithStreamingDistributions.html
*/
public function getSignedUrl($url, $expires = null, $policy = null)
{
// Determine the scheme of the url
$urlSections = explode('://', $url);
if (count($urlSections) < 2) {
throw new \InvalidArgumentException("Invalid URL: {$url}");
}
// Get the real scheme by removing wildcards from the scheme
$scheme = str_replace('*', '', $urlSections[0]);
$uri = new Uri($scheme . '://' . $urlSections[1]);
$query = Psr7\parse_query($uri->getQuery(), PHP_QUERY_RFC3986);
$signature = $this->signer->getSignature(
$this->createResource($scheme, (string) $uri),
$expires,
$policy
);
$uri = $uri->withQuery(
http_build_query($query + $signature, null, '&', PHP_QUERY_RFC3986)
);
return $scheme === 'rtmp'
? $this->createRtmpUrl($uri)
: (string) $uri;
}
private function createRtmpUrl(UriInterface $uri)
{
// Use a relative URL when creating Flash player URLs
$result = ltrim($uri->getPath(), '/');
if ($query = $uri->getQuery()) {
$result .= '?' . $query;
}
return $result;
}
/**
* @param $scheme
* @param $url
*
* @return string
*/
private function createResource($scheme, $url)
{
switch ($scheme) {
case 'http':
case 'http*':
case 'https':
return $url;
case 'rtmp':
$parts = parse_url($url);
$pathParts = pathinfo($parts['path']);
$resource = ltrim(
$pathParts['dirname'] . '/' . $pathParts['basename'],
'/'
);
// Add a query string if present.
if (isset($parts['query'])) {
$resource .= "?{$parts['query']}";
}
return $resource;
}
throw new \InvalidArgumentException("Invalid URI scheme: {$scheme}. "
. "Scheme must be one of: http, https, or rtmp");
}
}