PluginProbe
Media Cloud Sync / 1.4.1
Media Cloud Sync v1.4.1
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / Aws / Signature / SignatureProvider.php

SignatureProvider.php in Media Cloud Sync 1.4.1, at includes/sdk/s3/Aws/Signature/SignatureProvider.php

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