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![::aws_smithy_runtime_api::client::auth::AuthSchemeOption::from(
|
59 60 | ::aws_smithy_runtime::client::auth::no_auth::NO_AUTH_SCHEME_ID,
|
60 61 | )],
|
61 62 | operation_overrides: ::std::collections::HashMap::new(),
|
63 + | preference: ::std::option::Option::None,
|
62 64 | }
|
63 65 | }
|
64 66 | }
|
65 67 |
|
66 68 | impl crate::config::auth::ResolveAuthScheme for DefaultAuthSchemeResolver {
|
67 69 | fn resolve_auth_scheme<'a>(
|
68 70 | &'a self,
|
69 71 | params: &'a crate::config::auth::Params,
|
70 72 | _cfg: &'a ::aws_smithy_types::config_bag::ConfigBag,
|
71 73 | _runtime_components: &'a ::aws_smithy_runtime_api::client::runtime_components::RuntimeComponents,
|
72 74 | ) -> ::aws_smithy_runtime_api::client::auth::AuthSchemeOptionsFuture<'a> {
|
73 75 | let operation_name = params.operation_name();
|
74 76 |
|
75 77 | let modeled_auth_options = match self.operation_overrides.get(operation_name) {
|
76 78 | Some(overrides) => overrides,
|
77 79 | None => &self.service_defaults,
|
78 80 | };
|
79 81 |
|
80 82 | let _fut = ::aws_smithy_runtime_api::client::auth::AuthSchemeOptionsFuture::ready(Ok(modeled_auth_options.clone()));
|
81 83 |
|
82 - | _fut
|
84 + | match &self.preference {
|
85 + | ::std::option::Option::Some(preference) => {
|
86 + | _fut.map_ok({
|
87 + | // maps auth scheme ID to the index in the preference list
|
88 + | let preference_map: ::std::collections::HashMap<_, _> = preference.clone().into_iter().enumerate().map(|(i, s)| (s, i)).collect();
|
89 + | move |auth_scheme_options| {
|
90 + | let (mut preferred, non_preferred): (::std::vec::Vec<_>, ::std::vec::Vec<_>) = auth_scheme_options
|
91 + | .into_iter()
|
92 + | .partition(|auth_scheme_option| preference_map.contains_key(auth_scheme_option.scheme_id()));
|
93 + |
|
94 + | preferred.sort_by_key(|opt| preference_map.get(opt.scheme_id()).expect("guaranteed by `partition`"));
|
95 + | preferred.extend(non_preferred);
|
96 + | preferred
|
97 + | }
|
98 + | })
|
99 + | }
|
100 + | ::std::option::Option::None => _fut,
|
101 + | }
|
102 + | }
|
103 + | }
|
104 + |
|
105 + | impl DefaultAuthSchemeResolver {
|
106 + | /// Set auth scheme preference to the default auth scheme resolver
|
107 + | pub fn with_auth_scheme_preference(
|
108 + | mut self,
|
109 + | preference: impl ::std::convert::Into<::aws_smithy_runtime_api::client::auth::AuthSchemePreference>,
|
110 + | ) -> Self {
|
111 + | self.preference = ::std::option::Option::Some(preference.into());
|
112 + | self
|
83 113 | }
|
84 114 | }
|
85 115 |
|
86 116 | /// Configuration parameters for resolving the correct auth scheme
|
87 117 | #[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::fmt::Debug)]
|
88 118 | pub struct Params {
|
89 119 | operation_name: ::std::borrow::Cow<'static, str>,
|
90 120 | }
|
91 121 | impl Params {
|
92 122 | /// Create a builder for [`Params`]
|
93 123 | pub fn builder() -> crate::config::auth::ParamsBuilder {
|
94 124 | crate::config::auth::ParamsBuilder::default()
|
95 125 | }
|
96 126 |
|
97 127 | /// Return the operation name for [`Params`]
|
98 128 | pub fn operation_name(&self) -> &str {
|
99 129 | self.operation_name.as_ref()
|
100 130 | }
|
101 131 | }
|
102 132 |
|
103 133 | #[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::default::Default, ::std::fmt::Debug)]
|
104 134 | /// Builder for [`Params`]
|
105 135 | pub struct ParamsBuilder {
|
106 136 | operation_name: ::std::option::Option<::std::borrow::Cow<'static, str>>,
|
107 137 | }
|
108 138 | impl ParamsBuilder {
|
109 139 | /// Set the operation name for the builder
|
110 140 | pub fn operation_name(self, operation_name: impl Into<::std::borrow::Cow<'static, str>>) -> Self {
|
111 141 | self.set_operation_name(::std::option::Option::Some(operation_name.into()))
|
112 142 | }
|