| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Auth; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Api\Service; |
| 6 |
use Dudlewebs\WPMCS\s3\Aws\Auth\Exception\UnresolvedAuthSchemeException; |
| 7 |
use Dudlewebs\WPMCS\s3\Aws\CommandInterface; |
| 8 |
use Closure; |
| 9 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\Promise; |
| 10 |
/** |
| 11 |
* Handles auth scheme resolution. If a service models and auth scheme using |
| 12 |
* the `auth` trait and the operation or metadata levels, this middleware will |
| 13 |
* attempt to select the first compatible auth scheme it encounters and apply its |
| 14 |
* signature version to the command's `@context` property bag. |
| 15 |
* |
| 16 |
* IMPORTANT: this middleware must be added to the "build" step. |
| 17 |
* |
| 18 |
* @internal |
| 19 |
*/ |
| 20 |
class AuthSelectionMiddleware |
| 21 |
{ |
| 22 |
/** @var callable */ |
| 23 |
private $nextHandler; |
| 24 |
/** @var AuthSchemeResolverInterface */ |
| 25 |
private $authResolver; |
| 26 |
/** @var Service */ |
| 27 |
private $api; |
| 28 |
/** @var array|null */ |
| 29 |
private ?array $configuredAuthSchemes; |
| 30 |
/** |
| 31 |
* Create a middleware wrapper function |
| 32 |
* |
| 33 |
* @param AuthSchemeResolverInterface $authResolver |
| 34 |
* @param Service $api |
| 35 |
* @param array|null $configuredAuthSchemes |
| 36 |
* |
| 37 |
* @return Closure |
| 38 |
*/ |
| 39 |
public static function wrap(AuthSchemeResolverInterface $authResolver, Service $api, ?array $configuredAuthSchemes) : Closure |
| 40 |
{ |
| 41 |
return function (callable $handler) use($authResolver, $api, $configuredAuthSchemes) { |
| 42 |
return new self($handler, $authResolver, $api, $configuredAuthSchemes); |
| 43 |
}; |
| 44 |
} |
| 45 |
/** |
| 46 |
* @param callable $nextHandler |
| 47 |
* @param AuthSchemeResolverInterface $authResolver |
| 48 |
* @param Service $api |
| 49 |
* @param array|null $configuredAuthSchemes |
| 50 |
*/ |
| 51 |
public function __construct(callable $nextHandler, AuthSchemeResolverInterface $authResolver, Service $api, ?array $configuredAuthSchemes = null) |
| 52 |
{ |
| 53 |
$this->nextHandler = $nextHandler; |
| 54 |
$this->authResolver = $authResolver; |
| 55 |
$this->api = $api; |
| 56 |
$this->configuredAuthSchemes = $configuredAuthSchemes; |
| 57 |
} |
| 58 |
/** |
| 59 |
* @param CommandInterface $command |
| 60 |
* |
| 61 |
* @return Promise |
| 62 |
*/ |
| 63 |
public function __invoke(CommandInterface $command) |
| 64 |
{ |
| 65 |
$nextHandler = $this->nextHandler; |
| 66 |
$serviceAuth = $this->api->getMetadata('auth') ?: []; |
| 67 |
$operation = $this->api->getOperation($command->getName()); |
| 68 |
$operationAuth = $operation['auth'] ?? []; |
| 69 |
$unsignedPayload = $operation['unsignedpayload'] ?? \false; |
| 70 |
$resolvableAuth = $operationAuth ?: $serviceAuth; |
| 71 |
if (!empty($resolvableAuth)) { |
| 72 |
if (isset($command['@context']['auth_scheme_resolver']) && $command['@context']['auth_scheme_resolver'] instanceof AuthSchemeResolverInterface) { |
| 73 |
$resolver = $command['@context']['auth_scheme_resolver']; |
| 74 |
} else { |
| 75 |
$resolver = $this->authResolver; |
| 76 |
} |
| 77 |
try { |
| 78 |
$authSchemeList = $this->buildAuthSchemeList($resolvableAuth, $command['@context']['auth_scheme_preference'] ?? null); |
| 79 |
$selectedAuthScheme = $resolver->selectAuthScheme($authSchemeList, ['unsigned_payload' => $unsignedPayload]); |
| 80 |
if (!empty($selectedAuthScheme)) { |
| 81 |
$command['@context']['signature_version'] = $selectedAuthScheme; |
| 82 |
} |
| 83 |
} catch (UnresolvedAuthSchemeException $ignored) { |
| 84 |
// There was an error resolving auth |
| 85 |
// The signature version will fall back to the modeled `signatureVersion` |
| 86 |
// or auth schemes resolved during endpoint resolution |
| 87 |
} |
| 88 |
} |
| 89 |
return $nextHandler($command); |
| 90 |
} |
| 91 |
/** |
| 92 |
* Prioritizes auth schemes according to user preference order. |
| 93 |
* User-preferred schemes that are available will be placed first, |
| 94 |
* followed by remaining available schemes. |
| 95 |
* |
| 96 |
* @param array $resolvableAuthSchemeList Available auth schemes |
| 97 |
* @param array|null $commandConfiguredAuthSchemes Command-level preferences (overrides config) |
| 98 |
* |
| 99 |
* @return array Reordered auth schemes with user preferences first |
| 100 |
*/ |
| 101 |
private function buildAuthSchemeList(array $resolvableAuthSchemeList, ?array $commandConfiguredAuthSchemes) : array |
| 102 |
{ |
| 103 |
$userConfiguredAuthSchemes = $commandConfiguredAuthSchemes ?? $this->configuredAuthSchemes; |
| 104 |
if (empty($userConfiguredAuthSchemes)) { |
| 105 |
return $resolvableAuthSchemeList; |
| 106 |
} |
| 107 |
$prioritizedAuthSchemes = \array_intersect($userConfiguredAuthSchemes, $resolvableAuthSchemeList); |
| 108 |
// Get remaining schemes not in user preferences |
| 109 |
$remainingAuthSchemes = \array_diff($resolvableAuthSchemeList, $prioritizedAuthSchemes); |
| 110 |
return \array_merge($prioritizedAuthSchemes, $remainingAuthSchemes); |
| 111 |
} |
| 112 |
} |
| 113 |
|