Skip to content

Commit 393433e

Browse files
authored
Auth API update (aws#115)
* Update auth implementation against updated signing interface
1 parent c524d3a commit 393433e

File tree

13 files changed

+309
-96
lines changed

13 files changed

+309
-96
lines changed

aws-common-runtime/aws-c-auth

Submodule aws-c-auth updated 43 files

include/aws/crt/auth/Credentials.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ namespace Aws
4040
class AWS_CRT_CPP_API Credentials
4141
{
4242
public:
43-
Credentials(aws_credentials *credentials, Allocator *allocator = g_allocator) noexcept;
43+
Credentials(aws_credentials *credentials) noexcept;
4444
Credentials(
4545
ByteCursor access_key_id,
4646
ByteCursor secret_access_key,
4747
ByteCursor session_token,
48+
uint64_t expiration_timepoint_in_seconds,
4849
Allocator *allocator = g_allocator) noexcept;
4950

5051
~Credentials();
@@ -69,6 +70,11 @@ namespace Aws
6970
*/
7071
ByteCursor GetSessionToken() const noexcept;
7172

73+
/**
74+
* Gets the expiration timestamp for the credentials, or UINT64_MAX if no expiration
75+
*/
76+
uint64_t GetExpirationTimepointInSeconds() const noexcept;
77+
7278
/**
7379
* Validity check - returns true if the instance is valid, false otherwise
7480
*/
@@ -87,7 +93,7 @@ namespace Aws
8793
* Callback invoked by credentials providers when resolution succeeds (credentials will be non-null)
8894
* or fails (credentials will be null)
8995
*/
90-
using OnCredentialsResolved = std::function<void(std::shared_ptr<Credentials>)>;
96+
using OnCredentialsResolved = std::function<void(std::shared_ptr<Credentials>, int errorCode)>;
9197

9298
/**
9399
* Base interface for all credentials providers. Credentials providers are objects that
@@ -325,7 +331,7 @@ namespace Aws
325331
Allocator *allocator = g_allocator);
326332

327333
private:
328-
static void s_onCredentialsResolved(aws_credentials *credentials, void *user_data);
334+
static void s_onCredentialsResolved(aws_credentials *credentials, int error_code, void *user_data);
329335

330336
Allocator *m_allocator;
331337
aws_credentials_provider *m_provider;

include/aws/crt/auth/Sigv4Signing.h

Lines changed: 87 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <aws/crt/Types.h>
2121
#include <aws/crt/auth/Signing.h>
2222

23-
struct aws_signer;
2423
struct aws_signing_config_aws;
2524

2625
namespace Aws
@@ -34,20 +33,33 @@ namespace Aws
3433

3534
enum class SigningAlgorithm
3635
{
37-
SigV4Header = AWS_SIGNING_ALGORITHM_SIG_V4_HEADER,
38-
SigV4QueryParam = AWS_SIGNING_ALGORITHM_SIG_V4_QUERY_PARAM,
36+
SigV4 = AWS_SIGNING_ALGORITHM_V4,
37+
};
3938

40-
Count = AWS_SIGNING_ALGORITHM_COUNT
39+
enum class SignatureType
40+
{
41+
HttpRequestViaHeaders = AWS_ST_HTTP_REQUEST_HEADERS,
42+
HttpRequestViaQueryParams = AWS_ST_HTTP_REQUEST_QUERY_PARAMS,
43+
HttpRequestChunk = AWS_ST_HTTP_REQUEST_CHUNK,
44+
HttpRequestEvent = AWS_ST_HTTP_REQUEST_EVENT,
45+
};
46+
47+
enum class SignedBodyValueType
48+
{
49+
Empty = AWS_SBVT_EMPTY,
50+
Payload = AWS_SBVT_PAYLOAD,
51+
UnsignedPayload = AWS_SBVT_UNSIGNED_PAYLOAD,
52+
StreamingAws4HmacSha256Payload = AWS_SBVT_STREAMING_AWS4_HMAC_SHA256_PAYLOAD,
53+
StreamingAws4HmacSha256Events = AWS_SBVT_STREAMING_AWS4_HMAC_SHA256_EVENTS,
4154
};
4255

43-
enum class BodySigningType
56+
enum class SignedBodyHeaderType
4457
{
45-
NoSigning = AWS_BODY_SIGNING_OFF,
46-
SignBody = AWS_BODY_SIGNING_ON,
47-
UnsignedPayload = AWS_BODY_SIGNING_UNSIGNED_PAYLOAD
58+
None = AWS_SBHT_NONE,
59+
XAmzContentSha256 = AWS_SBHT_X_AMZ_CONTENT_SHA256,
4860
};
4961

50-
using ShouldSignParameterCb = bool (*)(const Crt::ByteCursor *, void *);
62+
using ShouldSignHeaderCb = bool (*)(const Crt::ByteCursor *, void *);
5163

5264
/**
5365
* Wrapper around the configuration structure specific to the AWS
@@ -71,6 +83,16 @@ namespace Aws
7183
*/
7284
void SetSigningAlgorithm(SigningAlgorithm algorithm) noexcept;
7385

86+
/**
87+
* Gets the type of signature we want to calculate
88+
*/
89+
SignatureType GetSignatureType() const noexcept;
90+
91+
/**
92+
* Sets the type of signature we want to calculate
93+
*/
94+
void SetSignatureType(SignatureType signatureType) noexcept;
95+
7496
/**
7597
* Gets the AWS region to sign against
7698
*/
@@ -129,45 +151,91 @@ namespace Aws
129151
*/
130152
void SetShouldNormalizeUriPath(bool shouldNormalizeUriPath) noexcept;
131153

154+
/**
155+
* Gets whether or not to omit the session token during signing. Only set to true when performing
156+
* a websocket handshake with IoT Core.
157+
*/
158+
bool GetOmitSessionToken() const noexcept;
159+
160+
/**
161+
* Sets whether or not to omit the session token during signing. Only set to true when performing
162+
* a websocket handshake with IoT Core.
163+
*/
164+
void SetOmitSessionToken(bool omitSessionToken) noexcept;
165+
132166
/**
133167
* Gets the ShouldSignHeadersCb from the underlying config.
134168
*/
135-
ShouldSignParameterCb GetShouldSignParameterCallback() const noexcept;
169+
ShouldSignHeaderCb GetShouldSignHeaderCallback() const noexcept;
136170

137171
/**
138172
* Sets a callback invoked during the signing process for white-listing headers that can be signed.
139173
* If you do not set this, all headers will be signed.
140174
*/
141-
void SetShouldSignHeadersCallback(ShouldSignParameterCb shouldSignParameterCb) noexcept;
175+
void SetShouldSignHeaderCallback(ShouldSignHeaderCb shouldSignHeaderCb) noexcept;
176+
177+
/**
178+
* Gets the value to use for the canonical request's payload
179+
*/
180+
SignedBodyValueType GetSignedBodyValue() const noexcept;
181+
182+
/**
183+
* Sets the value to use for the canonical request's payload
184+
*/
185+
void SetSignedBodyValue(SignedBodyValueType signedBodyValue) noexcept;
186+
187+
/**
188+
* Gets the name of the header to add that stores the signed body value
189+
*/
190+
SignedBodyHeaderType GetSignedBodyHeader() const noexcept;
142191

143192
/**
144-
* Gets whether or not the signer should add the x-amz-content-sha256 header (with appropriate value) to
145-
* the canonical request.
193+
* Sets the name of the header to add that stores the signed body value
146194
*/
147-
BodySigningType GetBodySigningType() const noexcept;
195+
void SetSignedBodyHeader(SignedBodyHeaderType signedBodyHeader) noexcept;
148196

149197
/**
150-
* Sets whether or not the signer should add the x-amz-content-sha256 header (with appropriate value) to
151-
* the canonical request.
198+
* (Query param signing only) Gets the amount of time, in seconds, the (pre)signed URI will be good for
199+
*/
200+
uint64_t GetExpirationInSeconds() const noexcept;
201+
202+
/**
203+
* (Query param signing only) Sets the amount of time, in seconds, the (pre)signed URI will be good for
204+
*/
205+
void SetExpirationInSeconds(uint64_t expirationInSeconds) noexcept;
206+
207+
/*
208+
* For Sigv4 signing, either the credentials provider or the credentials must be set.
209+
* Credentials, if set, takes precedence over the provider.
152210
*/
153-
void SetBodySigningType(BodySigningType bodysigningType) noexcept;
154211

155212
/**
156213
* Get the credentials provider to use for signing.
157214
*/
158215
const std::shared_ptr<ICredentialsProvider> &GetCredentialsProvider() const noexcept;
159216

160217
/**
161-
* Set the credentials provider to use for signing, this is mandatory for sigv4.
218+
* Set the credentials provider to use for signing.
162219
*/
163220
void SetCredentialsProvider(const std::shared_ptr<ICredentialsProvider> &credsProvider) noexcept;
164221

222+
/**
223+
* Get the credentials to use for signing.
224+
*/
225+
const std::shared_ptr<Credentials> &GetCredentials() const noexcept;
226+
227+
/**
228+
* Set the credentials to use for signing.
229+
*/
230+
void SetCredentials(const std::shared_ptr<Credentials> &credentials) noexcept;
231+
165232
/// @private
166233
const struct aws_signing_config_aws *GetUnderlyingHandle() const noexcept;
167234

168235
private:
169236
Allocator *m_allocator;
170-
std::shared_ptr<ICredentialsProvider> m_credentials;
237+
std::shared_ptr<ICredentialsProvider> m_credentialsProvider;
238+
std::shared_ptr<Credentials> m_credentials;
171239
struct aws_signing_config_aws m_config;
172240
Crt::String m_signingRegion;
173241
Crt::String m_serviceName;

source/auth/Credentials.cpp

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,40 @@ namespace Aws
2828
{
2929
namespace Auth
3030
{
31-
Credentials::Credentials(aws_credentials *credentials, Allocator *allocator) noexcept
32-
: m_credentials(aws_credentials_new_copy(allocator, credentials))
31+
Credentials::Credentials(aws_credentials *credentials) noexcept : m_credentials(credentials)
3332
{
33+
if (credentials != nullptr)
34+
{
35+
aws_credentials_acquire(credentials);
36+
}
3437
}
3538

3639
Credentials::Credentials(
3740
ByteCursor access_key_id,
3841
ByteCursor secret_access_key,
3942
ByteCursor session_token,
43+
uint64_t expiration_timepoint_in_seconds,
4044
Allocator *allocator) noexcept
41-
: m_credentials(
42-
aws_credentials_new_from_cursors(allocator, &access_key_id, &secret_access_key, &session_token))
45+
: m_credentials(aws_credentials_new(
46+
allocator,
47+
access_key_id,
48+
secret_access_key,
49+
session_token,
50+
expiration_timepoint_in_seconds))
4351
{
4452
}
4553

4654
Credentials::~Credentials()
4755
{
48-
aws_credentials_destroy(m_credentials);
56+
aws_credentials_release(m_credentials);
4957
m_credentials = nullptr;
5058
}
5159

5260
ByteCursor Credentials::GetAccessKeyId() const noexcept
5361
{
5462
if (m_credentials)
5563
{
56-
return aws_byte_cursor_from_string(m_credentials->access_key_id);
64+
return aws_credentials_get_access_key_id(m_credentials);
5765
}
5866
else
5967
{
@@ -65,7 +73,7 @@ namespace Aws
6573
{
6674
if (m_credentials)
6775
{
68-
return aws_byte_cursor_from_string(m_credentials->secret_access_key);
76+
return aws_credentials_get_secret_access_key(m_credentials);
6977
}
7078
else
7179
{
@@ -77,14 +85,26 @@ namespace Aws
7785
{
7886
if (m_credentials)
7987
{
80-
return aws_byte_cursor_from_string(m_credentials->session_token);
88+
return aws_credentials_get_session_token(m_credentials);
8189
}
8290
else
8391
{
8492
return ByteCursor{0, nullptr};
8593
}
8694
}
8795

96+
uint64_t Credentials::GetExpirationTimepointInSeconds() const noexcept
97+
{
98+
if (m_credentials)
99+
{
100+
return aws_credentials_get_expiration_timepoint_seconds(m_credentials);
101+
}
102+
else
103+
{
104+
return 0;
105+
}
106+
}
107+
88108
Credentials::operator bool() const noexcept { return m_credentials != nullptr; }
89109

90110
CredentialsProvider::CredentialsProvider(aws_credentials_provider *provider, Allocator *allocator) noexcept
@@ -109,14 +129,18 @@ namespace Aws
109129
std::shared_ptr<const CredentialsProvider> m_provider;
110130
};
111131

112-
void CredentialsProvider::s_onCredentialsResolved(aws_credentials *credentials, void *user_data)
132+
void CredentialsProvider::s_onCredentialsResolved(
133+
aws_credentials *credentials,
134+
int error_code,
135+
void *user_data)
113136
{
114137
CredentialsProviderCallbackArgs *callbackArgs =
115138
static_cast<CredentialsProviderCallbackArgs *>(user_data);
116139

117-
auto credentialsPtr = std::make_shared<Credentials>(credentials, callbackArgs->m_provider->m_allocator);
140+
auto credentialsPtr =
141+
Aws::Crt::MakeShared<Credentials>(callbackArgs->m_provider->m_allocator, credentials);
118142

119-
callbackArgs->m_onCredentialsResolved(credentialsPtr);
143+
callbackArgs->m_onCredentialsResolved(credentialsPtr, error_code);
120144

121145
Aws::Crt::Delete(callbackArgs, callbackArgs->m_provider->m_allocator);
122146
}

0 commit comments

Comments
 (0)