| 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\CommandInterface; |
| 7 |
use Closure; |
| 8 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\Promise; |
| 9 |
/** |
| 10 |
* Handles auth scheme resolution. If a service models and auth scheme using |
| 11 |
* the `auth` trait and the operation or metadata levels, this middleware will |
| 12 |
* attempt to select the first compatible auth scheme it encounters and apply its |
| 13 |
* signature version to the command's `@context` property bag. |
| 14 |
* |
| 15 |
* IMPORTANT: this middleware must be added to the "build" step. |
| 16 |
* |
| 17 |
* @internal |
| 18 |
*/ |
| 19 |
class AuthSelectionMiddleware |
| 20 |
{ |
| 21 |
/** @var callable */ |
| 22 |
private $nextHandler; |
| 23 |
/** @var AuthSchemeResolverInterface */ |
| 24 |
private $authResolver; |
| 25 |
/** @var Service */ |
| 26 |
private $api; |
| 27 |
/** |
| 28 |
* Create a middleware wrapper function |
| 29 |
* |
| 30 |
* @param AuthSchemeResolverInterface $authResolver |
| 31 |
* @param Service $api |
| 32 |
* @return Closure |
| 33 |
*/ |
| 34 |
public static function wrap(AuthSchemeResolverInterface $authResolver, Service $api) : Closure |
| 35 |
{ |
| 36 |
return function (callable $handler) use($authResolver, $api) { |
| 37 |
return new self($handler, $authResolver, $api); |
| 38 |
}; |
| 39 |
} |
| 40 |
/** |
| 41 |
* @param callable $nextHandler |
| 42 |
* @param $authResolver |
| 43 |
* @param callable $identityProvider |
| 44 |
* @param Service $api |
| 45 |
*/ |
| 46 |
public function __construct(callable $nextHandler, AuthSchemeResolverInterface $authResolver, Service $api) |
| 47 |
{ |
| 48 |
$this->nextHandler = $nextHandler; |
| 49 |
$this->authResolver = $authResolver; |
| 50 |
$this->api = $api; |
| 51 |
} |
| 52 |
/** |
| 53 |
* @param CommandInterface $command |
| 54 |
* |
| 55 |
* @return Promise |
| 56 |
*/ |
| 57 |
public function __invoke(CommandInterface $command) |
| 58 |
{ |
| 59 |
$nextHandler = $this->nextHandler; |
| 60 |
$serviceAuth = $this->api->getMetadata('auth') ?: []; |
| 61 |
$operation = $this->api->getOperation($command->getName()); |
| 62 |
$operationAuth = $operation['auth'] ?? []; |
| 63 |
$unsignedPayload = $operation['unsignedpayload'] ?? \false; |
| 64 |
$resolvableAuth = $operationAuth ?: $serviceAuth; |
| 65 |
if (!empty($resolvableAuth)) { |
| 66 |
if (isset($command['@context']['auth_scheme_resolver']) && $command['@context']['auth_scheme_resolver'] instanceof AuthSchemeResolverInterface) { |
| 67 |
$resolver = $command['@context']['auth_scheme_resolver']; |
| 68 |
} else { |
| 69 |
$resolver = $this->authResolver; |
| 70 |
} |
| 71 |
$selectedAuthScheme = $resolver->selectAuthScheme($resolvableAuth, ['unsigned_payload' => $unsignedPayload]); |
| 72 |
if (!empty($selectedAuthScheme)) { |
| 73 |
$command['@context']['signature_version'] = $selectedAuthScheme; |
| 74 |
} |
| 75 |
} |
| 76 |
return $nextHandler($command); |
| 77 |
} |
| 78 |
} |
| 79 |
|