| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Signature; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Exception\UnresolvedSignatureException; |
| 6 |
/** |
| 7 |
* Signature providers. |
| 8 |
* |
| 9 |
* A signature provider is a function that accepts a version, service, and |
| 10 |
* region and returns a {@see SignatureInterface} object on success or NULL if |
| 11 |
* no signature can be created from the provided arguments. |
| 12 |
* |
| 13 |
* You can wrap your calls to a signature provider with the |
| 14 |
* {@see SignatureProvider::resolve} function to ensure that a signature object |
| 15 |
* is created. If a signature object is not created, then the resolve() |
| 16 |
* function will throw a {@see Aws\Exception\UnresolvedSignatureException}. |
| 17 |
* |
| 18 |
* use Aws\Signature\SignatureProvider; |
| 19 |
* $provider = SignatureProvider::defaultProvider(); |
| 20 |
* // Returns a SignatureInterface or NULL. |
| 21 |
* $signer = $provider('v4', 's3', 'us-west-2'); |
| 22 |
* // Returns a SignatureInterface or throws. |
| 23 |
* $signer = SignatureProvider::resolve($provider, 'no', 's3', 'foo'); |
| 24 |
* |
| 25 |
* You can compose multiple providers into a single provider using |
| 26 |
* {@see Aws\or_chain}. This function accepts providers as arguments and |
| 27 |
* returns a new function that will invoke each provider until a non-null value |
| 28 |
* is returned. |
| 29 |
* |
| 30 |
* $a = SignatureProvider::defaultProvider(); |
| 31 |
* $b = function ($version, $service, $region) { |
| 32 |
* if ($version === 'foo') { |
| 33 |
* return new MyFooSignature(); |
| 34 |
* } |
| 35 |
* }; |
| 36 |
* $c = \Aws\or_chain($a, $b); |
| 37 |
* $signer = $c('v4', 'abc', '123'); // $a handles this. |
| 38 |
* $signer = $c('foo', 'abc', '123'); // $b handles this. |
| 39 |
* $nullValue = $c('???', 'abc', '123'); // Neither can handle this. |
| 40 |
*/ |
| 41 |
class SignatureProvider |
| 42 |
{ |
| 43 |
private static $s3v4SignedServices = ['s3' => \true, 's3control' => \true, 's3-object-lambda' => \true]; |
| 44 |
/** |
| 45 |
* Resolves and signature provider and ensures a non-null return value. |
| 46 |
* |
| 47 |
* @param callable $provider Provider function to invoke. |
| 48 |
* @param string $version Signature version. |
| 49 |
* @param string $service Service name. |
| 50 |
* @param string $region Region name. |
| 51 |
* |
| 52 |
* @return SignatureInterface |
| 53 |
* @throws UnresolvedSignatureException |
| 54 |
*/ |
| 55 |
public static function resolve(callable $provider, $version, $service, $region) |
| 56 |
{ |
| 57 |
$result = $provider($version, $service, $region); |
| 58 |
if ($result instanceof SignatureInterface) { |
| 59 |
return $result; |
| 60 |
} |
| 61 |
throw new UnresolvedSignatureException("Unable to resolve a signature for {$version}/{$service}/{$region}.\n" . "Valid signature versions include v4 and anonymous."); |
| 62 |
} |
| 63 |
/** |
| 64 |
* Default SDK signature provider. |
| 65 |
* |
| 66 |
* @return callable |
| 67 |
*/ |
| 68 |
public static function defaultProvider() |
| 69 |
{ |
| 70 |
return self::memoize(self::version()); |
| 71 |
} |
| 72 |
/** |
| 73 |
* Creates a signature provider that caches previously created signature |
| 74 |
* objects. The computed cache key is the concatenation of the version, |
| 75 |
* service, and region. |
| 76 |
* |
| 77 |
* @param callable $provider Signature provider to wrap. |
| 78 |
* |
| 79 |
* @return callable |
| 80 |
*/ |
| 81 |
public static function memoize(callable $provider) |
| 82 |
{ |
| 83 |
$cache = []; |
| 84 |
return function ($version, $service, $region) use(&$cache, $provider) { |
| 85 |
$key = "({$version})({$service})({$region})"; |
| 86 |
if (!isset($cache[$key])) { |
| 87 |
$cache[$key] = $provider($version, $service, $region); |
| 88 |
} |
| 89 |
return $cache[$key]; |
| 90 |
}; |
| 91 |
} |
| 92 |
/** |
| 93 |
* Creates signature objects from known signature versions. |
| 94 |
* |
| 95 |
* This provider currently recognizes the following signature versions: |
| 96 |
* |
| 97 |
* - v4: Signature version 4. |
| 98 |
* - anonymous: Does not sign requests. |
| 99 |
* |
| 100 |
* @return callable |
| 101 |
*/ |
| 102 |
public static function version() |
| 103 |
{ |
| 104 |
return function ($version, $service, $region) { |
| 105 |
switch ($version) { |
| 106 |
case 's3v4': |
| 107 |
case 'v4': |
| 108 |
return !empty(self::$s3v4SignedServices[$service]) ? new S3SignatureV4($service, $region) : new SignatureV4($service, $region); |
| 109 |
case 'v4a': |
| 110 |
return new SignatureV4($service, $region, ['use_v4a' => \true]); |
| 111 |
case 'v4-unsigned-body': |
| 112 |
return !empty(self::$s3v4SignedServices[$service]) ? new S3SignatureV4($service, $region, ['unsigned-body' => 'true']) : new SignatureV4($service, $region, ['unsigned-body' => 'true']); |
| 113 |
case 'anonymous': |
| 114 |
return new AnonymousSignature(); |
| 115 |
default: |
| 116 |
return null; |
| 117 |
} |
| 118 |
}; |
| 119 |
} |
| 120 |
} |
| 121 |
|