AWS IoT Device SDK C++ v2: crt/aws-crt-cpp/include/aws/crt/auth/Credentials.h Source File

AWS IoT Device SDK C++ v2 1.14.0 AWS IoT Device SDK C++ v2

Credentials.h
Go to the documentation of this file.
1#pragma once
7#include <aws/crt/Exports.h>
8#include <aws/crt/Types.h>
9#include <aws/crt/http/HttpConnection.h>
10#include <aws/crt/io/TlsOptions.h>
11
12#include <chrono>
13#include <functional>
14
15struct aws_credentials;
16struct aws_credentials_provider;
17
18namespace Aws
19{
20 namespace Crt
21 {
22 namespace Io
23 {
24 class ClientBootstrap;
25 }
26
27 namespace Http
28 {
29 class HttpClientConnectionProxyOptions;
30 }
31
32 namespace Auth
33 {
37 class AWS_CRT_CPP_API Credentials
38 {
39 public:
40 Credentials(const aws_credentials *credentials) noexcept;
41 Credentials(
42 ByteCursor access_key_id,
43 ByteCursor secret_access_key,
44 ByteCursor session_token,
45 uint64_t expiration_timepoint_in_seconds,
46 Allocator *allocator = g_allocator) noexcept;
47
48 ~Credentials();
49
50 Credentials(const Credentials &) = delete;
51 Credentials(Credentials &&) = delete;
52 Credentials &operator=(const Credentials &) = delete;
53 Credentials &operator=(Credentials &&) = delete;
54
58 ByteCursor GetAccessKeyId() const noexcept;
59
63 ByteCursor GetSecretAccessKey() const noexcept;
64
68 ByteCursor GetSessionToken() const noexcept;
69
73 uint64_t GetExpirationTimepointInSeconds() const noexcept;
74
78 explicit operator bool() const noexcept;
79
83 const aws_credentials *GetUnderlyingHandle() const noexcept { return m_credentials; }
84
85 private:
86 const aws_credentials *m_credentials;
87 };
88
93 using OnCredentialsResolved = std::function<void(std::shared_ptr<Credentials>, int errorCode)>;
94
98 using GetCredentialsHandler = std::function<std::shared_ptr<Credentials>()>;
99
104 class AWS_CRT_CPP_API ICredentialsProvider : public std::enable_shared_from_this<ICredentialsProvider>
105 {
106 public:
107 virtual ~ICredentialsProvider() = default;
108
112 virtual bool GetCredentials(const OnCredentialsResolved &onCredentialsResolved) const = 0;
113
120 virtual aws_credentials_provider *GetUnderlyingHandle() const noexcept = 0;
121
125 virtual bool IsValid() const noexcept = 0;
126 };
127
131 struct AWS_CRT_CPP_API CredentialsProviderStaticConfig
132 {
133 CredentialsProviderStaticConfig()
134 {
135 AWS_ZERO_STRUCT(AccessKeyId);
136 AWS_ZERO_STRUCT(SecretAccessKey);
137 AWS_ZERO_STRUCT(SessionToken);
138 }
139
143 ByteCursor AccessKeyId;
144
148 ByteCursor SecretAccessKey;
149
153 ByteCursor SessionToken;
154 };
155
159 struct AWS_CRT_CPP_API CredentialsProviderProfileConfig
160 {
161 CredentialsProviderProfileConfig() : Bootstrap(nullptr), TlsContext(nullptr)
162 {
163 AWS_ZERO_STRUCT(ProfileNameOverride);
164 AWS_ZERO_STRUCT(ConfigFileNameOverride);
165 AWS_ZERO_STRUCT(CredentialsFileNameOverride);
166 }
167
171 ByteCursor ProfileNameOverride;
172
177 ByteCursor ConfigFileNameOverride;
178
183 ByteCursor CredentialsFileNameOverride;
184
189 Io::ClientBootstrap *Bootstrap;
190
199 Io::TlsContext *TlsContext;
200 };
201
205 struct AWS_CRT_CPP_API CredentialsProviderImdsConfig
206 {
207 CredentialsProviderImdsConfig() : Bootstrap(nullptr) {}
208
213 Io::ClientBootstrap *Bootstrap;
214 };
215
221 struct AWS_CRT_CPP_API CredentialsProviderChainConfig
222 {
223 CredentialsProviderChainConfig() : Providers() {}
224
228 Vector<std::shared_ptr<ICredentialsProvider>> Providers;
229 };
230
234 struct AWS_CRT_CPP_API CredentialsProviderCachedConfig
235 {
236 CredentialsProviderCachedConfig() : Provider(), CachedCredentialTTL() {}
237
241 std::shared_ptr<ICredentialsProvider> Provider;
242
246 std::chrono::milliseconds CachedCredentialTTL;
247 };
248
255 struct AWS_CRT_CPP_API CredentialsProviderChainDefaultConfig
256 {
257 CredentialsProviderChainDefaultConfig() : Bootstrap(nullptr), TlsContext(nullptr) {}
258
262 Io::ClientBootstrap *Bootstrap;
263
271 Io::TlsContext *TlsContext;
272 };
273
277 struct AWS_CRT_CPP_API CredentialsProviderX509Config
278 {
279 CredentialsProviderX509Config()
280 : Bootstrap(nullptr), TlsOptions(), ThingName(), RoleAlias(), Endpoint(), ProxyOptions()
281 {
282 }
283
288 Io::ClientBootstrap *Bootstrap;
289
290 /* TLS connection options that have been initialized with your x509 certificate and private key */
291 Io::TlsConnectionOptions TlsOptions;
292
293 /* IoT thing name you registered with AWS IOT for your device, it will be used in http request header */
294 String ThingName;
295
296 /* Iot role alias you created with AWS IoT for your IAM role, it will be used in http request path */
297 String RoleAlias;
298
305 String Endpoint;
306
310 Optional<Http::HttpClientConnectionProxyOptions> ProxyOptions;
311 };
312
316 struct AWS_CRT_CPP_API CredentialsProviderDelegateConfig
317 {
318 /* handler to provider credentials */
319 GetCredentialsHandler Handler;
320 };
321
328 class AWS_CRT_CPP_API CredentialsProvider : public ICredentialsProvider
329 {
330 public:
331 CredentialsProvider(aws_credentials_provider *provider, Allocator *allocator = g_allocator) noexcept;
332
333 virtual ~CredentialsProvider();
334
335 CredentialsProvider(const CredentialsProvider &) = delete;
336 CredentialsProvider(CredentialsProvider &&) = delete;
337 CredentialsProvider &operator=(const CredentialsProvider &) = delete;
338 CredentialsProvider &operator=(CredentialsProvider &&) = delete;
339
343 virtual bool GetCredentials(const OnCredentialsResolved &onCredentialsResolved) const override;
344
348 virtual aws_credentials_provider *GetUnderlyingHandle() const noexcept override { return m_provider; }
349
353 virtual bool IsValid() const noexcept override { return m_provider != nullptr; }
354
355 /*
356 * Factory methods for all of the basic credentials provider types
357 */
358
362 static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderStatic(
363 const CredentialsProviderStaticConfig &config,
364 Allocator *allocator = g_allocator);
365
369 static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderEnvironment(
370 Allocator *allocator = g_allocator);
371
375 static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderProfile(
376 const CredentialsProviderProfileConfig &config,
377 Allocator *allocator = g_allocator);
378
382 static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderImds(
383 const CredentialsProviderImdsConfig &config,
384 Allocator *allocator = g_allocator);
385
390 static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderChain(
391 const CredentialsProviderChainConfig &config,
392 Allocator *allocator = g_allocator);
393
394 /*
395 * Creates a provider that puts a simple time-based cache in front of its queries
396 * to a subordinate provider.
397 */
398 static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderCached(
399 const CredentialsProviderCachedConfig &config,
400 Allocator *allocator = g_allocator);
401
408 static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderChainDefault(
409 const CredentialsProviderChainDefaultConfig &config,
410 Allocator *allocator = g_allocator);
411
416 static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderX509(
417 const CredentialsProviderX509Config &config,
418 Allocator *allocator = g_allocator);
419
424 static std::shared_ptr<ICredentialsProvider> CreateCredentialsProviderDelegate(
425 const CredentialsProviderDelegateConfig &config,
426 Allocator *allocator = g_allocator);
427
428 private:
429 static void s_onCredentialsResolved(aws_credentials *credentials, int error_code, void *user_data);
430
431 Allocator *m_allocator;
432 aws_credentials_provider *m_provider;
433 };
434 } // namespace Auth
435 } // namespace Crt
436} // namespace Aws
HttpConnection.h
TlsOptions.h
Types.h
Aws::Crt::Auth::Credentials
Definition: Credentials.h:38
Aws::Crt::Auth::Credentials::operator=
Credentials & operator=(Credentials &&)=delete
Aws::Crt::Auth::Credentials::operator=
Credentials & operator=(const Credentials &)=delete
Aws::Crt::Auth::Credentials::Credentials
Credentials(const Credentials &)=delete
Aws::Crt::Auth::Credentials::Credentials
Credentials(Credentials &&)=delete
Aws::Crt::Auth::CredentialsProvider
Definition: Credentials.h:329
Aws::Crt::Auth::CredentialsProvider::operator=
CredentialsProvider & operator=(CredentialsProvider &&)=delete
Aws::Crt::Auth::CredentialsProvider::GetUnderlyingHandle
virtual aws_credentials_provider * GetUnderlyingHandle() const noexcept override
Definition: Credentials.h:348
Aws::Crt::Auth::CredentialsProvider::CredentialsProvider
CredentialsProvider(const CredentialsProvider &)=delete
Aws::Crt::Auth::CredentialsProvider::IsValid
virtual bool IsValid() const noexcept override
Definition: Credentials.h:353
Aws::Crt::Auth::CredentialsProvider::CredentialsProvider
CredentialsProvider(CredentialsProvider &&)=delete
Aws::Crt::Auth::CredentialsProvider::operator=
CredentialsProvider & operator=(const CredentialsProvider &)=delete
Aws::Crt::Auth::ICredentialsProvider
Definition: Credentials.h:105
Aws::Crt::Auth::ICredentialsProvider::GetCredentials
virtual bool GetCredentials(const OnCredentialsResolved &onCredentialsResolved) const =0
Aws::Crt::Auth::ICredentialsProvider::GetUnderlyingHandle
virtual aws_credentials_provider * GetUnderlyingHandle() const noexcept=0
Aws::Crt::Auth::ICredentialsProvider::~ICredentialsProvider
virtual ~ICredentialsProvider()=default
Aws::Crt::Http::HttpClientConnectionProxyOptions
Definition: HttpConnection.h:270
Aws::Crt::Io::ClientBootstrap
Definition: Bootstrap.h:35
Aws::Crt::Io::TlsConnectionOptions
Definition: TlsOptions.h:274
Aws::Crt::Io::TlsContext
Definition: TlsOptions.h:331
Aws::Crt::Optional
Definition: Optional.h:17
Exports.h
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Aws::Crt::Auth::OnCredentialsResolved
std::function< void(std::shared_ptr< Credentials >, int errorCode)> OnCredentialsResolved
Definition: Credentials.h:93
Aws::Crt::Auth::GetCredentialsHandler
std::function< std::shared_ptr< Credentials >()> GetCredentialsHandler
Definition: Credentials.h:98
Aws::Crt::ByteCursor
aws_byte_cursor ByteCursor
Definition: Types.h:33
Aws::Crt::Allocator
aws_allocator Allocator
Definition: StlAllocator.h:17
Aws::Crt::g_allocator
AWS_CRT_CPP_API Allocator * g_allocator
Definition: Api.cpp:21
Aws::Crt::String
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:47
Aws::Crt::Vector
std::vector< T, StlAllocator< T > > Vector
Definition: Types.h:55
Aws
Definition: Api.h:17
Aws::Crt::Auth::CredentialsProviderCachedConfig
Definition: Credentials.h:235
Aws::Crt::Auth::CredentialsProviderCachedConfig::Provider
std::shared_ptr< ICredentialsProvider > Provider
Definition: Credentials.h:241
Aws::Crt::Auth::CredentialsProviderCachedConfig::CredentialsProviderCachedConfig
CredentialsProviderCachedConfig()
Definition: Credentials.h:236
Aws::Crt::Auth::CredentialsProviderCachedConfig::CachedCredentialTTL
std::chrono::milliseconds CachedCredentialTTL
Definition: Credentials.h:246
Aws::Crt::Auth::CredentialsProviderChainConfig
Definition: Credentials.h:222
Aws::Crt::Auth::CredentialsProviderChainConfig::CredentialsProviderChainConfig
CredentialsProviderChainConfig()
Definition: Credentials.h:223
Aws::Crt::Auth::CredentialsProviderChainConfig::Providers
Vector< std::shared_ptr< ICredentialsProvider > > Providers
Definition: Credentials.h:228
Aws::Crt::Auth::CredentialsProviderChainDefaultConfig
Definition: Credentials.h:256
Aws::Crt::Auth::CredentialsProviderChainDefaultConfig::TlsContext
Io::TlsContext * TlsContext
Definition: Credentials.h:271
Aws::Crt::Auth::CredentialsProviderChainDefaultConfig::Bootstrap
Io::ClientBootstrap * Bootstrap
Definition: Credentials.h:262
Aws::Crt::Auth::CredentialsProviderChainDefaultConfig::CredentialsProviderChainDefaultConfig
CredentialsProviderChainDefaultConfig()
Definition: Credentials.h:257
Aws::Crt::Auth::CredentialsProviderDelegateConfig
Definition: Credentials.h:317
Aws::Crt::Auth::CredentialsProviderDelegateConfig::Handler
GetCredentialsHandler Handler
Definition: Credentials.h:319
Aws::Crt::Auth::CredentialsProviderImdsConfig
Definition: Credentials.h:206
Aws::Crt::Auth::CredentialsProviderImdsConfig::CredentialsProviderImdsConfig
CredentialsProviderImdsConfig()
Definition: Credentials.h:207
Aws::Crt::Auth::CredentialsProviderImdsConfig::Bootstrap
Io::ClientBootstrap * Bootstrap
Definition: Credentials.h:213
Aws::Crt::Auth::CredentialsProviderProfileConfig
Definition: Credentials.h:160
Aws::Crt::Auth::CredentialsProviderProfileConfig::ConfigFileNameOverride
ByteCursor ConfigFileNameOverride
Definition: Credentials.h:177
Aws::Crt::Auth::CredentialsProviderProfileConfig::CredentialsFileNameOverride
ByteCursor CredentialsFileNameOverride
Definition: Credentials.h:183
Aws::Crt::Auth::CredentialsProviderProfileConfig::ProfileNameOverride
ByteCursor ProfileNameOverride
Definition: Credentials.h:171
Aws::Crt::Auth::CredentialsProviderProfileConfig::Bootstrap
Io::ClientBootstrap * Bootstrap
Definition: Credentials.h:189
Aws::Crt::Auth::CredentialsProviderProfileConfig::CredentialsProviderProfileConfig
CredentialsProviderProfileConfig()
Definition: Credentials.h:161
Aws::Crt::Auth::CredentialsProviderProfileConfig::TlsContext
Io::TlsContext * TlsContext
Definition: Credentials.h:199
Aws::Crt::Auth::CredentialsProviderStaticConfig
Definition: Credentials.h:132
Aws::Crt::Auth::CredentialsProviderStaticConfig::SecretAccessKey
ByteCursor SecretAccessKey
Definition: Credentials.h:148
Aws::Crt::Auth::CredentialsProviderStaticConfig::AccessKeyId
ByteCursor AccessKeyId
Definition: Credentials.h:143
Aws::Crt::Auth::CredentialsProviderStaticConfig::CredentialsProviderStaticConfig
CredentialsProviderStaticConfig()
Definition: Credentials.h:133
Aws::Crt::Auth::CredentialsProviderStaticConfig::SessionToken
ByteCursor SessionToken
Definition: Credentials.h:153
Aws::Crt::Auth::CredentialsProviderX509Config
Definition: Credentials.h:278
Aws::Crt::Auth::CredentialsProviderX509Config::ProxyOptions
Optional< Http::HttpClientConnectionProxyOptions > ProxyOptions
Definition: Credentials.h:310
Aws::Crt::Auth::CredentialsProviderX509Config::CredentialsProviderX509Config
CredentialsProviderX509Config()
Definition: Credentials.h:279
Aws::Crt::Auth::CredentialsProviderX509Config::Endpoint
String Endpoint
Definition: Credentials.h:305
Aws::Crt::Auth::CredentialsProviderX509Config::ThingName
String ThingName
Definition: Credentials.h:294
Aws::Crt::Auth::CredentialsProviderX509Config::RoleAlias
String RoleAlias
Definition: Credentials.h:297
Aws::Crt::Auth::CredentialsProviderX509Config::Bootstrap
Io::ClientBootstrap * Bootstrap
Definition: Credentials.h:288
Aws::Crt::Auth::CredentialsProviderX509Config::TlsOptions
Io::TlsConnectionOptions TlsOptions
Definition: Credentials.h:291