Skip to content

Commit 3c7ff0e

Browse files
authored
Add C++ Cognito MVP (Issue #4098) (PR #4278)
1 parent 8a825e1 commit 3c7ff0e

11 files changed

+1453
-0
lines changed

.doc_gen/metadata/cognito-identity-provider_metadata.yaml

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ cognito-identity-provider_SignUp:
6767
- description:
6868
snippet_tags:
6969
- javascript.v3.cognito-idp.actions.SignUp
70+
C++:
71+
versions:
72+
- sdk_version: 1
73+
github: cpp/example_code/cognito
74+
excerpts:
75+
- description:
76+
snippet_tags:
77+
- cpp.example_code.cognito.client_configuration
78+
- cpp.example_code.cognito.cognito_client
79+
- cpp.example_code.cognito.signup
7080
services:
7181
cognito-identity-provider: {SignUp}
7282
cognito-identity-provider_AdminGetUser:
@@ -121,6 +131,16 @@ cognito-identity-provider_AdminGetUser:
121131
- description:
122132
snippet_tags:
123133
- javascript.v3.cognito-idp.actions.AdminGetUser
134+
C++:
135+
versions:
136+
- sdk_version: 1
137+
github: cpp/example_code/cognito
138+
excerpts:
139+
- description:
140+
snippet_tags:
141+
- cpp.example_code.cognito.client_configuration
142+
- cpp.example_code.cognito.cognito_client
143+
- cpp.example_code.cognito.admin_get_user
124144
services:
125145
cognito-identity-provider: {AdminGetUser}
126146
cognito-identity-provider_ResendConfirmationCode:
@@ -175,6 +195,16 @@ cognito-identity-provider_ResendConfirmationCode:
175195
- description:
176196
snippet_tags:
177197
- javascript.v3.cognito-idp.actions.ResendConfirmationCode
198+
C++:
199+
versions:
200+
- sdk_version: 1
201+
github: cpp/example_code/cognito
202+
excerpts:
203+
- description:
204+
snippet_tags:
205+
- cpp.example_code.cognito.client_configuration
206+
- cpp.example_code.cognito.cognito_client
207+
- cpp.example_code.cognito.resend_confirmation
178208
services:
179209
cognito-identity-provider: {ResendConfirmationCode}
180210
cognito-identity-provider_ConfirmSignUp:
@@ -229,6 +259,16 @@ cognito-identity-provider_ConfirmSignUp:
229259
- description:
230260
snippet_tags:
231261
- javascript.v3.cognito-idp.actions.ConfirmSignUp
262+
C++:
263+
versions:
264+
- sdk_version: 1
265+
github: cpp/example_code/cognito
266+
excerpts:
267+
- description:
268+
snippet_tags:
269+
- cpp.example_code.cognito.client_configuration
270+
- cpp.example_code.cognito.cognito_client
271+
- cpp.example_code.cognito.confirm_signup
232272
services:
233273
cognito-identity-provider: {ConfirmSignUp}
234274
cognito-identity-provider_ListUsers:
@@ -319,6 +359,16 @@ cognito-identity-provider_AdminInitiateAuth:
319359
- description:
320360
snippet_tags:
321361
- javascript.v3.cognito-idp.actions.AdminInitiateAuth
362+
C++:
363+
versions:
364+
- sdk_version: 1
365+
github: cpp/example_code/cognito
366+
excerpts:
367+
- description:
368+
snippet_tags:
369+
- cpp.example_code.cognito.client_configuration
370+
- cpp.example_code.cognito.cognito_client
371+
- cpp.example_code.cognito.admin_initiate_auth
322372
services:
323373
cognito-identity-provider: {AdminInitiateAuth}
324374
cognito-identity-provider_AssociateSoftwareToken:
@@ -373,6 +423,16 @@ cognito-identity-provider_AssociateSoftwareToken:
373423
- description:
374424
snippet_tags:
375425
- javascript.v3.cognito-idp.actions.AssociateSoftwareToken
426+
C++:
427+
versions:
428+
- sdk_version: 1
429+
github: cpp/example_code/cognito
430+
excerpts:
431+
- description:
432+
snippet_tags:
433+
- cpp.example_code.cognito.client_configuration
434+
- cpp.example_code.cognito.cognito_client
435+
- cpp.example_code.cognito.associate_software_token
376436
services:
377437
cognito-identity-provider: {AssociateSoftwareToken}
378438
cognito-identity-provider_VerifySoftwareToken:
@@ -427,6 +487,16 @@ cognito-identity-provider_VerifySoftwareToken:
427487
- description:
428488
snippet_tags:
429489
- javascript.v3.cognito-idp.actions.VerifySoftwareToken
490+
C++:
491+
versions:
492+
- sdk_version: 1
493+
github: cpp/example_code/cognito
494+
excerpts:
495+
- description:
496+
snippet_tags:
497+
- cpp.example_code.cognito.client_configuration
498+
- cpp.example_code.cognito.cognito_client
499+
- cpp.example_code.cognito.verify_software_token
430500
services:
431501
cognito-identity-provider: {VerifySoftwareToken}
432502
cognito-identity-provider_AdminRespondToAuthChallenge:
@@ -482,6 +552,16 @@ cognito-identity-provider_AdminRespondToAuthChallenge:
482552
- description:
483553
snippet_tags:
484554
- javascript.v3.cognito-idp.actions.AdminRespondToAuthChallenge
555+
C++:
556+
versions:
557+
- sdk_version: 1
558+
github: cpp/example_code/cognito
559+
excerpts:
560+
- description:
561+
snippet_tags:
562+
- cpp.example_code.cognito.client_configuration
563+
- cpp.example_code.cognito.cognito_client
564+
- cpp.example_code.cognito.admin_respond_to_auth_challenge
485565
services:
486566
cognito-identity-provider: {AdminRespondToAuthChallenge}
487567
cognito-identity-provider_ConfirmDevice:
@@ -587,6 +667,24 @@ cognito-identity-provider_RespondToAuthChallenge:
587667
- javascript.v3.cognito-idp.actions.RespondToAuthChallenge
588668
services:
589669
cognito-identity-provider: {RespondToAuthChallenge}
670+
cognito-identity-provider_DeleteUser:
671+
title: Delete an &COG; user using an &AWS; SDK
672+
title_abbrev: Delete a user
673+
synopsis: delete an &COG; user.
674+
category:
675+
languages:
676+
C++:
677+
versions:
678+
- sdk_version: 1
679+
github: cpp/example_code/cognito
680+
excerpts:
681+
- description:
682+
snippet_tags:
683+
- cpp.example_code.cognito.client_configuration
684+
- cpp.example_code.cognito.cognito_client
685+
- cpp.example_code.cognito.delete_user
686+
services:
687+
cognito-identity-provider: {DeleteUser}
590688
cognito-identity-provider_Scenario_SignUpUserWithMfa:
591689
title: Sign up a user with an &COG; user pool that requires MFA using an &AWS; SDK
592690
title_abbrev: Sign up a user with a user pool that requires MFA
@@ -656,6 +754,15 @@ cognito-identity-provider_Scenario_SignUpUserWithMfa:
656754
- javascript.v3.cognito-idp.actions.RespondToAuthChallenge
657755
- javascript.v3.cognito-idp.scenarios.basic.VerifySoftwareTokenHandler
658756
- javascript.v3.cognito-idp.actions.VerifySoftwareToken
757+
C++:
758+
versions:
759+
- sdk_version: 1
760+
github: cpp/example_code/cognito
761+
excerpts:
762+
- description:
763+
snippet_tags:
764+
- cpp.example_code.cognito.client_configuration
765+
- cpp.example_code.getting_started_with_user_pools
659766
services:
660767
cognito-identity-provider: {ConfirmSignUp, ConfirmDevice, AssociateSoftwareToken,
661768
AdminGetUser, ListUsers, ResendConfirmationCode,
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Set the minimum required version of CMake for this project.
5+
cmake_minimum_required(VERSION 3.8)
6+
7+
set(SERVICE_NAME cognito)
8+
set(SERVICE_COMPONENTS cognito-idp)
9+
10+
# Set this project's name.
11+
project("${SERVICE_NAME}-examples")
12+
13+
set(USING_CONAN FALSE) # Setting to true enables generation of a QR code.
14+
15+
16+
if (USING_CONAN)
17+
set(ADDED_CONAN_DEFINE USING_QR=1)
18+
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
19+
conan_basic_setup()
20+
else ()
21+
22+
endif ()
23+
24+
#Set the location of where Windows can find the installed libraries of the SDK.
25+
if (MSVC)
26+
string(REPLACE ";" "/aws-cpp-sdk-all;" SYSTEM_MODULE_PATH "${CMAKE_SYSTEM_PREFIX_PATH}/aws-cpp-sdk-all")
27+
list(APPEND CMAKE_PREFIX_PATH ${SYSTEM_MODULE_PATH})
28+
endif ()
29+
30+
# Set the C++ standard to use to build this target.
31+
set(CMAKE_CXX_STANDARD 11)
32+
33+
# Enable CTest for testing these code examples.
34+
if (BUILD_TESTS)
35+
include(CTest)
36+
endif ()
37+
38+
# Build shared libraries by default.
39+
if (NOT DEFINED BUILD_SHARED_LIBS)
40+
set(BUILD_SHARED_LIBS ON)
41+
endif ()
42+
43+
# Find the AWS SDK for C++ package.
44+
find_package(AWSSDK REQUIRED COMPONENTS ${SERVICE_COMPONENTS})
45+
46+
# If the compiler is some version of Microsoft Visual C++, or another compiler simulating C++,
47+
# and building as shared libraries, then dynamically link to those shared libraries.
48+
if (MSVC AND BUILD_SHARED_LIBS)
49+
add_definitions(-DUSE_IMPORT_EXPORT)
50+
# Copy relevant AWS SDK for C++ libraries into the current binary directory for running and debugging.
51+
52+
set(CMAKE_BUILD_TYPE Debug) # Explicitly setting CMAKE_BUILD_TYPE is necessary in Windows to copy DLLs.
53+
54+
list(APPEND SERVICE_LIST ${SERVICE_COMPONENTS})
55+
56+
AWSSDK_CPY_DYN_LIBS(SERVICE_LIST "" ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE})
57+
endif ()
58+
59+
# AWSDOC_SOURCE can be defined in the command line to limit the files in a build. For example,
60+
# you can limit files to one action.
61+
if (NOT DEFINED AWSDOC_SOURCE)
62+
file(GLOB AWSDOC_SOURCE
63+
"*.cpp"
64+
)
65+
endif ()
66+
67+
# Check whether the target system is Windows, including Win64.
68+
if (WIN32)
69+
# Check whether the compiler is some version of Microsoft Visual C++, or another compiler simulating C++.
70+
if (MSVC)
71+
source_group("Source Files" FILES ${AWSDOC_SOURCE})
72+
endif (MSVC)
73+
endif ()
74+
75+
foreach (file ${AWSDOC_SOURCE})
76+
get_filename_component(EXAMPLE ${file} NAME_WE)
77+
78+
# Build the code example executables.
79+
set(EXAMPLE_EXE run_${EXAMPLE})
80+
81+
add_executable(${EXAMPLE_EXE} ${file})
82+
83+
target_link_libraries(${EXAMPLE_EXE} ${AWSSDK_LINK_LIBRARIES}
84+
${AWSSDK_PLATFORM_DEPS}
85+
${CONAN_LIBS})
86+
87+
target_compile_definitions(${EXAMPLE_EXE}
88+
PRIVATE
89+
${ADDED_CONAN_DEFINE}
90+
SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
91+
)
92+
endforeach ()
93+
94+
# Conan and googletest are not getting along in this instance.
95+
if (BUILD_TESTS AND NOT USING_CONAN)
96+
add_subdirectory(tests)
97+
endif ()
98+

cpp/example_code/cognito/README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Amazon Cognito code examples for the SDK for C++
2+
3+
## Overview
4+
5+
Shows how to use the AWS SDK for C++ with Amazon Cognito to sign up users, set users up for multi-factor authentication (MFA), and sign in to
6+
get access tokens.
7+
8+
*Amazon Cognito provides authentication, authorization, and user management for your web and mobile apps. Your users can sign in directly with a user name and password, or through a third party such as Facebook, Amazon, Google or Apple.*
9+
10+
## ⚠️ Important
11+
12+
* Running this code might result in charges to your AWS account.
13+
* Running the tests might result in charges to your AWS account.
14+
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
15+
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
16+
17+
## Code examples
18+
19+
### Single actions
20+
21+
Code excerpts that show you how to call individual service functions.
22+
23+
* [Confirm a user](./getting_started_with_user_pools.cpp) (ConfirmSignUp)
24+
* [Delete a user](./getting_started_with_user_pools.cpp) (DeleteUser)
25+
* [Get a token to associate an MFA application with a user](./getting_started_with_user_pools.cpp) (AssociateSoftwareToken)
26+
* [Get information about a user](./getting_started_with_user_pools.cpp) (AdminGetUser)
27+
* [Resend a confirmation code](./getting_started_with_user_pools.cpp) (ResendConfirmationCode)
28+
* [Respond to SRP authentication challenges](./getting_started_with_user_pools.cpp) (RespondToAuthChallenge)
29+
* [Sign up a user](./getting_started_with_user_pools.cpp) (SignUp)
30+
* [Start authentication with a tracked device](./getting_started_with_user_pools.cpp) (InitiateAuth)
31+
* [Verify an MFA application with a user](./getting_started_with_user_pools.cpp) (VerifySoftwareToken)
32+
33+
### Scenarios
34+
35+
Code examples that show you how to accomplish a specific task by calling multiple functions within the same service.
36+
37+
* [Sign up a user with a user pool that requires MFA](./getting_started_with_user_pools.cpp) (ConfirmSignUp, DeleteUser,
38+
AssociateSoftwareToken, AdminGetUser, ResendConfirmationCode, RespondToAuthChallenge, SignUp, InitiateAuth, VerifySoftwareToken)
39+
40+
## Run the examples
41+
42+
### Prerequisites
43+
Before using the code examples, first complete the installation and setup steps
44+
for [Getting started](https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html) in the AWS SDK for
45+
C++ Developer Guide.
46+
This section covers how to get and build the SDK, and how to build your own code by using the SDK with a
47+
sample Hello World-style application.
48+
49+
Next, for information on code example structures and how to build and run the examples, see [Getting started with the AWS SDK for C++ code examples](https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html).
50+
51+
#### Running the `Sign up a user with a user pool that requires MFA` scenario.
52+
53+
The Amazon Cognito resources for `Sign up a user with a user pool that requires MFA` can be created by running the AWS CloudFormation
54+
`setup.yaml` script in [resources/cdk/cognito_scenario_user_pool_with_mfa](../../../resources/cdk/cognito_scenario_user_pool_with_mfa/setup.yaml).
55+
56+
For instructions on how to run the script, see the [README](../../../resources/cdk/cognito_scenario_user_pool_with_mfa/README.md).
57+
58+
Optionally, build [Sign up a user with a user pool that requires MFA](./getting_started_with_user_pools.cpp) to use the
59+
[qr-code-generator](https://github.com/nayuki/QR-Code-generator) library. This creates a QR code image for MFA authentication.
60+
A [conanfile.txt](conanfile.txt) is included with the sample code. You can install the qr-code-generator library by using the [Conan C++ package manager](https://conan.io/).
61+
62+
To install the `qr-code-generator` library using Conan, run the following command from the build directory.
63+
64+
On Linux and Mac.
65+
66+
`conan install <path_to_source_dir> --build=missing`
67+
68+
On Windows.
69+
70+
`conan install <path_to_source_dir> --build=missing -s build_type=Debug`
71+
72+
Next, enable the `USING_CONAN` variable in [CMakeLists.txt](CMakeLists.txt).
73+
74+
`set(USING_CONAN TRUE) # Setting to true enables generation of a QR code.`
75+
76+
## Tests
77+
⚠️ Running the tests might result in charges to your AWS account.
78+
79+
```sh
80+
cd <BUILD_DIR>
81+
cmake <path-to-root-of-this-source-code> -DBUILD_TESTS=ON
82+
make
83+
ctest
84+
```
85+
86+
## Additional resources
87+
88+
* [Amazon Cognito Developer Guide](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools.html)
89+
* [Amazon Cognito API Reference](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/Welcome.html)
90+
* [AWS SDK for C++ Documentation](https://docs.aws.amazon.com/sdk-for-cpp/index.html)
91+
92+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#pragma once
7+
8+
#ifndef COGNITO_EXAMPLES_COGNITO_SAMPLES_H
9+
#define COGNITO_EXAMPLES_COGNITO_SAMPLES_H
10+
11+
namespace AwsDoc {
12+
namespace Cognito {
13+
//! Scenario that adds a user to an Amazon Cognito user pool.
14+
/*!
15+
\sa gettingStartedWithUserPools()
16+
\param clientID: Client ID associated with an Amazon Cognito user pool.
17+
\param userPoolID: An Amazon Cognito user pool ID.
18+
\param clientConfig: Aws client configuration.
19+
\return bool: Successful completion.
20+
*/
21+
bool gettingStartedWithUserPools(const Aws::String &clientID,
22+
const Aws::String &userPoolID,
23+
const Aws::Client::ClientConfiguration &clientConfig);
24+
} // namespace Cognito
25+
} // namespace AwsDoc
26+
27+
28+
#endif //COGNITO_EXAMPLES_COGNITO_SAMPLES_H

0 commit comments

Comments
 (0)