8#include <smithy/identity/signer/AwsSignerBase.h>
9#include <smithy/identity/identity/AwsCredentialIdentityBase.h>
10#include <aws/core/auth/AWSCredentials.h>
11#include <aws/crt/auth/Credentials.h>
13#include <aws/core/http/HttpRequest.h>
14#include <aws/core/auth/signer/AWSAuthSignerHelper.h>
15#include <aws/crt/http/HttpConnection.h>
16#include <aws/crt/http/HttpRequestResponse.h>
17#include <condition_variable>
22 static const char*
EMPTY_STRING_SHA256 =
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
43 auto signPayloadIt = properties.find(
"SignPayload");
44 bool signPayload = signPayloadIt != properties.end() ? signPayloadIt->second.get<
Aws::String>() ==
"true" :
false;
49 auto &request = *httpRequest;
52 Aws::Crt::ByteCursorFromCString(identity.
accessKeyId().c_str()),
54 Aws::Crt::ByteCursorFromCString((*identity.
sessionToken()).c_str()),
57 Aws::Crt::Auth::AwsSigningConfig awsSigningConfig;
68 std::shared_ptr<Aws::Crt::Http::HttpRequest> crtHttpRequest = request.ToCrtHttpRequest();
70 auto sigv4HttpRequestSigner = Aws::MakeShared<Aws::Crt::Auth::Sigv4HttpRequestSigner>(
v4AsymmetricLogTag);
73 bool processed =
false;
75 sigv4HttpRequestSigner->SignRequest(crtHttpRequest, awsSigningConfig,
76 [&request, &success, &errorMessage, &processed,
this](
const std::shared_ptr<Aws::Crt::Http::HttpRequest>& signedCrtHttpRequest,
int errorCode) {
77 std::unique_lock<std::mutex> lock(
m_mutex);
78 m_cv.wait(lock, [&]{
return !processed; });
79 success = (errorCode == AWS_ERROR_SUCCESS);
82 if (
m_signatureType == Aws::Crt::Auth::SignatureType::HttpRequestViaHeaders)
84 for (
size_t i = 0; i < signedCrtHttpRequest->GetHeaderCount(); i++)
86 Aws::Crt::Optional<Aws::Crt::Http::HttpHeader> httpHeader = signedCrtHttpRequest->GetHeader(i);
87 request.SetHeaderValue(
Aws::String(
reinterpret_cast<const char*
>(httpHeader->name.ptr), httpHeader->name.len),
88 Aws::String(
reinterpret_cast<const char*
>(httpHeader->value.ptr), httpHeader->value.len));
91 else if (
m_signatureType == Aws::Crt::Auth::SignatureType::HttpRequestViaQueryParams)
93 Aws::Http::URI newPath(
reinterpret_cast<const char*
>(signedCrtHttpRequest->GetPath()->ptr));
98 errorMessage =
"No action to take when signature type is neither \"HttpRequestViaHeaders\" nor \"HttpRequestViaQueryParams\"";
106 errStream <<
"Encountered internal error during signing process with AWS signature version 4 (Asymmetric):" << aws_error_str(errorCode);
107 errorMessage = errStream.str();
118 std::unique_lock<std::mutex> lock(
m_mutex);
119 m_cv.wait(lock, [&]{
return processed; });
132 std::shared_ptr<Aws::Crt::Auth::Credentials>& crtCredentials,
134 Aws::Crt::Auth::AwsSigningConfig& awsSigningConfig,
139 awsSigningConfig.SetRegion(
m_region.c_str());
140 awsSigningConfig.SetService(
m_region.c_str());
142 awsSigningConfig.SetUseDoubleUriEncode(
m_urlEscape);
143 awsSigningConfig.SetShouldNormalizeUriPath(
true);
144 awsSigningConfig.SetOmitSessionToken(
false);
146 awsSigningConfig.SetShouldSignHeaderCallback([](
const Aws::Crt::ByteCursor *name,
void *user_data) {
148 Aws::String headerKey(
reinterpret_cast<const char*
>(name->ptr), name->len);
151 if (
m_signatureType == Aws::Crt::Auth::SignatureType::HttpRequestViaHeaders)
169 AWS_LOGSTREAM_DEBUG(
v4AsymmetricLogTag,
"Note: Http payloads are not being signed. signPayloads=" << signBody
172 awsSigningConfig.SetSignedBodyValue(payloadHash.c_str());
173 awsSigningConfig.SetSignedBodyHeader(
m_includeSha256HashHeader ? Aws::Crt::Auth::SignedBodyHeaderType::XAmzContentSha256 : Aws::Crt::Auth::SignedBodyHeaderType::None);
175 else if (
m_signatureType == Aws::Crt::Auth::SignatureType::HttpRequestViaQueryParams)
188 AWS_LOGSTREAM_ERROR(
v4AsymmetricLogTag,
"The signature type should be either \"HttpRequestViaHeaders\" or \"HttpRequestViaQueryParams\"");
192 awsSigningConfig.SetCredentials(crtCredentials);
205 return "s3" == serviceName ||
"s3-object-lambda" == serviceName;
215 const Aws::Crt::Auth::SignatureType
m_signatureType{Aws::Crt::Auth::SignatureType::HttpRequestViaQueryParams};
virtual const std::shared_ptr< Aws::IOStream > & GetContentBody() const =0
const Aws::String & GetQueryString() const
static Aws::String ToLower(const char *source)
virtual Aws::String accessKeyId() const =0
virtual Aws::Crt::Optional< AwsIdentity::DateTime > expiration() const override=0
virtual Aws::String secretAccessKey() const =0
virtual Aws::Crt::Optional< Aws::String > sessionToken() const =0
std::condition_variable m_cv
const Aws::Crt::Auth::SignatureType m_signatureType
bool ServiceRequireUnsignedPayload(const Aws::String &serviceName) const
virtual ~AwsSigV4aSigner()
AwsSigV4aSigner(const Aws::String &serviceName, const Aws::String ®ion)
const Aws::Set< Aws::String > m_unsignedHeaders
long long m_expirationTimeInSeconds
bool createAwsSigningConfig(std::shared_ptr< Aws::Crt::Auth::Credentials > &crtCredentials, const Aws::Http::HttpRequest &request, Aws::Crt::Auth::AwsSigningConfig &awsSigningConfig, bool signBody) const
const bool m_includeSha256HashHeader
SigningFutureOutcome sign(std::shared_ptr< HttpRequest > httpRequest, const AwsCredentialIdentityBase &identity, SigningProperties properties) override
Aws::String m_serviceName
Aws::Client::AWSError< Aws::Client::CoreErrors > SigningError
Aws::Utils::FutureOutcome< std::shared_ptr< HttpRequest >, SigningError > SigningFutureOutcome
Aws::UnorderedMap< Aws::String, Aws::Crt::Variant< Aws::String, bool > > SigningProperties
virtual Aws::Utils::DateTime GetSigningTimestamp() const
AWS_CORE_API const char * ToString(Scheme scheme)
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
std::set< T, std::less< T >, Aws::Allocator< T > > Set
std::basic_ostringstream< char, std::char_traits< char >, Aws::Allocator< char > > OStringStream
static const char v4AsymmetricLogTag[]
static const char * UNSIGNED_PAYLOAD
static const char * EMPTY_STRING_SHA256
static const char * X_AMZN_TRACE_ID
static const char * USER_AGENT