1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! Utilities to sign HTTP requests.
//!
//! # Example: Signing an HTTP request
//!
//! **Note**: This requires `http0-compat` to be enabled.
//!
//! ```rust
//! # use aws_credential_types::Credentials;
//! use aws_smithy_runtime_api::client::identity::Identity;
//! # use aws_sigv4::http_request::SignableBody;
//! #[cfg(feature = "http1")]
//! fn test() -> Result<(), aws_sigv4::http_request::SigningError> {
//! use aws_sigv4::http_request::{sign, SigningSettings, SigningParams, SignableRequest};
//! use aws_sigv4::sign::v4;
//! use http0;
//! use std::time::SystemTime;
//!
//! // Set up information and settings for the signing
//! // You can obtain credentials from `SdkConfig`.
//! let identity = Credentials::new(
//!     "AKIDEXAMPLE",
//!     "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
//!     None,
//!     None,
//!     "hardcoded-credentials"
//! ).into();
//! let signing_settings = SigningSettings::default();
//! let signing_params = v4::SigningParams::builder()
//!     .identity(&identity)
//!     .region("us-east-1")
//!     .name("exampleservice")
//!     .time(SystemTime::now())
//!     .settings(signing_settings)
//!     .build()
//!     .unwrap()
//!     .into();
//! // Convert the HTTP request into a signable request
//! let signable_request = SignableRequest::new(
//!     "GET",
//!     "https://some-endpoint.some-region.amazonaws.com",
//!     std::iter::empty(),
//!     SignableBody::Bytes(&[])
//! ).expect("signable request");
//!
//! let mut my_req = http::Request::new("...");
//! // Sign and then apply the signature to the request
//! let (signing_instructions, _signature) = sign(signable_request, &signing_params)?.into_parts();
//! signing_instructions.apply_to_request_http1x(&mut my_req);
//! # Ok(())
//! # }
//! ```

mod canonical_request;
mod error;
mod settings;
mod sign;
mod uri_path_normalization;
mod url_escape;

#[cfg(test)]
pub(crate) mod test;

use crate::sign::v4;
#[cfg(feature = "sigv4a")]
use crate::sign::v4a;
use crate::SignatureVersion;
use aws_credential_types::Credentials;
pub use error::SigningError;
pub use settings::{
    PayloadChecksumKind, PercentEncodingMode, SessionTokenMode, SignatureLocation, SigningSettings,
    UriPathNormalizationMode,
};
pub use sign::{sign, SignableBody, SignableRequest, SigningInstructions};
use std::time::SystemTime;

// Individual Debug impls are responsible for redacting sensitive fields.
#[derive(Debug)]
#[non_exhaustive]
/// Parameters for signing an HTTP request.
pub enum SigningParams<'a> {
    /// Sign with the SigV4 algorithm
    V4(v4::SigningParams<'a, SigningSettings>),
    #[cfg(feature = "sigv4a")]
    /// Sign with the SigV4a algorithm
    V4a(v4a::SigningParams<'a, SigningSettings>),
}

impl<'a> From<v4::SigningParams<'a, SigningSettings>> for SigningParams<'a> {
    fn from(value: v4::SigningParams<'a, SigningSettings>) -> Self {
        Self::V4(value)
    }
}

#[cfg(feature = "sigv4a")]
impl<'a> From<v4a::SigningParams<'a, SigningSettings>> for SigningParams<'a> {
    fn from(value: v4a::SigningParams<'a, SigningSettings>) -> Self {
        Self::V4a(value)
    }
}

impl<'a> SigningParams<'a> {
    /// Return the credentials within the signing params.
    pub(crate) fn credentials(&self) -> Result<&Credentials, SigningError> {
        let identity = match self {
            Self::V4(v4::SigningParams { identity, .. }) => identity,
            #[cfg(feature = "sigv4a")]
            Self::V4a(v4a::SigningParams { identity, .. }) => identity,
        };

        identity
            .data::<Credentials>()
            .ok_or_else(SigningError::unsupported_identity_type)
    }

    /// If the signing params are for SigV4, return the region. Otherwise, return `None`.
    pub fn region(&self) -> Option<&str> {
        match self {
            SigningParams::V4(v4::SigningParams { region, .. }) => Some(region),
            #[allow(unreachable_patterns)]
            _ => None,
        }
    }

    #[cfg(feature = "sigv4a")]
    /// If the signing params are for SigV4a, return the region set. Otherwise, return `None`.
    pub fn region_set(&self) -> Option<&str> {
        match self {
            SigningParams::V4a(v4a::SigningParams { region_set, .. }) => Some(region_set),
            _ => None,
        }
    }

    /// Return a reference to the settings held by the signing params.
    pub fn settings(&self) -> &SigningSettings {
        match self {
            Self::V4(v4::SigningParams { settings, .. }) => settings,
            #[cfg(feature = "sigv4a")]
            Self::V4a(v4a::SigningParams { settings, .. }) => settings,
        }
    }

    /// Return a mutable reference to the settings held by the signing params.
    pub fn settings_mut(&mut self) -> &mut SigningSettings {
        match self {
            Self::V4(v4::SigningParams { settings, .. }) => settings,
            #[cfg(feature = "sigv4a")]
            Self::V4a(v4a::SigningParams { settings, .. }) => settings,
        }
    }

    #[cfg(test)]
    /// Set the [`PayloadChecksumKind`] for the signing params.
    pub fn set_payload_checksum_kind(&mut self, kind: PayloadChecksumKind) {
        let settings = self.settings_mut();

        settings.payload_checksum_kind = kind;
    }

    #[cfg(test)]
    /// Set the [`SessionTokenMode`] for the signing params.
    pub fn set_session_token_mode(&mut self, mode: SessionTokenMode) {
        let settings = self.settings_mut();

        settings.session_token_mode = mode;
    }

    /// Return a reference to the time in the signing params.
    pub fn time(&self) -> &SystemTime {
        match self {
            Self::V4(v4::SigningParams { time, .. }) => time,
            #[cfg(feature = "sigv4a")]
            Self::V4a(v4a::SigningParams { time, .. }) => time,
        }
    }

    /// Return a reference to the name in the signing params.
    pub fn name(&self) -> &str {
        match self {
            Self::V4(v4::SigningParams { name, .. }) => name,
            #[cfg(feature = "sigv4a")]
            Self::V4a(v4a::SigningParams { name, .. }) => name,
        }
    }

    /// Return the name of the configured signing algorithm.
    pub fn algorithm(&self) -> &'static str {
        match self {
            Self::V4(params) => params.algorithm(),
            #[cfg(feature = "sigv4a")]
            Self::V4a(params) => params.algorithm(),
        }
    }

    /// Return the name of the signing scheme
    pub fn signature_version(&self) -> SignatureVersion {
        match self {
            Self::V4(..) => SignatureVersion::V4,
            #[cfg(feature = "sigv4a")]
            Self::V4a(..) => SignatureVersion::V4a,
        }
    }
}