Skip to content

Commit 811219b

Browse files
Edward AlmondEdward Almond
authored andcommitted
Implement full SigV4 signature verification
- Calculate payload SHA256 hash - Build canonical request with signed headers - Build string to sign with scope - Derive signing key from secret key - Compare calculated signature with provided signature - Returns SignatureMismatch error on failure
1 parent c52bc5b commit 811219b

1 file changed

Lines changed: 45 additions & 11 deletions

File tree

ruststack-auth/src/sigv4.rs

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,54 @@ fn create_string_to_sign(
159159
)
160160
}
161161

162-
/// Verify a SigV4 signature (placeholder - full implementation needed)
162+
/// Verify a SigV4 signature
163163
#[allow(clippy::too_many_arguments)]
164164
pub fn verify_signature(
165-
_method: &str,
166-
_path: &str,
167-
_query_string: &str,
168-
_headers: &[(String, String)],
169-
_payload: &[u8],
170-
_auth_header: &AuthorizationHeader,
171-
_secret_key: &str,
172-
_timestamp: &DateTime<Utc>,
165+
method: &str,
166+
path: &str,
167+
query_string: &str,
168+
headers: &[(String, String)],
169+
payload: &[u8],
170+
auth_header: &AuthorizationHeader,
171+
secret_key: &str,
172+
timestamp: &DateTime<Utc>,
173173
) -> Result<bool, SigV4Error> {
174-
// TODO: Implement full verification
175-
// For now, accept all signatures (dev mode)
174+
let payload_hash = hex::encode(Sha256::digest(payload));
175+
176+
let canonical_request = create_canonical_request(
177+
method,
178+
path,
179+
query_string,
180+
headers,
181+
&auth_header.signed_headers,
182+
&payload_hash,
183+
);
184+
185+
let scope = format!(
186+
"{}/{}/{}/aws4_request",
187+
auth_header.date, auth_header.region, auth_header.service
188+
);
189+
190+
let string_to_sign = create_string_to_sign(
191+
&auth_header.algorithm,
192+
&auth_header.date,
193+
&scope,
194+
&canonical_request,
195+
);
196+
197+
let signing_key = derive_signing_key(
198+
secret_key,
199+
&auth_header.date,
200+
&auth_header.region,
201+
&auth_header.service,
202+
);
203+
204+
let expected_signature = hex::encode(hmac_sha256(&signing_key, string_to_sign.as_bytes()));
205+
206+
if expected_signature != auth_header.signature {
207+
return Err(SigV4Error::SignatureMismatch);
208+
}
209+
176210
Ok(true)
177211
}
178212

0 commit comments

Comments
 (0)