Struct Request

Source
pub struct Request {
    pub request_method: String,
    pub uri: Uri,
    pub headers: HeaderMap<HeaderValue>,
    pub body: Option<Vec<u8>>,
}
Expand description

A data structure containing the elements of the request (some client-supplied, some service-supplied) involved in the SigV4 verification process.

This is typically populated from the HTTP request headers and body by using the Request::from_http_request_parts method.

Fields§

§request_method: String

The request method (GET, PUT, POST) (client).

§uri: Uri

The URI path being accessed (client).

§headers: HeaderMap<HeaderValue>

The HTTP headers sent with the request (client).

§body: Option<Vec<u8>>

The request body (if any) (client).

Implementations§

Source§

impl Request

Source

pub fn from_http_request_parts(parts: &Parts, body: Option<Vec<u8>>) -> Self

Create a Request from an HTTP request.

§Example
let http_req = http::Request::get("https://example.com")
    .header("X-Amz-Date", "20210101T000000Z")
    .body(())
    .unwrap();
let req = Request::from_http_request_parts(&http_req.into_parts().0, None);
Source

pub fn to_get_signing_key_request<A1, A2>( &self, signing_key_kind: SigningKeyKind, region: A1, service: A2, ) -> Result<GetSigningKeyRequest, SignatureError>
where A1: AsRef<str>, A2: AsRef<str>,

Returns a GetSigningKeyRequest from this request.

This is used to request a signing key from an external source.

§Example
let gsk_req = req.to_get_signing_key_request(
    SigningKeyKind::KSigning, "us-east-1", "example").unwrap();
assert_eq!(gsk_req.signing_key_kind, SigningKeyKind::KSigning);
assert_eq!(gsk_req.access_key, "AKIAIOSFODNN7EXAMPLE");
assert_eq!(gsk_req.request_date, NaiveDate::from_ymd_opt(2021, 1, 1).unwrap());
assert_eq!(gsk_req.region, "us-east-1");
assert_eq!(gsk_req.service, "example");
Source

pub fn get_request_timestamp(&self) -> Result<DateTime<Utc>, SignatureError>

The timestamp of the request.

This returns the first value found from:

  • The X-Amz-Date query parameter.
  • The X-Amz-Date HTTP header.
  • The Date HTTP header.

The timestamp should be in ISO 8601 YYYYMMDDTHHMMSSZ format without milliseconds (must per AWS documentation). However, the AWS SigV4 test suite includes a variety of date formats, including RFC 2822, RFC 3339, and ISO 8601. This routine allows all of these formats.

This is unlikely to be useful in regular code; it is exposed for testing purposes.

§Errors

If the X-Amz-Date`` query parameter is present but is not a valid timestamp, [SignatureError::MalformedParameter`] is returned.

If the X-Amz-Date query parameter is missing and the X-Amz-Date header is present but is not a valid timestamp, SignatureError::MalformedHeader is returned.

If the X-Amz-Date query parameter and X-Amz-Date header are missing and the Date header is present but is not a valid timestamp, SignatureError::MalformedHeader is returned.

If none of the above are present, SignatureError::MissingHeader is returned.

§Example
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
use http::Request;
use scratchstack_aws_signature::Request as SigRequest;

let req = Request::get("https://example.com")
    .header("X-Amz-Date", "20210101T000000Z")
    .body(())
    .unwrap();
let req = SigRequest::from_http_request_parts(&req.into_parts().0, None);
let expected = DateTime::<Utc>::from_naive_utc_and_offset(
    NaiveDateTime::new(
        NaiveDate::from_ymd_opt(2021, 1, 1).unwrap(),
        NaiveTime::from_hms_opt(0, 0, 0).unwrap()),
    Utc
);
assert_eq!(req.get_request_timestamp().unwrap(), expected);

Trait Implementations§

Source§

impl Debug for Request

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.