18 18 | }
|
19 19 | }
|
20 20 |
|
21 21 | #[derive(Debug)]
|
22 22 | struct DowncastParams<T>(T);
|
23 23 | impl<T> ::aws_smithy_runtime_api::client::auth::ResolveAuthSchemeOptions for DowncastParams<T>
|
24 24 | where
|
25 25 | T: ResolveAuthScheme,
|
26 26 | {
|
27 27 | fn resolve_auth_scheme_options_v2<'a>(
|
28 28 | &'a self,
|
29 29 | params: &'a ::aws_smithy_runtime_api::client::auth::AuthSchemeOptionResolverParams,
|
30 30 | cfg: &'a ::aws_smithy_types::config_bag::ConfigBag,
|
31 31 | runtime_components: &'a ::aws_smithy_runtime_api::client::runtime_components::RuntimeComponents,
|
32 32 | ) -> ::aws_smithy_runtime_api::client::auth::AuthSchemeOptionsFuture<'a> {
|
33 33 | match params.get::<crate::config::auth::Params>() {
|
34 34 | ::std::option::Option::Some(concrete_params) => self.0.resolve_auth_scheme(concrete_params, cfg, runtime_components),
|
35 35 | ::std::option::Option::None => ::aws_smithy_runtime_api::client::auth::AuthSchemeOptionsFuture::ready(::std::result::Result::Err(
|
36 36 | "params of expected type was not present".into(),
|
37 37 | )),
|
38 38 | }
|
39 39 | }
|
40 40 | }
|
41 41 |
|
42 42 | /// The default auth scheme resolver
|
43 43 | #[derive(Debug)]
|
44 44 | #[allow(dead_code)]
|
45 45 | pub struct DefaultAuthSchemeResolver {
|
46 46 | service_defaults: Vec<::aws_smithy_runtime_api::client::auth::AuthSchemeOption>,
|
47 47 | operation_overrides: ::std::collections::HashMap<&'static str, Vec<::aws_smithy_runtime_api::client::auth::AuthSchemeOption>>,
|
48 + | preference: ::std::option::Option<::aws_smithy_runtime_api::client::auth::AuthSchemePreference>,
|
48 49 | }
|
49 50 |
|
50 51 | // TODO(https://github.com/smithy-lang/smithy-rs/issues/4177): Remove `allow(...)` once the issue is addressed.
|
51 52 | // When generating code for tests (e.g., `codegen-client-test`), this manual implementation
|
52 53 | // of the `Default` trait may appear as if it could be derived automatically.
|
53 54 | // However, that is not the case in production.
|
54 55 | #[allow(clippy::derivable_impls)]
|
55 56 | impl Default for DefaultAuthSchemeResolver {
|
56 57 | fn default() -> Self {
|
57 58 | Self {
|
58 59 | service_defaults: vec![
|
59 60 | ::aws_smithy_runtime_api::client::auth::AuthSchemeOption::builder()
|
60 61 | .scheme_id(::aws_runtime::auth::sigv4::SCHEME_ID)
|
61 62 | .build()
|
62 63 | .expect("required fields set"),
|
63 64 | #[cfg(feature = "sigv4a")]
|
64 65 | {
|
65 66 | ::aws_smithy_runtime_api::client::auth::AuthSchemeOption::builder()
|
66 67 | .scheme_id(::aws_runtime::auth::sigv4a::SCHEME_ID)
|
67 68 | .build()
|
68 69 | .expect("required fields set")
|
69 70 | },
|
70 71 | ::aws_smithy_runtime_api::client::auth::AuthSchemeOption::from(::aws_smithy_runtime::client::auth::no_auth::NO_AUTH_SCHEME_ID),
|
71 72 | ],
|
72 73 | operation_overrides: [(
|
73 74 | "WriteGetObjectResponse",
|
74 75 | vec![::aws_smithy_runtime_api::client::auth::AuthSchemeOption::builder()
|
75 76 | .scheme_id(::aws_runtime::auth::sigv4::SCHEME_ID)
|
76 77 | .properties({
|
77 78 | let mut layer = ::aws_smithy_types::config_bag::Layer::new("WriteGetObjectResponseAuthOptionProperties");
|
78 79 | layer.store_put(::aws_runtime::auth::PayloadSigningOverride::unsigned_payload());
|
79 80 | layer.freeze()
|
80 81 | })
|
81 82 | .build()
|
82 83 | .expect("required fields set")],
|
83 84 | )]
|
84 85 | .into(),
|
86 + | preference: ::std::option::Option::None,
|
85 87 | }
|
86 88 | }
|
87 89 | }
|
88 90 |
|
89 91 | impl crate::config::auth::ResolveAuthScheme for DefaultAuthSchemeResolver {
|
90 92 | fn resolve_auth_scheme<'a>(
|
91 93 | &'a self,
|
92 94 | params: &'a crate::config::auth::Params,
|
93 95 | _cfg: &'a ::aws_smithy_types::config_bag::ConfigBag,
|
94 96 | _runtime_components: &'a ::aws_smithy_runtime_api::client::runtime_components::RuntimeComponents,
|
95 97 | ) -> ::aws_smithy_runtime_api::client::auth::AuthSchemeOptionsFuture<'a> {
|
96 98 | let operation_name = params.operation_name();
|
97 99 |
|
98 100 | let modeled_auth_options = match self.operation_overrides.get(operation_name) {
|
99 101 | Some(overrides) => overrides,
|
100 102 | None => &self.service_defaults,
|
101 103 | };
|
102 104 |
|
103 105 | let _fut = ::aws_smithy_runtime_api::client::auth::AuthSchemeOptionsFuture::ready(Ok(modeled_auth_options.clone()));
|
104 106 |
|
105 107 | let _fut = ::aws_smithy_runtime_api::client::auth::AuthSchemeOptionsFuture::new(async move {
|
106 108 | crate::endpoint_auth::resolve_endpoint_based_auth_scheme_options(modeled_auth_options, _cfg, _runtime_components).await
|
107 109 | });
|
108 110 |
|
109 - | _fut
|
111 + | match &self.preference {
|
112 + | ::std::option::Option::Some(preference) => {
|
113 + | _fut.map_ok({
|
114 + | // maps auth scheme ID to the index in the preference list
|
115 + | let preference_map: ::std::collections::HashMap<_, _> = preference.clone().into_iter().enumerate().map(|(i, s)| (s, i)).collect();
|
116 + | move |auth_scheme_options| {
|
117 + | let (mut preferred, non_preferred): (::std::vec::Vec<_>, ::std::vec::Vec<_>) = auth_scheme_options
|
118 + | .into_iter()
|
119 + | .partition(|auth_scheme_option| preference_map.contains_key(auth_scheme_option.scheme_id()));
|
120 + |
|
121 + | preferred.sort_by_key(|opt| preference_map.get(opt.scheme_id()).expect("guaranteed by `partition`"));
|
122 + | preferred.extend(non_preferred);
|
123 + | preferred
|
124 + | }
|
125 + | })
|
126 + | }
|
127 + | ::std::option::Option::None => _fut,
|
128 + | }
|
129 + | }
|
130 + | }
|
131 + |
|
132 + | impl DefaultAuthSchemeResolver {
|
133 + | /// Set auth scheme preference to the default auth scheme resolver
|
134 + | pub fn with_auth_scheme_preference(
|
135 + | mut self,
|
136 + | preference: impl ::std::convert::Into<::aws_smithy_runtime_api::client::auth::AuthSchemePreference>,
|
137 + | ) -> Self {
|
138 + | self.preference = ::std::option::Option::Some(preference.into());
|
139 + | self
|
110 140 | }
|
111 141 | }
|
112 142 |
|
113 143 | /// Configuration parameters for resolving the correct auth scheme
|
114 144 | #[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::fmt::Debug)]
|
115 145 | pub struct Params {
|
116 146 | operation_name: ::std::borrow::Cow<'static, str>,
|
117 147 | }
|
118 148 | impl Params {
|
119 149 | /// Create a builder for [`Params`]
|
120 150 | pub fn builder() -> crate::config::auth::ParamsBuilder {
|
121 151 | crate::config::auth::ParamsBuilder::default()
|
122 152 | }
|
123 153 |
|
124 154 | /// Return the operation name for [`Params`]
|
125 155 | pub fn operation_name(&self) -> &str {
|
126 156 | self.operation_name.as_ref()
|
127 157 | }
|
128 158 | }
|
129 159 |
|
130 160 | #[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::default::Default, ::std::fmt::Debug)]
|
131 161 | /// Builder for [`Params`]
|
132 162 | pub struct ParamsBuilder {
|
133 163 | operation_name: ::std::option::Option<::std::borrow::Cow<'static, str>>,
|
134 164 | }
|
135 165 | impl ParamsBuilder {
|
136 166 | /// Set the operation name for the builder
|
137 167 | pub fn operation_name(self, operation_name: impl Into<::std::borrow::Cow<'static, str>>) -> Self {
|
138 168 | self.set_operation_name(::std::option::Option::Some(operation_name.into()))
|
139 169 | }
|