Function s3::signing::scope_string
source · pub fn scope_string(
datetime: &OffsetDateTime,
region: &Region
) -> Result<String, S3Error>Expand description
Generate an AWS scope string.
Examples found in repository?
src/signing.rs (line 161)
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
pub fn string_to_sign(
datetime: &OffsetDateTime,
region: &Region,
canonical_req: &str,
) -> Result<String, S3Error> {
let mut hasher = Sha256::default();
hasher.update(canonical_req.as_bytes());
let string_to = format!(
"AWS4-HMAC-SHA256\n{timestamp}\n{scope}\n{hash}",
timestamp = datetime.format(LONG_DATETIME)?,
scope = scope_string(datetime, region)?,
hash = hex::encode(hasher.finalize().as_slice())
);
Ok(string_to)
}
/// Generate the AWS signing key, derived from the secret key, date, region,
/// and service name.
pub fn signing_key(
datetime: &OffsetDateTime,
secret_key: &str,
region: &Region,
service: &str,
) -> Result<Vec<u8>, S3Error> {
let secret = format!("AWS4{}", secret_key);
let mut date_hmac = HmacSha256::new_from_slice(secret.as_bytes())?;
date_hmac.update(datetime.format(SHORT_DATE)?.as_bytes());
let mut region_hmac = HmacSha256::new_from_slice(&date_hmac.finalize().into_bytes())?;
region_hmac.update(region.to_string().as_bytes());
let mut service_hmac = HmacSha256::new_from_slice(®ion_hmac.finalize().into_bytes())?;
service_hmac.update(service.as_bytes());
let mut signing_hmac = HmacSha256::new_from_slice(&service_hmac.finalize().into_bytes())?;
signing_hmac.update(b"aws4_request");
Ok(signing_hmac.finalize().into_bytes().to_vec())
}
/// Generate the AWS authorization header.
pub fn authorization_header(
access_key: &str,
datetime: &OffsetDateTime,
region: &Region,
signed_headers: &str,
signature: &str,
) -> Result<String, S3Error> {
Ok(format!(
"AWS4-HMAC-SHA256 Credential={access_key}/{scope},\
SignedHeaders={signed_headers},Signature={signature}",
access_key = access_key,
scope = scope_string(datetime, region)?,
signed_headers = signed_headers,
signature = signature
))
}
pub fn authorization_query_params_no_sig(
access_key: &str,
datetime: &OffsetDateTime,
region: &Region,
expires: u32,
custom_headers: Option<&HeaderMap>,
token: Option<&String>,
) -> Result<String, S3Error> {
let credentials = format!("{}/{}", access_key, scope_string(datetime, region)?);
let credentials = utf8_percent_encode(&credentials, FRAGMENT_SLASH);
let mut signed_headers = vec!["host".to_string()];
if let Some(custom_headers) = &custom_headers {
for k in custom_headers.keys() {
signed_headers.push(k.to_string())
}
}
let signed_headers = signed_headers.join(";");
let signed_headers = utf8_percent_encode(&signed_headers, FRAGMENT_SLASH);
let mut query_params = format!(
"?X-Amz-Algorithm=AWS4-HMAC-SHA256\
&X-Amz-Credential={credentials}\
&X-Amz-Date={long_date}\
&X-Amz-Expires={expires}\
&X-Amz-SignedHeaders={signed_headers}",
credentials = credentials,
long_date = datetime.format(LONG_DATETIME)?,
expires = expires,
signed_headers = signed_headers,
);
if let Some(token) = token {
write!(
query_params,
"&X-Amz-Security-Token={}",
utf8_percent_encode(token, FRAGMENT_SLASH)
)
.expect("Could not write token");
}
Ok(query_params)
}