Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Auth/AuthSelectionMiddleware.php

raw symbols SCIP
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 * 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
25 /** @var AuthSchemeResolverInterface */
26 private $authResolver;
27
28 /** @var Service */
29 private $api;
30
31 /** @var array|null */
32 private ?array $configuredAuthSchemes;
33
34 /**
35 * Create a middleware wrapper function
36 *
37 * @param AuthSchemeResolverInterface $authResolver
38 * @param Service $api
39 * @param array|null $configuredAuthSchemes
40 *
41 * @return Closure
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 * @param callable $nextHandler
60 * @param AuthSchemeResolverInterface $authResolver
61 * @param Service $api
62 * @param array|null $configuredAuthSchemes
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 * @param CommandInterface $command
79 *
80 * @return Promise
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 // There was an error resolving auth
116 // The signature version will fall back to the modeled `signatureVersion`
117 // or auth schemes resolved during endpoint resolution
118 }
119 }
120
121 return $nextHandler($command);
122 }
123
124 /**
125 * Prioritizes auth schemes according to user preference order.
126 * User-preferred schemes that are available will be placed first,
127 * followed by remaining available schemes.
128 *
129 * @param array $resolvableAuthSchemeList Available auth schemes
130 * @param array|null $commandConfiguredAuthSchemes Command-level preferences (overrides config)
131 *
132 * @return array Reordered auth schemes with user preferences first
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 // Get remaining schemes not in user preferences
152 $remainingAuthSchemes = array_diff(
153 $resolvableAuthSchemeList,
154 $prioritizedAuthSchemes
155 );
156
157 return array_merge($prioritizedAuthSchemes, $remainingAuthSchemes);
158 }
159 }