Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dotnetv3/Cognito/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ These examples also require the following resources:


To create these resources, run the AWS CloudFormation script in the
[resources/cdk/cognito_scenario_user_pool_with_mfa](../../../resources/cdk/cognito_scenario_user_pool_with_mfa)
[resources/cdk/cognito_scenario_user_pool_with_mfa](../../resources/cdk/cognito_scenario_user_pool_with_mfa)
folder. This script outputs a user pool ID and a client ID that you can use to run
the scenario.
<!--custom.prerequisites.end-->
Expand Down
2 changes: 1 addition & 1 deletion dotnetv4/Aurora/Actions/AuroraWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public async Task<string> ModifyIntegerParametersInGroupAsync(string groupName,
{
foreach (var p in parameters)
{
if (p.IsModifiable.Value && p.DataType == "integer")
if (p.IsModifiable.GetValueOrDefault() && p.DataType == "integer")
{
while (newValue == 0)
{
Expand Down
17 changes: 17 additions & 0 deletions dotnetv4/Cognito/Actions/CognitoActions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.CognitoIdentityProvider" Version="4.0.0-preview.6" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="4.0.0-preview.6" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.1" />
</ItemGroup>

</Project>
347 changes: 347 additions & 0 deletions dotnetv4/Cognito/Actions/CognitoWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,347 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// snippet-start:[Cognito.dotnetv4.CognitoWrapper]
using System.Net;

namespace CognitoActions;

/// <summary>
/// Methods to perform Amazon Cognito Identity Provider actions.
/// </summary>
public class CognitoWrapper
{
private readonly IAmazonCognitoIdentityProvider _cognitoService;

/// <summary>
/// Constructor for the wrapper class containing Amazon Cognito actions.
/// </summary>
/// <param name="cognitoService">The Amazon Cognito client object.</param>
public CognitoWrapper(IAmazonCognitoIdentityProvider cognitoService)
{
_cognitoService = cognitoService;
}

// snippet-start:[Cognito.dotnetv4.ListUserPools]
/// <summary>
/// List the Amazon Cognito user pools for an account.
/// </summary>
/// <returns>A list of UserPoolDescriptionType objects.</returns>
public async Task<List<UserPoolDescriptionType>> ListUserPoolsAsync()
{
var userPools = new List<UserPoolDescriptionType>();

var userPoolsPaginator = _cognitoService.Paginators.ListUserPools(new ListUserPoolsRequest());

await foreach (var response in userPoolsPaginator.Responses)
{
userPools.AddRange(response.UserPools);
}

return userPools;
}

// snippet-end:[Cognito.dotnetv4.ListUserPools]

// snippet-start:[Cognito.dotnetv4.ListUsers]
/// <summary>
/// Get a list of users for the Amazon Cognito user pool.
/// </summary>
/// <param name="userPoolId">The user pool ID.</param>
/// <returns>A list of users.</returns>
public async Task<List<UserType>> ListUsersAsync(string userPoolId)
{
var request = new ListUsersRequest
{
UserPoolId = userPoolId
};

var users = new List<UserType>();

var usersPaginator = _cognitoService.Paginators.ListUsers(request);
await foreach (var response in usersPaginator.Responses)
{
users.AddRange(response.Users);
}

return users;
}

// snippet-end:[Cognito.dotnetv4.ListUsers]

// snippet-start:[Cognito.dotnetv4.AdminRespondToAuthChallenge]
/// <summary>
/// Respond to an admin authentication challenge.
/// </summary>
/// <param name="userName">The name of the user.</param>
/// <param name="clientId">The client ID.</param>
/// <param name="mfaCode">The multi-factor authentication code.</param>
/// <param name="session">The current application session.</param>
/// <param name="clientId">The user pool ID.</param>
/// <returns>The result of the authentication response.</returns>
public async Task<AuthenticationResultType> AdminRespondToAuthChallengeAsync(
string userName,
string clientId,
string mfaCode,
string session,
string userPoolId)
{
Console.WriteLine("SOFTWARE_TOKEN_MFA challenge is generated");

var challengeResponses = new Dictionary<string, string>();
challengeResponses.Add("USERNAME", userName);
challengeResponses.Add("SOFTWARE_TOKEN_MFA_CODE", mfaCode);

var respondToAuthChallengeRequest = new AdminRespondToAuthChallengeRequest
{
ChallengeName = ChallengeNameType.SOFTWARE_TOKEN_MFA,
ClientId = clientId,
ChallengeResponses = challengeResponses,
Session = session,
UserPoolId = userPoolId,
};

var response = await _cognitoService.AdminRespondToAuthChallengeAsync(respondToAuthChallengeRequest);
Console.WriteLine($"Response to Authentication {response.AuthenticationResult.TokenType}");
return response.AuthenticationResult;
}

// snippet-end:[Cognito.dotnetv4.AdminRespondToAuthChallenge]

// snippet-start:[Cognito.dotnetv4.VerifySoftwareToken]
/// <summary>
/// Verify the TOTP and register for MFA.
/// </summary>
/// <param name="session">The name of the session.</param>
/// <param name="code">The MFA code.</param>
/// <returns>The status of the software token.</returns>
public async Task<VerifySoftwareTokenResponseType> VerifySoftwareTokenAsync(string session, string code)
{
var tokenRequest = new VerifySoftwareTokenRequest
{
UserCode = code,
Session = session,
};

var verifyResponse = await _cognitoService.VerifySoftwareTokenAsync(tokenRequest);

return verifyResponse.Status;
}

// snippet-end:[Cognito.dotnetv4.VerifySoftwareToken]

// snippet-start:[Cognito.dotnetv4.AssociateSoftwareToken]
/// <summary>
/// Get an MFA token to authenticate the user with the authenticator.
/// </summary>
/// <param name="session">The session name.</param>
/// <returns>The session name.</returns>
public async Task<string> AssociateSoftwareTokenAsync(string session)
{
var softwareTokenRequest = new AssociateSoftwareTokenRequest
{
Session = session,
};

var tokenResponse = await _cognitoService.AssociateSoftwareTokenAsync(softwareTokenRequest);
var secretCode = tokenResponse.SecretCode;

Console.WriteLine($"Use the following secret code to set up the authenticator: {secretCode}");

return tokenResponse.Session;
}

// snippet-end:[Cognito.dotnetv4.AssociateSoftwareToken]

// snippet-start:[Cognito.dotnetv4.AdminInitiateAuth]
/// <summary>
/// Initiate an admin auth request.
/// </summary>
/// <param name="clientId">The client ID to use.</param>
/// <param name="userPoolId">The ID of the user pool.</param>
/// <param name="userName">The username to authenticate.</param>
/// <param name="password">The user's password.</param>
/// <returns>The session to use in challenge-response.</returns>
public async Task<string> AdminInitiateAuthAsync(string clientId, string userPoolId, string userName, string password)
{
var authParameters = new Dictionary<string, string>();
authParameters.Add("USERNAME", userName);
authParameters.Add("PASSWORD", password);

var request = new AdminInitiateAuthRequest
{
ClientId = clientId,
UserPoolId = userPoolId,
AuthParameters = authParameters,
AuthFlow = AuthFlowType.ADMIN_USER_PASSWORD_AUTH,
};

var response = await _cognitoService.AdminInitiateAuthAsync(request);
return response.Session;
}
// snippet-end:[Cognito.dotnetv4.AdminInitiateAuth]

// snippet-start:[Cognito.dotnetv4.InitiateAuth]
/// <summary>
/// Initiate authorization.
/// </summary>
/// <param name="clientId">The client Id of the application.</param>
/// <param name="userName">The name of the user who is authenticating.</param>
/// <param name="password">The password for the user who is authenticating.</param>
/// <returns>The response from the initiate auth request.</returns>
public async Task<InitiateAuthResponse> InitiateAuthAsync(string clientId, string userName, string password)
{
var authParameters = new Dictionary<string, string>();
authParameters.Add("USERNAME", userName);
authParameters.Add("PASSWORD", password);

var authRequest = new InitiateAuthRequest

{
ClientId = clientId,
AuthParameters = authParameters,
AuthFlow = AuthFlowType.USER_PASSWORD_AUTH,
};

var response = await _cognitoService.InitiateAuthAsync(authRequest);
Console.WriteLine($"Result Challenge is : {response.ChallengeName}");

return response;
}
// snippet-end:[Cognito.dotnetv4.InitiateAuth]

// snippet-start:[Cognito.dotnetv4.ConfirmSignUp]
/// <summary>
/// Confirm that the user has signed up.
/// </summary>
/// <param name="clientId">The Id of this application.</param>
/// <param name="code">The confirmation code sent to the user.</param>
/// <param name="userName">The username.</param>
/// <returns>True if successful.</returns>
public async Task<bool> ConfirmSignupAsync(string clientId, string code, string userName)
{
var signUpRequest = new ConfirmSignUpRequest
{
ClientId = clientId,
ConfirmationCode = code,
Username = userName,
};

var response = await _cognitoService.ConfirmSignUpAsync(signUpRequest);
if (response.HttpStatusCode == HttpStatusCode.OK)
{
Console.WriteLine($"{userName} was confirmed");
return true;
}
return false;
}

// snippet-end:[Cognito.dotnetv4.ConfirmSignUp]

// snippet-start:[Cognito.dotnetv4.ConfirmDevice]
/// <summary>
/// Initiates and confirms tracking of the device.
/// </summary>
/// <param name="accessToken">The user's access token.</param>
/// <param name="deviceKey">The key of the device from Amazon Cognito.</param>
/// <param name="deviceName">The device name.</param>
/// <returns></returns>
public async Task<bool?> ConfirmDeviceAsync(string accessToken, string deviceKey, string deviceName)
{
var request = new ConfirmDeviceRequest
{
AccessToken = accessToken,
DeviceKey = deviceKey,
DeviceName = deviceName
};

var response = await _cognitoService.ConfirmDeviceAsync(request);
return response.UserConfirmationNecessary;
}

// snippet-end:[Cognito.dotnetv4.ConfirmDevice]

// snippet-start:[Cognito.dotnetv4.ResendConfirmationCode]
/// <summary>
/// Send a new confirmation code to a user.
/// </summary>
/// <param name="clientId">The Id of the client application.</param>
/// <param name="userName">The username of user who will receive the code.</param>
/// <returns>The delivery details.</returns>
public async Task<CodeDeliveryDetailsType> ResendConfirmationCodeAsync(string clientId, string userName)
{
var codeRequest = new ResendConfirmationCodeRequest
{
ClientId = clientId,
Username = userName,
};

var response = await _cognitoService.ResendConfirmationCodeAsync(codeRequest);

Console.WriteLine($"Method of delivery is {response.CodeDeliveryDetails.DeliveryMedium}");

return response.CodeDeliveryDetails;
}

// snippet-end:[Cognito.dotnetv4.ResendConfirmationCode]

// snippet-start:[Cognito.dotnetv4.GetAdminUser]
/// <summary>
/// Get the specified user from an Amazon Cognito user pool with administrator access.
/// </summary>
/// <param name="userName">The name of the user.</param>
/// <param name="poolId">The Id of the Amazon Cognito user pool.</param>
/// <returns>Async task.</returns>
public async Task<UserStatusType> GetAdminUserAsync(string userName, string poolId)
{
AdminGetUserRequest userRequest = new AdminGetUserRequest
{
Username = userName,
UserPoolId = poolId,
};

var response = await _cognitoService.AdminGetUserAsync(userRequest);

Console.WriteLine($"User status {response.UserStatus}");
return response.UserStatus;
}

// snippet-end:[Cognito.dotnetv4.GetAdminUser]

// snippet-start:[Cognito.dotnetv4.SignUp]
/// <summary>
/// Sign up a new user.
/// </summary>
/// <param name="clientId">The client Id of the application.</param>
/// <param name="userName">The username to use.</param>
/// <param name="password">The user's password.</param>
/// <param name="email">The email address of the user.</param>
/// <returns>A Boolean value indicating whether the user was confirmed.</returns>
public async Task<bool> SignUpAsync(string clientId, string userName, string password, string email)
{
var userAttrs = new AttributeType
{
Name = "email",
Value = email,
};

var userAttrsList = new List<AttributeType>();

userAttrsList.Add(userAttrs);

var signUpRequest = new SignUpRequest
{
UserAttributes = userAttrsList,
Username = userName,
ClientId = clientId,
Password = password
};

var response = await _cognitoService.SignUpAsync(signUpRequest);
return response.HttpStatusCode == HttpStatusCode.OK;
}

// snippet-end:[Cognito.dotnetv4.SignUp]
}

// snippet-end:[Cognito.dotnetv4.CognitoWrapper]
Loading