123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace Aws\Auth;
- use Aws\Api\Service;
- use Aws\CommandInterface;
- use Closure;
- use GuzzleHttp\Promise\Promise;
- class AuthSelectionMiddleware
- {
-
- private $nextHandler;
-
- private $authResolver;
-
- private $api;
-
- public static function wrap(
- AuthSchemeResolverInterface $authResolver,
- Service $api
- ): Closure
- {
- return function (callable $handler) use ($authResolver, $api) {
- return new self($handler, $authResolver, $api);
- };
- }
-
- public function __construct(
- callable $nextHandler,
- AuthSchemeResolverInterface $authResolver,
- Service $api
- )
- {
- $this->nextHandler = $nextHandler;
- $this->authResolver = $authResolver;
- $this->api = $api;
- }
-
- public function __invoke(CommandInterface $command)
- {
- $nextHandler = $this->nextHandler;
- $serviceAuth = $this->api->getMetadata('auth') ?: [];
- $operation = $this->api->getOperation($command->getName());
- $operationAuth = $operation['auth'] ?? [];
- $unsignedPayload = $operation['unsignedpayload'] ?? false;
- $resolvableAuth = $operationAuth ?: $serviceAuth;
- if (!empty($resolvableAuth)) {
- if (isset($command['@context']['auth_scheme_resolver'])
- && $command['@context']['auth_scheme_resolver'] instanceof AuthSchemeResolverInterface
- ){
- $resolver = $command['@context']['auth_scheme_resolver'];
- } else {
- $resolver = $this->authResolver;
- }
- $selectedAuthScheme = $resolver->selectAuthScheme(
- $resolvableAuth,
- ['unsigned_payload' => $unsignedPayload]
- );
- if (!empty($selectedAuthScheme)) {
- $command['@context']['signature_version'] = $selectedAuthScheme;
- }
- }
- return $nextHandler($command);
- }
- }
|