|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <gtest/gtest.h> |
| 7 | +#include <aws/testing/AwsTestHelpers.h> |
| 8 | +#include <aws/testing/MemoryTesting.h> |
| 9 | +#include <algorithm> |
| 10 | +#include <thread> |
| 11 | + |
| 12 | +#include <aws/cognito-idp/CognitoIdentityProviderClient.h> |
| 13 | +#include <aws/cognito-idp/CognitoIdentityProviderErrors.h> |
| 14 | +#include <aws/cognito-idp/model/AuthFlowType.h> |
| 15 | +#include <aws/cognito-idp/model/ExplicitAuthFlowsType.h> |
| 16 | +#include <aws/cognito-idp/model/InitiateAuthRequest.h> |
| 17 | +#include <aws/cognito-idp/model/CreateUserPoolClientRequest.h> |
| 18 | +#include <aws/cognito-idp/model/SignUpRequest.h> |
| 19 | +#include <aws/cognito-idp/model/DeleteUserPoolClientRequest.h> |
| 20 | + |
| 21 | +#include <aws/core/client/CoreErrors.h> |
| 22 | +#include <aws/core/utils/json/JsonSerializer.h> |
| 23 | +#include <aws/core/utils/Outcome.h> |
| 24 | +#include <aws/testing/TestingEnvironment.h> |
| 25 | +#include <aws/core/platform/Environment.h> |
| 26 | +#include <openssl/srp.h> |
| 27 | +using namespace Aws::CognitoIdentityProvider; |
| 28 | +using namespace Aws::CognitoIdentityProvider::Model; |
| 29 | +using namespace Aws::Client; |
| 30 | +using namespace Aws::Region; |
| 31 | + |
| 32 | +#define TEST_POOL_PREFIX "IntegrationTest_" |
| 33 | + |
| 34 | +namespace |
| 35 | +{ |
| 36 | +static const char* ALLOCATION_TAG = "IdentityProviderOperationTest"; |
| 37 | + |
| 38 | + |
| 39 | +class IdentityProviderOperationTest : public ::testing::Test |
| 40 | +{ |
| 41 | +public: |
| 42 | + IdentityProviderOperationTest() : |
| 43 | + client(nullptr) |
| 44 | + {} |
| 45 | + |
| 46 | + std::shared_ptr<Aws::CognitoIdentityProvider::CognitoIdentityProviderClient> client; |
| 47 | + Aws::String testTrace; |
| 48 | + |
| 49 | +protected: |
| 50 | + |
| 51 | + const Aws::String m_clientName{"testClient"}; |
| 52 | + |
| 53 | + void SetUp() |
| 54 | + { |
| 55 | + Aws::Client::ClientConfiguration config; |
| 56 | + config.region = AWS_TEST_REGION; |
| 57 | + |
| 58 | + //TODO: move this over to profile config file. |
| 59 | + client = Aws::MakeShared<Aws::CognitoIdentityProvider::CognitoIdentityProviderClient>(ALLOCATION_TAG, config); |
| 60 | + } |
| 61 | + |
| 62 | + void TearDown() |
| 63 | + { |
| 64 | + client = nullptr; |
| 65 | + if (::testing::Test::HasFailure()) |
| 66 | + { |
| 67 | + std::cout << "Test traces: " << testTrace << "\n"; |
| 68 | + } |
| 69 | + testTrace.erase(); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + void registerUser(const Aws::String& username, const Aws::String& password) |
| 74 | + { |
| 75 | + // Set up the request for creating a new User Pool Client |
| 76 | + Aws::CognitoIdentityProvider::Model::SignUpRequest request; |
| 77 | + request.SetClientId(m_clientName); |
| 78 | + request.SetUsername(username); |
| 79 | + request.SetPassword(password); |
| 80 | + |
| 81 | + auto outcome = client->SignUp(request); |
| 82 | + ASSERT_TRUE(outcome.IsSuccess()); |
| 83 | + if (outcome.IsSuccess()) |
| 84 | + { |
| 85 | + std::cout << "User registered successfully." << std::endl; |
| 86 | + std::cout << "User confirmed: " << (outcome.GetResult().GetUserConfirmed() ? "Yes" : "No") << std::endl; |
| 87 | + std::cout << "User sub: " << outcome.GetResult().GetUserSub() << std::endl; |
| 88 | + |
| 89 | + |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + CreateUserPoolClientOutcome createPoolClient() |
| 94 | + { |
| 95 | + // Set up the request for creating a new User Pool Client |
| 96 | + Aws::CognitoIdentityProvider::Model::CreateUserPoolClientRequest request; |
| 97 | + request.SetUserPoolId("test-pool-id"); |
| 98 | + request.SetClientName("testClient"); |
| 99 | + |
| 100 | + request.SetGenerateSecret(true); // If you need a secret for your client |
| 101 | + Aws::Vector<Aws::CognitoIdentityProvider::Model::ExplicitAuthFlowsType> authFlows{Aws::CognitoIdentityProvider::Model::ExplicitAuthFlowsType::ALLOW_USER_SRP_AUTH}; |
| 102 | + request.SetExplicitAuthFlows(authFlows); |
| 103 | + |
| 104 | + // Make the call to create the user pool client |
| 105 | + auto outcome = client->CreateUserPoolClient(request); |
| 106 | + ASSERT_TRUE(outcome.IsSuccess()); |
| 107 | + if (outcome.IsSuccess()) |
| 108 | + { |
| 109 | + std::cout << "User Pool Client created successfully." << std::endl; |
| 110 | + std::cout << "Client ID: " << outcome.GetResult().GetUserPoolClient().GetClientId() << std::endl; |
| 111 | + if (request.GetGenerateSecret()) |
| 112 | + { |
| 113 | + std::cout << "Client Secret: " << outcome.GetResult().GetUserPoolClient().GetClientSecret() << std::endl; |
| 114 | + } |
| 115 | + } |
| 116 | + return outcome; |
| 117 | + } |
| 118 | + |
| 119 | + void deletePoolClient() |
| 120 | + { |
| 121 | + // Set up the request for deleting the User Pool Client |
| 122 | + Aws::CognitoIdentityProvider::Model::DeleteUserPoolClientRequest request; |
| 123 | + request.SetUserPoolId("test-pool-id"); |
| 124 | + request.SetClientId("testClient"); |
| 125 | + |
| 126 | + // Make the call to delete the user pool client |
| 127 | + auto outcome = client->DeleteUserPoolClient(request); |
| 128 | + ASSERT_TRUE(outcome.IsSuccess()); |
| 129 | + if (outcome.IsSuccess()) |
| 130 | + { |
| 131 | + std::cout << "User Pool Client deleted successfully." << std::endl; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | +}; |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +TEST_F(IdentityProviderOperationTest, testSecret) |
| 144 | +{ |
| 145 | + auto outcome = createPoolClient(); |
| 146 | + |
| 147 | + |
| 148 | + Aws::String ComputeSecretHash(const Aws::String &userPoolClientId, const Aws::String &userPoolClientSecret, const Aws::String &userName) |
| 149 | + { |
| 150 | + const auto secret = userPoolClientSecret; |
| 151 | + const auto message = userName + userPoolClientId; |
| 152 | + |
| 153 | + unsigned char * digest = HMAC(EVP_sha256(), |
| 154 | + secret.c_str(), static_cast<int>(secret.length()), |
| 155 | + reinterpret_cast<const unsigned char*>(message.c_str()), |
| 156 | + static_cast<int>(message.length()), |
| 157 | + NULL, NULL); |
| 158 | + |
| 159 | + char mdString[SHA256_DIGEST_LENGTH * 2 + 1]; |
| 160 | + for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) |
| 161 | + sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]); |
| 162 | + |
| 163 | + mdString[64] = '\0'; // null-terminate the string |
| 164 | + |
| 165 | + return Aws::String(mdString); |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + Aws::Map<Aws::String, Aws::String> authParameters; |
| 170 | + authParameters["USERNAME"] = "dummyuser"; // Replace with actual username |
| 171 | + |
| 172 | + // Compute SECRET_HASH if the client has a secret |
| 173 | + const Aws::String clientId = outcome.GetResult().GetUserPoolClient().GetClientId(); // Client ID from previous step |
| 174 | + const Aws::String clientSecret = outcome.GetResult().GetUserPoolClient().GetClientSecret(); // Client Secret if generated |
| 175 | + authParameters["SECRET_HASH"] = ComputeSecretHash(clientId, clientSecret, "dummyuser"); |
| 176 | + |
| 177 | + // Note: SRP_A is part of the SRP protocol, you need to implement or use a library for SRP |
| 178 | + // This is a placeholder for a correct SRP implementation: |
| 179 | + authParameters["SRP_A"] = "dummy"; // Replace with actual SRP_A value |
| 180 | + |
| 181 | + Aws::CognitoIdentityProvider::Model::InitiateAuthRequest authRequest; |
| 182 | + authRequest.SetClientId(clientId); // Client ID from when you created the client |
| 183 | + authRequest.SetAuthFlow(Aws::CognitoIdentityProvider::Model::AuthFlowType::USER_SRP_AUTH); |
| 184 | + authRequest.SetAuthParameters(authParameters); |
| 185 | + |
| 186 | + Aws::CognitoIdentityProvider::Model::InitiateAuthOutcome authResult = client->InitiateAuth( authRequest ); |
| 187 | + |
| 188 | + EXPECT_TRUE(authResult.IsSuccess()); |
| 189 | + |
| 190 | + |
| 191 | + deletePoolClient(); |
| 192 | +} |
| 193 | + |
| 194 | +} |
0 commit comments