| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
namespace Aws\Auth;
|
|
|
3 |
|
|
|
4 |
use Aws\Api\Service;
|
|
|
5 |
use Aws\CommandInterface;
|
|
|
6 |
use Closure;
|
|
|
7 |
use GuzzleHttp\Promise\Promise;
|
|
|
8 |
|
|
|
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 |
|
|
|
24 |
/** @var AuthSchemeResolverInterface */
|
|
|
25 |
private $authResolver;
|
|
|
26 |
|
|
|
27 |
/** @var Service */
|
|
|
28 |
private $api;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Create a middleware wrapper function
|
|
|
32 |
*
|
|
|
33 |
* @param AuthSchemeResolverInterface $authResolver
|
|
|
34 |
* @param Service $api
|
|
|
35 |
* @return Closure
|
|
|
36 |
*/
|
|
|
37 |
public static function wrap(
|
|
|
38 |
AuthSchemeResolverInterface $authResolver,
|
|
|
39 |
Service $api
|
|
|
40 |
): Closure
|
|
|
41 |
{
|
|
|
42 |
return function (callable $handler) use ($authResolver, $api) {
|
|
|
43 |
return new self($handler, $authResolver, $api);
|
|
|
44 |
};
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* @param callable $nextHandler
|
|
|
49 |
* @param $authResolver
|
|
|
50 |
* @param callable $identityProvider
|
|
|
51 |
* @param Service $api
|
|
|
52 |
*/
|
|
|
53 |
public function __construct(
|
|
|
54 |
callable $nextHandler,
|
|
|
55 |
AuthSchemeResolverInterface $authResolver,
|
|
|
56 |
Service $api
|
|
|
57 |
)
|
|
|
58 |
{
|
|
|
59 |
$this->nextHandler = $nextHandler;
|
|
|
60 |
$this->authResolver = $authResolver;
|
|
|
61 |
$this->api = $api;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* @param CommandInterface $command
|
|
|
66 |
*
|
|
|
67 |
* @return Promise
|
|
|
68 |
*/
|
|
|
69 |
public function __invoke(CommandInterface $command)
|
|
|
70 |
{
|
|
|
71 |
$nextHandler = $this->nextHandler;
|
|
|
72 |
$serviceAuth = $this->api->getMetadata('auth') ?: [];
|
|
|
73 |
$operation = $this->api->getOperation($command->getName());
|
|
|
74 |
$operationAuth = $operation['auth'] ?? [];
|
|
|
75 |
$unsignedPayload = $operation['unsignedpayload'] ?? false;
|
|
|
76 |
$resolvableAuth = $operationAuth ?: $serviceAuth;
|
|
|
77 |
|
|
|
78 |
if (!empty($resolvableAuth)) {
|
|
|
79 |
if (isset($command['@context']['auth_scheme_resolver'])
|
|
|
80 |
&& $command['@context']['auth_scheme_resolver'] instanceof AuthSchemeResolverInterface
|
|
|
81 |
){
|
|
|
82 |
$resolver = $command['@context']['auth_scheme_resolver'];
|
|
|
83 |
} else {
|
|
|
84 |
$resolver = $this->authResolver;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
$selectedAuthScheme = $resolver->selectAuthScheme(
|
|
|
88 |
$resolvableAuth,
|
|
|
89 |
['unsigned_payload' => $unsignedPayload]
|
|
|
90 |
);
|
|
|
91 |
|
|
|
92 |
if (!empty($selectedAuthScheme)) {
|
|
|
93 |
$command['@context']['signature_version'] = $selectedAuthScheme;
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
return $nextHandler($command);
|
|
|
98 |
}
|
|
|
99 |
}
|