Skip to content

Commit 9a7d14c

Browse files
committed
Clean up sigv4 function
1 parent 12b9c33 commit 9a7d14c

File tree

1 file changed

+17
-32
lines changed

1 file changed

+17
-32
lines changed

src/client/auth/aws.rs

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ async fn authenticate_stream_inner(
138138
// &server_first.server_nonce,
139139
// )?;
140140

141-
// dbg!("authorization header: {}", &authorization_header);
142-
143141
// let mut client_second_payload = doc! {
144142
// "a": authorization_header,
145143
// "d": date.format(AWS_LONG_DATE_FMT).to_string(),
@@ -149,23 +147,14 @@ async fn authenticate_stream_inner(
149147
// client_second_payload.insert("t", security_token);
150148
// }
151149

152-
let sigv4_headers = compute_aws_sigv4_headers(
150+
let client_second_payload = compute_aws_sigv4_payload(
153151
creds,
154152
date,
155153
&server_first.sts_host,
156154
&server_first.server_nonce,
157155
)
158156
.await?;
159157

160-
let mut client_second_payload = doc! {
161-
"a": sigv4_headers.authorization,
162-
"d": sigv4_headers.date,
163-
};
164-
165-
if let Some(token) = sigv4_headers.token {
166-
client_second_payload.insert("t", token);
167-
}
168-
169158
let client_second_payload_bytes = client_second_payload.encode_to_vec()?;
170159

171160
let sasl_continue = SaslContinue::new(
@@ -226,18 +215,12 @@ pub(crate) async fn get_aws_credentials(credential: &Credential) -> Result<Crede
226215
}
227216
}
228217

229-
pub struct AwsSigV4Headers {
230-
pub authorization: String,
231-
pub date: String,
232-
pub token: Option<String>,
233-
}
234-
235-
pub async fn compute_aws_sigv4_headers(
218+
pub async fn compute_aws_sigv4_payload(
236219
creds: Credentials,
237220
date: DateTime<Utc>,
238221
host: &str,
239222
server_nonce: &[u8],
240-
) -> Result<AwsSigV4Headers> {
223+
) -> Result<Document> {
241224
let date_str = date.format("%Y%m%dT%H%M%SZ").to_string();
242225

243226
let region = if host == "sts.amazonaws.com" {
@@ -260,9 +243,9 @@ pub async fn compute_aws_sigv4_headers(
260243
.header("host", host)
261244
.header("content-type", "application/x-www-form-urlencoded")
262245
.header("content-length", body_bytes.len())
263-
.header("x-amz-date", date_str.clone())
246+
.header("x-amz-date", &date_str)
264247
.header("x-mongodb-gs2-cb-flag", "n")
265-
.header("x-mongodb-server-nonce", nonce_b64.clone());
248+
.header("x-mongodb-server-nonce", &nonce_b64);
266249

267250
if let Some(token) = creds.session_token() {
268251
builder = builder.header("x-amz-security-token", token);
@@ -307,33 +290,35 @@ pub async fn compute_aws_sigv4_headers(
307290
.into_parts();
308291

309292
signing_instructions.apply_to_request_http1x(&mut request);
310-
dbg!("ending computation part of compute_aws_sigv4_headers");
311293

312294
// Extract the Authorization header
313295
let headers = request.headers();
314-
let authorization = headers
296+
let authorization_header = headers
315297
.get("authorization")
316298
.ok_or_else(|| Error::authentication_error(MECH_NAME, "Missing authorization header"))?
317299
.to_str()
318300
.map_err(|e| Error::authentication_error(MECH_NAME, &format!("Invalid header value: {e}")))?
319301
.to_string();
320-
dbg!("authorization header: {}", &authorization);
321302

322-
let token = headers
303+
let token_header = headers
323304
.get("x-amz-security-token")
324305
.map(|v| {
325306
v.to_str().map(|s| s.to_string()).map_err(|e| {
326307
Error::authentication_error(MECH_NAME, &format!("Invalid token header: {e}"))
327308
})
328309
})
329310
.transpose()?;
330-
dbg!("token header: {}", &token);
331311

332-
Ok(AwsSigV4Headers {
333-
authorization,
334-
date: date_str,
335-
token,
336-
})
312+
let mut payload = doc! {
313+
"a": authorization_header,
314+
"d": date_str,
315+
};
316+
317+
if let Some(token) = token_header {
318+
payload.insert("t", token);
319+
}
320+
321+
Ok(payload)
337322
}
338323

339324
/// Contains the credentials for MONGODB-AWS authentication.

0 commit comments

Comments
 (0)