aws_smithy_runtime_api/client/
auth.rs1use crate::box_error::BoxError;
9use crate::client::identity::{Identity, SharedIdentityResolver};
10use crate::client::orchestrator::HttpRequest;
11use crate::client::runtime_components::sealed::ValidateConfig;
12use crate::client::runtime_components::{GetIdentityResolver, RuntimeComponents};
13use crate::impl_shared_conversions;
14use aws_smithy_types::config_bag::{ConfigBag, FrozenLayer, Storable, StoreReplace};
15use aws_smithy_types::type_erasure::TypeErasedBox;
16use aws_smithy_types::Document;
17use std::borrow::Cow;
18use std::fmt;
19use std::sync::Arc;
20
21#[cfg(feature = "http-auth")]
23pub mod http;
24
25pub mod static_resolver;
27
28#[derive(Clone, Debug)]
32pub struct AuthSchemeOption {
33 scheme_id: AuthSchemeId,
34 properties: Option<FrozenLayer>,
35}
36
37impl AuthSchemeOption {
38 pub fn builder() -> AuthSchemeOptionBuilder {
40 AuthSchemeOptionBuilder::default()
41 }
42
43 pub fn scheme_id(&self) -> &AuthSchemeId {
45 &self.scheme_id
46 }
47
48 pub fn properties(&self) -> Option<FrozenLayer> {
53 self.properties.clone()
54 }
55}
56
57impl From<AuthSchemeId> for AuthSchemeOption {
58 fn from(auth_scheme_id: AuthSchemeId) -> Self {
59 AuthSchemeOption::builder()
60 .scheme_id(auth_scheme_id)
61 .build()
62 .expect("required fields set")
63 }
64}
65
66#[derive(Debug, Default)]
68pub struct AuthSchemeOptionBuilder {
69 scheme_id: Option<AuthSchemeId>,
70 properties: Option<FrozenLayer>,
71}
72
73impl AuthSchemeOptionBuilder {
74 pub fn scheme_id(mut self, auth_scheme_id: AuthSchemeId) -> Self {
76 self.set_scheme_id(Some(auth_scheme_id));
77 self
78 }
79
80 pub fn set_scheme_id(&mut self, auth_scheme_id: Option<AuthSchemeId>) {
82 self.scheme_id = auth_scheme_id;
83 }
84
85 pub fn properties(mut self, properties: FrozenLayer) -> Self {
87 self.set_properties(Some(properties));
88 self
89 }
90
91 pub fn set_properties(&mut self, properties: Option<FrozenLayer>) {
93 self.properties = properties;
94 }
95
96 pub fn build(self) -> Result<AuthSchemeOption, AuthSchemeOptionBuilderError> {
98 let scheme_id = self
99 .scheme_id
100 .ok_or(ErrorKind::MissingRequiredField("auth_scheme_id"))?;
101 Ok(AuthSchemeOption {
102 scheme_id,
103 properties: self.properties,
104 })
105 }
106}
107
108#[derive(Debug)]
109enum ErrorKind {
110 MissingRequiredField(&'static str),
111}
112
113impl From<ErrorKind> for AuthSchemeOptionBuilderError {
114 fn from(kind: ErrorKind) -> Self {
115 Self { kind }
116 }
117}
118
119#[derive(Debug)]
121pub struct AuthSchemeOptionBuilderError {
122 kind: ErrorKind,
123}
124
125impl fmt::Display for AuthSchemeOptionBuilderError {
126 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127 match self.kind {
128 ErrorKind::MissingRequiredField(name) => {
129 write!(f, "`{name}` is required")
130 }
131 }
132 }
133}
134
135impl std::error::Error for AuthSchemeOptionBuilderError {}
136
137#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
143pub struct AuthSchemeId {
144 scheme_id: Cow<'static, str>,
145}
146
147impl AsRef<AuthSchemeId> for AuthSchemeId {
149 fn as_ref(&self) -> &AuthSchemeId {
150 self
151 }
152}
153
154impl AuthSchemeId {
155 pub const fn new(scheme_id: &'static str) -> Self {
157 Self {
158 scheme_id: Cow::Borrowed(scheme_id),
159 }
160 }
161
162 #[deprecated(
164 note = "This function is no longer functional. Use `inner` instead",
165 since = "1.8.0"
166 )]
167 pub const fn as_str(&self) -> &'static str {
168 match self.scheme_id {
169 Cow::Borrowed(val) => val,
170 Cow::Owned(_) => {
171 ""
173 }
174 }
175 }
176
177 pub fn inner(&self) -> &str {
179 &self.scheme_id
180 }
181}
182
183impl From<&'static str> for AuthSchemeId {
184 fn from(scheme_id: &'static str) -> Self {
185 Self::new(scheme_id)
186 }
187}
188
189impl From<Cow<'static, str>> for AuthSchemeId {
190 fn from(scheme_id: Cow<'static, str>) -> Self {
191 Self { scheme_id }
192 }
193}
194
195#[derive(Debug)]
205pub struct AuthSchemeOptionResolverParams(TypeErasedBox);
206
207impl AuthSchemeOptionResolverParams {
208 pub fn new<T: fmt::Debug + Send + Sync + 'static>(params: T) -> Self {
210 Self(TypeErasedBox::new(params))
211 }
212
213 pub fn get<T: fmt::Debug + Send + Sync + 'static>(&self) -> Option<&T> {
215 self.0.downcast_ref()
216 }
217}
218
219impl Storable for AuthSchemeOptionResolverParams {
220 type Storer = StoreReplace<Self>;
221}
222
223new_type_future! {
224 #[doc = "Future for [`ResolveAuthSchemeOptions::resolve_auth_scheme_options_v2`]."]
225 pub struct AuthSchemeOptionsFuture<'a, Vec<AuthSchemeOption>, BoxError>;
226}
227
228pub trait ResolveAuthSchemeOptions: Send + Sync + fmt::Debug {
242 #[deprecated(
243 note = "This method is deprecated, use `resolve_auth_scheme_options_v2` instead.",
244 since = "1.8.0"
245 )]
246 fn resolve_auth_scheme_options(
248 &self,
249 _params: &AuthSchemeOptionResolverParams,
250 ) -> Result<Cow<'_, [AuthSchemeId]>, BoxError> {
251 unimplemented!("This method is deprecated, use `resolve_auth_scheme_options_v2` instead.");
252 }
253
254 #[allow(deprecated)]
255 fn resolve_auth_scheme_options_v2<'a>(
257 &'a self,
258 params: &'a AuthSchemeOptionResolverParams,
259 _cfg: &'a ConfigBag,
260 _runtime_components: &'a RuntimeComponents,
261 ) -> AuthSchemeOptionsFuture<'a> {
262 AuthSchemeOptionsFuture::ready({
263 self.resolve_auth_scheme_options(params).map(|options| {
264 options
265 .iter()
266 .cloned()
267 .map(|scheme_id| {
268 AuthSchemeOption::builder()
269 .scheme_id(scheme_id)
270 .build()
271 .expect("required fields set")
272 })
273 .collect::<Vec<_>>()
274 })
275 })
276 }
277}
278
279#[derive(Clone, Debug)]
281pub struct SharedAuthSchemeOptionResolver(Arc<dyn ResolveAuthSchemeOptions>);
282
283impl SharedAuthSchemeOptionResolver {
284 pub fn new(auth_scheme_option_resolver: impl ResolveAuthSchemeOptions + 'static) -> Self {
286 Self(Arc::new(auth_scheme_option_resolver))
287 }
288}
289
290impl ResolveAuthSchemeOptions for SharedAuthSchemeOptionResolver {
291 #[allow(deprecated)]
292 fn resolve_auth_scheme_options(
293 &self,
294 params: &AuthSchemeOptionResolverParams,
295 ) -> Result<Cow<'_, [AuthSchemeId]>, BoxError> {
296 (*self.0).resolve_auth_scheme_options(params)
297 }
298
299 fn resolve_auth_scheme_options_v2<'a>(
300 &'a self,
301 params: &'a AuthSchemeOptionResolverParams,
302 cfg: &'a ConfigBag,
303 runtime_components: &'a RuntimeComponents,
304 ) -> AuthSchemeOptionsFuture<'a> {
305 (*self.0).resolve_auth_scheme_options_v2(params, cfg, runtime_components)
306 }
307}
308
309impl_shared_conversions!(
310 convert SharedAuthSchemeOptionResolver
311 from ResolveAuthSchemeOptions
312 using SharedAuthSchemeOptionResolver::new
313);
314
315pub trait AuthScheme: Send + Sync + fmt::Debug {
320 fn scheme_id(&self) -> AuthSchemeId;
326
327 fn identity_resolver(
336 &self,
337 identity_resolvers: &dyn GetIdentityResolver,
338 ) -> Option<SharedIdentityResolver>;
339
340 fn signer(&self) -> &dyn Sign;
342}
343
344#[derive(Clone, Debug)]
346pub struct SharedAuthScheme(Arc<dyn AuthScheme>);
347
348impl SharedAuthScheme {
349 pub fn new(auth_scheme: impl AuthScheme + 'static) -> Self {
351 Self(Arc::new(auth_scheme))
352 }
353}
354
355impl AuthScheme for SharedAuthScheme {
356 fn scheme_id(&self) -> AuthSchemeId {
357 self.0.scheme_id()
358 }
359
360 fn identity_resolver(
361 &self,
362 identity_resolvers: &dyn GetIdentityResolver,
363 ) -> Option<SharedIdentityResolver> {
364 self.0.identity_resolver(identity_resolvers)
365 }
366
367 fn signer(&self) -> &dyn Sign {
368 self.0.signer()
369 }
370}
371
372impl ValidateConfig for SharedAuthScheme {}
373
374impl_shared_conversions!(convert SharedAuthScheme from AuthScheme using SharedAuthScheme::new);
375
376pub trait Sign: Send + Sync + fmt::Debug {
378 fn sign_http_request(
382 &self,
383 request: &mut HttpRequest,
384 identity: &Identity,
385 auth_scheme_endpoint_config: AuthSchemeEndpointConfig<'_>,
386 runtime_components: &RuntimeComponents,
387 config_bag: &ConfigBag,
388 ) -> Result<(), BoxError>;
389}
390
391#[non_exhaustive]
397#[derive(Clone, Debug)]
398pub struct AuthSchemeEndpointConfig<'a>(Option<&'a Document>);
399
400impl<'a> AuthSchemeEndpointConfig<'a> {
401 pub fn empty() -> Self {
403 Self(None)
404 }
405
406 pub fn as_document(&self) -> Option<&'a Document> {
408 self.0
409 }
410}
411
412impl<'a> From<Option<&'a Document>> for AuthSchemeEndpointConfig<'a> {
413 fn from(value: Option<&'a Document>) -> Self {
414 Self(value)
415 }
416}
417
418impl<'a> From<&'a Document> for AuthSchemeEndpointConfig<'a> {
419 fn from(value: &'a Document) -> Self {
420 Self(Some(value))
421 }
422}
423
424#[derive(Clone, Debug, Default, Eq, PartialEq)]
430pub struct AuthSchemePreference {
431 preference_list: Vec<AuthSchemeId>,
432}
433
434impl Storable for AuthSchemePreference {
435 type Storer = StoreReplace<Self>;
436}
437
438impl IntoIterator for AuthSchemePreference {
439 type Item = AuthSchemeId;
440 type IntoIter = std::vec::IntoIter<Self::Item>;
441
442 fn into_iter(self) -> Self::IntoIter {
443 self.preference_list.into_iter()
444 }
445}
446
447impl<T> From<T> for AuthSchemePreference
448where
449 T: AsRef<[AuthSchemeId]>,
450{
451 fn from(slice: T) -> Self {
452 AuthSchemePreference {
453 preference_list: slice.as_ref().to_vec(),
454 }
455 }
456}