Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Signature/SignatureProvider.php

raw symbols SCIP
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-outposts' => true,
48 's3-object-lambda' => true,
49 's3express' => true
50 ];
51
52 /**
53 * Resolves and signature provider and ensures a non-null return value.
54 *
55 * @param callable $provider Provider function to invoke.
56 * @param string $version Signature version.
57 * @param string $service Service name.
58 * @param string $region Region name.
59 *
60 * @return SignatureInterface
61 * @throws UnresolvedSignatureException
62 */
63 public static function resolve(callable $provider, $version, $service, $region)
64 {
65 $result = $provider($version, $service, $region);
66 if ($result instanceof SignatureInterface
67 || $result instanceof BearerTokenAuthorization
68 || $result instanceof DpopSignature
69 ) {
70 return $result;
71 }
72
73 throw new UnresolvedSignatureException(
74 "Unable to resolve a signature for $version/$service/$region.\n"
75 . "Valid signature versions include v4 and anonymous."
76 );
77 }
78
79 /**
80 * Default SDK signature provider.
81 *
82 * @return callable
83 */
84 public static function defaultProvider()
85 {
86 return self::memoize(self::version());
87 }
88
89 /**
90 * Creates a signature provider that caches previously created signature
91 * objects. The computed cache key is the concatenation of the version,
92 * service, and region.
93 *
94 * @param callable $provider Signature provider to wrap.
95 *
96 * @return callable
97 */
98 public static function memoize(callable $provider)
99 {
100 $cache = [];
101 return function ($version, $service, $region) use (&$cache, $provider) {
102 $key = "($version)($service)($region)";
103 if (!isset($cache[$key])) {
104 $cache[$key] = $provider($version, $service, $region);
105 }
106 return $cache[$key];
107 };
108 }
109
110 /**
111 * Creates signature objects from known signature versions.
112 *
113 * This provider currently recognizes the following signature versions:
114 *
115 * - v4: Signature version 4.
116 * - anonymous: Does not sign requests.
117 *
118 * @return callable
119 */
120 public static function version()
121 {
122 return function ($version, $service, $region) {
123 switch ($version) {
124 case 'v4-s3express':
125 return new S3ExpressSignature($service, $region);
126 case 's3v4':
127 case 'v4':
128 return !empty(self::$s3v4SignedServices[$service])
129 ? new S3SignatureV4($service, $region)
130 : new SignatureV4($service, $region);
131 case 'v4a':
132 return !empty(self::$s3v4SignedServices[$service])
133 ? new S3SignatureV4($service, $region, ['use_v4a' => true])
134 : new SignatureV4($service, $region, ['use_v4a' => true]);
135 case 'v4-unsigned-body':
136 return !empty(self::$s3v4SignedServices[$service])
137 ? new S3SignatureV4($service, $region, ['unsigned-body' => 'true'])
138 : new SignatureV4($service, $region, ['unsigned-body' => 'true']);
139 case 'bearer':
140 return new BearerTokenAuthorization();
141 case 'anonymous':
142 return new AnonymousSignature();
143 case 'dpop':
144 return new DpopSignature($service);
145 default:
146 return null;
147 }
148 };
149 }
150 }