| 1 |
<?php |
| 2 |
namespace Aws\Auth; |
| 3 |
|
| 4 |
use Aws\Api\Service; |
| 5 |
use Aws\Auth\Exception\UnresolvedAuthSchemeException; |
| 6 |
use Aws\CommandInterface; |
| 7 |
use Closure; |
| 8 |
use GuzzleHttp\Promise\Promise; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class AuthSelectionMiddleware |
| 21 |
{ |
| 22 |
|
| 23 |
private $nextHandler; |
| 24 |
|
| 25 |
|
| 26 |
private $authResolver; |
| 27 |
|
| 28 |
|
| 29 |
private $api; |
| 30 |
|
| 31 |
|
| 32 |
private ?array $configuredAuthSchemes; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public static function wrap( |
| 44 |
AuthSchemeResolverInterface $authResolver, |
| 45 |
Service $api, |
| 46 |
?array $configuredAuthSchemes |
| 47 |
): Closure |
| 48 |
{ |
| 49 |
return function (callable $handler) use ( |
| 50 |
$authResolver, |
| 51 |
$api, |
| 52 |
$configuredAuthSchemes |
| 53 |
) { |
| 54 |
return new self($handler, $authResolver, $api, $configuredAuthSchemes); |
| 55 |
}; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function __construct( |
| 65 |
callable $nextHandler, |
| 66 |
AuthSchemeResolverInterface $authResolver, |
| 67 |
Service $api, |
| 68 |
?array $configuredAuthSchemes = null |
| 69 |
) |
| 70 |
{ |
| 71 |
$this->nextHandler = $nextHandler; |
| 72 |
$this->authResolver = $authResolver; |
| 73 |
$this->api = $api; |
| 74 |
$this->configuredAuthSchemes = $configuredAuthSchemes; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function __invoke(CommandInterface $command) |
| 83 |
{ |
| 84 |
$nextHandler = $this->nextHandler; |
| 85 |
$serviceAuth = $this->api->getMetadata('auth') ?: []; |
| 86 |
$operation = $this->api->getOperation($command->getName()); |
| 87 |
$operationAuth = $operation['auth'] ?? []; |
| 88 |
$unsignedPayload = $operation['unsignedpayload'] ?? false; |
| 89 |
$resolvableAuth = $operationAuth ?: $serviceAuth; |
| 90 |
|
| 91 |
if (!empty($resolvableAuth)) { |
| 92 |
if (isset($command['@context']['auth_scheme_resolver']) |
| 93 |
&& $command['@context']['auth_scheme_resolver'] instanceof AuthSchemeResolverInterface |
| 94 |
){ |
| 95 |
$resolver = $command['@context']['auth_scheme_resolver']; |
| 96 |
} else { |
| 97 |
$resolver = $this->authResolver; |
| 98 |
} |
| 99 |
|
| 100 |
try { |
| 101 |
$authSchemeList = $this->buildAuthSchemeList( |
| 102 |
$resolvableAuth, |
| 103 |
$command['@context']['auth_scheme_preference'] |
| 104 |
?? null, |
| 105 |
); |
| 106 |
$selectedAuthScheme = $resolver->selectAuthScheme( |
| 107 |
$authSchemeList, |
| 108 |
['unsigned_payload' => $unsignedPayload] |
| 109 |
); |
| 110 |
|
| 111 |
if (!empty($selectedAuthScheme)) { |
| 112 |
$command['@context']['signature_version'] = $selectedAuthScheme; |
| 113 |
} |
| 114 |
} catch (UnresolvedAuthSchemeException $ignored) { |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return $nextHandler($command); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
private function buildAuthSchemeList( |
| 135 |
array $resolvableAuthSchemeList, |
| 136 |
?array $commandConfiguredAuthSchemes, |
| 137 |
): array |
| 138 |
{ |
| 139 |
$userConfiguredAuthSchemes = $commandConfiguredAuthSchemes |
| 140 |
?? $this->configuredAuthSchemes; |
| 141 |
|
| 142 |
if (empty($userConfiguredAuthSchemes)) { |
| 143 |
return $resolvableAuthSchemeList; |
| 144 |
} |
| 145 |
|
| 146 |
$prioritizedAuthSchemes = array_intersect( |
| 147 |
$userConfiguredAuthSchemes, |
| 148 |
$resolvableAuthSchemeList |
| 149 |
); |
| 150 |
|
| 151 |
|
| 152 |
$remainingAuthSchemes = array_diff( |
| 153 |
$resolvableAuthSchemeList, |
| 154 |
$prioritizedAuthSchemes |
| 155 |
); |
| 156 |
|
| 157 |
return array_merge($prioritizedAuthSchemes, $remainingAuthSchemes); |
| 158 |
} |
| 159 |
} |