| 1 |
<?php |
| 2 |
namespace Aws\Signature; |
| 3 |
|
| 4 |
use Aws\Exception\UnresolvedSignatureException; |
| 5 |
use Aws\Token\BearerTokenAuthorization; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 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 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public static function defaultProvider() |
| 85 |
{ |
| 86 |
return self::memoize(self::version()); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 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 |
} |