PluginProbe ʕ •ᴥ•ʔ
Transferito: WP Migration / trunk
Transferito: WP Migration vtrunk
trunk 11.4.0 12.0.0 13.1.0 14.0.0 14.0.11 14.0.7 14.1.0 14.1.1 14.1.2 14.1.3 14.1.4
transferito / vendor / aws / aws-sdk-php / src / Signature / SignatureProvider.php
transferito / vendor / aws / aws-sdk-php / src / Signature Last commit date
AnonymousSignature.php 11 months ago S3ExpressSignature.php 11 months ago S3SignatureV4.php 11 months ago SignatureInterface.php 11 months ago SignatureProvider.php 11 months ago SignatureTrait.php 11 months ago SignatureV4.php 11 months ago
SignatureProvider.php
147 lines
1 <?php
2 namespace Aws\Signature;
3
4 use Aws\Exception\UnresolvedSignatureException;
5 use Aws\Token\BearerTokenAuthorization;
6
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 = [
45 's3' => true,
46 's3control' => true,
47 's3-object-lambda' => true,
48 's3express' => true
49 ];
50
51 /**
52 * Resolves and signature provider and ensures a non-null return value.
53 *
54 * @param callable $provider Provider function to invoke.
55 * @param string $version Signature version.
56 * @param string $service Service name.
57 * @param string $region Region name.
58 *
59 * @return SignatureInterface
60 * @throws UnresolvedSignatureException
61 */
62 public static function resolve(callable $provider, $version, $service, $region)
63 {
64 $result = $provider($version, $service, $region);
65 if ($result instanceof SignatureInterface
66 || $result instanceof BearerTokenAuthorization
67 ) {
68 return $result;
69 }
70
71 throw new UnresolvedSignatureException(
72 "Unable to resolve a signature for $version/$service/$region.\n"
73 . "Valid signature versions include v4 and anonymous."
74 );
75 }
76
77 /**
78 * Default SDK signature provider.
79 *
80 * @return callable
81 */
82 public static function defaultProvider()
83 {
84 return self::memoize(self::version());
85 }
86
87 /**
88 * Creates a signature provider that caches previously created signature
89 * objects. The computed cache key is the concatenation of the version,
90 * service, and region.
91 *
92 * @param callable $provider Signature provider to wrap.
93 *
94 * @return callable
95 */
96 public static function memoize(callable $provider)
97 {
98 $cache = [];
99 return function ($version, $service, $region) use (&$cache, $provider) {
100 $key = "($version)($service)($region)";
101 if (!isset($cache[$key])) {
102 $cache[$key] = $provider($version, $service, $region);
103 }
104 return $cache[$key];
105 };
106 }
107
108 /**
109 * Creates signature objects from known signature versions.
110 *
111 * This provider currently recognizes the following signature versions:
112 *
113 * - v4: Signature version 4.
114 * - anonymous: Does not sign requests.
115 *
116 * @return callable
117 */
118 public static function version()
119 {
120 return function ($version, $service, $region) {
121 switch ($version) {
122 case 'v4-s3express':
123 return new S3ExpressSignature($service, $region);
124 case 's3v4':
125 case 'v4':
126 return !empty(self::$s3v4SignedServices[$service])
127 ? new S3SignatureV4($service, $region)
128 : new SignatureV4($service, $region);
129 case 'v4a':
130 return !empty(self::$s3v4SignedServices[$service])
131 ? new S3SignatureV4($service, $region, ['use_v4a' => true])
132 : new SignatureV4($service, $region, ['use_v4a' => true]);
133 case 'v4-unsigned-body':
134 return !empty(self::$s3v4SignedServices[$service])
135 ? new S3SignatureV4($service, $region, ['unsigned-body' => 'true'])
136 : new SignatureV4($service, $region, ['unsigned-body' => 'true']);
137 case 'bearer':
138 return new BearerTokenAuthorization();
139 case 'anonymous':
140 return new AnonymousSignature();
141 default:
142 return null;
143 }
144 };
145 }
146 }
147