aws_sigv4/
lib.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6/* Automatically managed default lints */
7#![cfg_attr(docsrs, feature(doc_auto_cfg))]
8/* End of automatically managed default lints */
9//! Provides functions for calculating Sigv4 signing keys, signatures, and
10//! optional utilities for signing HTTP requests and Event Stream messages.
11
12#![allow(clippy::derive_partial_eq_without_eq)]
13#![warn(
14    missing_docs,
15    rustdoc::missing_crate_level_docs,
16    missing_debug_implementations,
17    rust_2018_idioms,
18    unreachable_pub
19)]
20
21use std::fmt;
22
23pub mod sign;
24
25mod date_time;
26
27#[cfg(feature = "sign-eventstream")]
28pub mod event_stream;
29
30#[cfg(feature = "sign-http")]
31pub mod http_request;
32
33/// The version of the signing algorithm to use
34#[derive(Debug, Eq, PartialEq, Copy, Clone)]
35#[non_exhaustive]
36pub enum SignatureVersion {
37    /// The SigV4 signing algorithm.
38    V4,
39    /// The SigV4a signing algorithm.
40    V4a,
41}
42
43impl fmt::Display for SignatureVersion {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            SignatureVersion::V4 => write!(f, "SigV4"),
47            SignatureVersion::V4a => write!(f, "SigV4a"),
48        }
49    }
50}
51
52/// Container for the signed output and the signature.
53///
54/// This is returned by signing functions, and the signed output will be
55/// different based on what is being signed (for example, an event stream
56/// message, or an HTTP request).
57#[derive(Debug)]
58pub struct SigningOutput<T> {
59    output: T,
60    signature: String,
61}
62
63impl<T> SigningOutput<T> {
64    /// Creates a new [`SigningOutput`]
65    pub fn new(output: T, signature: String) -> Self {
66        Self { output, signature }
67    }
68
69    /// Returns the signed output
70    pub fn output(&self) -> &T {
71        &self.output
72    }
73
74    /// Returns the signature as a lowercase hex string
75    pub fn signature(&self) -> &str {
76        &self.signature
77    }
78
79    /// Decomposes the `SigningOutput` into a tuple of the signed output and the signature
80    pub fn into_parts(self) -> (T, String) {
81        (self.output, self.signature)
82    }
83}