1+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+ // SPDX-License-Identifier: Apache-2.0
3+
4+ // snippet-start:[Cognito.dotnetv4.CognitoWrapper]
5+ using System . Net ;
6+
7+ namespace CognitoActions ;
8+
9+ /// <summary>
10+ /// Methods to perform Amazon Cognito Identity Provider actions.
11+ /// </summary>
12+ public class CognitoWrapper
13+ {
14+ private readonly IAmazonCognitoIdentityProvider _cognitoService ;
15+
16+ /// <summary>
17+ /// Constructor for the wrapper class containing Amazon Cognito actions.
18+ /// </summary>
19+ /// <param name="cognitoService">The Amazon Cognito client object.</param>
20+ public CognitoWrapper ( IAmazonCognitoIdentityProvider cognitoService )
21+ {
22+ _cognitoService = cognitoService ;
23+ }
24+
25+ // snippet-start:[Cognito.dotnetv4.ListUserPools]
26+ /// <summary>
27+ /// List the Amazon Cognito user pools for an account.
28+ /// </summary>
29+ /// <returns>A list of UserPoolDescriptionType objects.</returns>
30+ public async Task < List < UserPoolDescriptionType > > ListUserPoolsAsync ( )
31+ {
32+ var userPools = new List < UserPoolDescriptionType > ( ) ;
33+
34+ var userPoolsPaginator = _cognitoService . Paginators . ListUserPools ( new ListUserPoolsRequest ( ) ) ;
35+
36+ await foreach ( var response in userPoolsPaginator . Responses )
37+ {
38+ userPools . AddRange ( response . UserPools ) ;
39+ }
40+
41+ return userPools ;
42+ }
43+
44+ // snippet-end:[Cognito.dotnetv4.ListUserPools]
45+
46+ // snippet-start:[Cognito.dotnetv4.ListUsers]
47+ /// <summary>
48+ /// Get a list of users for the Amazon Cognito user pool.
49+ /// </summary>
50+ /// <param name="userPoolId">The user pool ID.</param>
51+ /// <returns>A list of users.</returns>
52+ public async Task < List < UserType > > ListUsersAsync ( string userPoolId )
53+ {
54+ var request = new ListUsersRequest
55+ {
56+ UserPoolId = userPoolId
57+ } ;
58+
59+ var users = new List < UserType > ( ) ;
60+
61+ var usersPaginator = _cognitoService . Paginators . ListUsers ( request ) ;
62+ await foreach ( var response in usersPaginator . Responses )
63+ {
64+ users . AddRange ( response . Users ) ;
65+ }
66+
67+ return users ;
68+ }
69+
70+ // snippet-end:[Cognito.dotnetv4.ListUsers]
71+
72+ // snippet-start:[Cognito.dotnetv4.AdminRespondToAuthChallenge]
73+ /// <summary>
74+ /// Respond to an admin authentication challenge.
75+ /// </summary>
76+ /// <param name="userName">The name of the user.</param>
77+ /// <param name="clientId">The client ID.</param>
78+ /// <param name="mfaCode">The multi-factor authentication code.</param>
79+ /// <param name="session">The current application session.</param>
80+ /// <param name="clientId">The user pool ID.</param>
81+ /// <returns>The result of the authentication response.</returns>
82+ public async Task < AuthenticationResultType > AdminRespondToAuthChallengeAsync (
83+ string userName ,
84+ string clientId ,
85+ string mfaCode ,
86+ string session ,
87+ string userPoolId )
88+ {
89+ Console . WriteLine ( "SOFTWARE_TOKEN_MFA challenge is generated" ) ;
90+
91+ var challengeResponses = new Dictionary < string , string > ( ) ;
92+ challengeResponses . Add ( "USERNAME" , userName ) ;
93+ challengeResponses . Add ( "SOFTWARE_TOKEN_MFA_CODE" , mfaCode ) ;
94+
95+ var respondToAuthChallengeRequest = new AdminRespondToAuthChallengeRequest
96+ {
97+ ChallengeName = ChallengeNameType . SOFTWARE_TOKEN_MFA ,
98+ ClientId = clientId ,
99+ ChallengeResponses = challengeResponses ,
100+ Session = session ,
101+ UserPoolId = userPoolId ,
102+ } ;
103+
104+ var response = await _cognitoService . AdminRespondToAuthChallengeAsync ( respondToAuthChallengeRequest ) ;
105+ Console . WriteLine ( $ "Response to Authentication { response . AuthenticationResult . TokenType } ") ;
106+ return response . AuthenticationResult ;
107+ }
108+
109+ // snippet-end:[Cognito.dotnetv4.AdminRespondToAuthChallenge]
110+
111+ // snippet-start:[Cognito.dotnetv4.VerifySoftwareToken]
112+ /// <summary>
113+ /// Verify the TOTP and register for MFA.
114+ /// </summary>
115+ /// <param name="session">The name of the session.</param>
116+ /// <param name="code">The MFA code.</param>
117+ /// <returns>The status of the software token.</returns>
118+ public async Task < VerifySoftwareTokenResponseType > VerifySoftwareTokenAsync ( string session , string code )
119+ {
120+ var tokenRequest = new VerifySoftwareTokenRequest
121+ {
122+ UserCode = code ,
123+ Session = session ,
124+ } ;
125+
126+ var verifyResponse = await _cognitoService . VerifySoftwareTokenAsync ( tokenRequest ) ;
127+
128+ return verifyResponse . Status ;
129+ }
130+
131+ // snippet-end:[Cognito.dotnetv4.VerifySoftwareToken]
132+
133+ // snippet-start:[Cognito.dotnetv4.AssociateSoftwareToken]
134+ /// <summary>
135+ /// Get an MFA token to authenticate the user with the authenticator.
136+ /// </summary>
137+ /// <param name="session">The session name.</param>
138+ /// <returns>The session name.</returns>
139+ public async Task < string > AssociateSoftwareTokenAsync ( string session )
140+ {
141+ var softwareTokenRequest = new AssociateSoftwareTokenRequest
142+ {
143+ Session = session ,
144+ } ;
145+
146+ var tokenResponse = await _cognitoService . AssociateSoftwareTokenAsync ( softwareTokenRequest ) ;
147+ var secretCode = tokenResponse . SecretCode ;
148+
149+ Console . WriteLine ( $ "Use the following secret code to set up the authenticator: { secretCode } ") ;
150+
151+ return tokenResponse . Session ;
152+ }
153+
154+ // snippet-end:[Cognito.dotnetv4.AssociateSoftwareToken]
155+
156+ // snippet-start:[Cognito.dotnetv4.AdminInitiateAuth]
157+ /// <summary>
158+ /// Initiate an admin auth request.
159+ /// </summary>
160+ /// <param name="clientId">The client ID to use.</param>
161+ /// <param name="userPoolId">The ID of the user pool.</param>
162+ /// <param name="userName">The username to authenticate.</param>
163+ /// <param name="password">The user's password.</param>
164+ /// <returns>The session to use in challenge-response.</returns>
165+ public async Task < string > AdminInitiateAuthAsync ( string clientId , string userPoolId , string userName , string password )
166+ {
167+ var authParameters = new Dictionary < string , string > ( ) ;
168+ authParameters . Add ( "USERNAME" , userName ) ;
169+ authParameters . Add ( "PASSWORD" , password ) ;
170+
171+ var request = new AdminInitiateAuthRequest
172+ {
173+ ClientId = clientId ,
174+ UserPoolId = userPoolId ,
175+ AuthParameters = authParameters ,
176+ AuthFlow = AuthFlowType . ADMIN_USER_PASSWORD_AUTH ,
177+ } ;
178+
179+ var response = await _cognitoService . AdminInitiateAuthAsync ( request ) ;
180+ return response . Session ;
181+ }
182+ // snippet-end:[Cognito.dotnetv4.AdminInitiateAuth]
183+
184+ // snippet-start:[Cognito.dotnetv4.InitiateAuth]
185+ /// <summary>
186+ /// Initiate authorization.
187+ /// </summary>
188+ /// <param name="clientId">The client Id of the application.</param>
189+ /// <param name="userName">The name of the user who is authenticating.</param>
190+ /// <param name="password">The password for the user who is authenticating.</param>
191+ /// <returns>The response from the initiate auth request.</returns>
192+ public async Task < InitiateAuthResponse > InitiateAuthAsync ( string clientId , string userName , string password )
193+ {
194+ var authParameters = new Dictionary < string , string > ( ) ;
195+ authParameters . Add ( "USERNAME" , userName ) ;
196+ authParameters . Add ( "PASSWORD" , password ) ;
197+
198+ var authRequest = new InitiateAuthRequest
199+
200+ {
201+ ClientId = clientId ,
202+ AuthParameters = authParameters ,
203+ AuthFlow = AuthFlowType . USER_PASSWORD_AUTH ,
204+ } ;
205+
206+ var response = await _cognitoService . InitiateAuthAsync ( authRequest ) ;
207+ Console . WriteLine ( $ "Result Challenge is : { response . ChallengeName } ") ;
208+
209+ return response ;
210+ }
211+ // snippet-end:[Cognito.dotnetv4.InitiateAuth]
212+
213+ // snippet-start:[Cognito.dotnetv4.ConfirmSignUp]
214+ /// <summary>
215+ /// Confirm that the user has signed up.
216+ /// </summary>
217+ /// <param name="clientId">The Id of this application.</param>
218+ /// <param name="code">The confirmation code sent to the user.</param>
219+ /// <param name="userName">The username.</param>
220+ /// <returns>True if successful.</returns>
221+ public async Task < bool > ConfirmSignupAsync ( string clientId , string code , string userName )
222+ {
223+ var signUpRequest = new ConfirmSignUpRequest
224+ {
225+ ClientId = clientId ,
226+ ConfirmationCode = code ,
227+ Username = userName ,
228+ } ;
229+
230+ var response = await _cognitoService . ConfirmSignUpAsync ( signUpRequest ) ;
231+ if ( response . HttpStatusCode == HttpStatusCode . OK )
232+ {
233+ Console . WriteLine ( $ "{ userName } was confirmed") ;
234+ return true ;
235+ }
236+ return false ;
237+ }
238+
239+ // snippet-end:[Cognito.dotnetv4.ConfirmSignUp]
240+
241+ // snippet-start:[Cognito.dotnetv4.ConfirmDevice]
242+ /// <summary>
243+ /// Initiates and confirms tracking of the device.
244+ /// </summary>
245+ /// <param name="accessToken">The user's access token.</param>
246+ /// <param name="deviceKey">The key of the device from Amazon Cognito.</param>
247+ /// <param name="deviceName">The device name.</param>
248+ /// <returns></returns>
249+ public async Task < bool ? > ConfirmDeviceAsync ( string accessToken , string deviceKey , string deviceName )
250+ {
251+ var request = new ConfirmDeviceRequest
252+ {
253+ AccessToken = accessToken ,
254+ DeviceKey = deviceKey ,
255+ DeviceName = deviceName
256+ } ;
257+
258+ var response = await _cognitoService . ConfirmDeviceAsync ( request ) ;
259+ return response . UserConfirmationNecessary ;
260+ }
261+
262+ // snippet-end:[Cognito.dotnetv4.ConfirmDevice]
263+
264+ // snippet-start:[Cognito.dotnetv4.ResendConfirmationCode]
265+ /// <summary>
266+ /// Send a new confirmation code to a user.
267+ /// </summary>
268+ /// <param name="clientId">The Id of the client application.</param>
269+ /// <param name="userName">The username of user who will receive the code.</param>
270+ /// <returns>The delivery details.</returns>
271+ public async Task < CodeDeliveryDetailsType > ResendConfirmationCodeAsync ( string clientId , string userName )
272+ {
273+ var codeRequest = new ResendConfirmationCodeRequest
274+ {
275+ ClientId = clientId ,
276+ Username = userName ,
277+ } ;
278+
279+ var response = await _cognitoService . ResendConfirmationCodeAsync ( codeRequest ) ;
280+
281+ Console . WriteLine ( $ "Method of delivery is { response . CodeDeliveryDetails . DeliveryMedium } ") ;
282+
283+ return response . CodeDeliveryDetails ;
284+ }
285+
286+ // snippet-end:[Cognito.dotnetv4.ResendConfirmationCode]
287+
288+ // snippet-start:[Cognito.dotnetv4.GetAdminUser]
289+ /// <summary>
290+ /// Get the specified user from an Amazon Cognito user pool with administrator access.
291+ /// </summary>
292+ /// <param name="userName">The name of the user.</param>
293+ /// <param name="poolId">The Id of the Amazon Cognito user pool.</param>
294+ /// <returns>Async task.</returns>
295+ public async Task < UserStatusType > GetAdminUserAsync ( string userName , string poolId )
296+ {
297+ AdminGetUserRequest userRequest = new AdminGetUserRequest
298+ {
299+ Username = userName ,
300+ UserPoolId = poolId ,
301+ } ;
302+
303+ var response = await _cognitoService . AdminGetUserAsync ( userRequest ) ;
304+
305+ Console . WriteLine ( $ "User status { response . UserStatus } ") ;
306+ return response . UserStatus ;
307+ }
308+
309+ // snippet-end:[Cognito.dotnetv4.GetAdminUser]
310+
311+ // snippet-start:[Cognito.dotnetv4.SignUp]
312+ /// <summary>
313+ /// Sign up a new user.
314+ /// </summary>
315+ /// <param name="clientId">The client Id of the application.</param>
316+ /// <param name="userName">The username to use.</param>
317+ /// <param name="password">The user's password.</param>
318+ /// <param name="email">The email address of the user.</param>
319+ /// <returns>A Boolean value indicating whether the user was confirmed.</returns>
320+ public async Task < bool > SignUpAsync ( string clientId , string userName , string password , string email )
321+ {
322+ var userAttrs = new AttributeType
323+ {
324+ Name = "email" ,
325+ Value = email ,
326+ } ;
327+
328+ var userAttrsList = new List < AttributeType > ( ) ;
329+
330+ userAttrsList . Add ( userAttrs ) ;
331+
332+ var signUpRequest = new SignUpRequest
333+ {
334+ UserAttributes = userAttrsList ,
335+ Username = userName ,
336+ ClientId = clientId ,
337+ Password = password
338+ } ;
339+
340+ var response = await _cognitoService . SignUpAsync ( signUpRequest ) ;
341+ return response . HttpStatusCode == HttpStatusCode . OK ;
342+ }
343+
344+ // snippet-end:[Cognito.dotnetv4.SignUp]
345+ }
346+
347+ // snippet-end:[Cognito.dotnetv4.CognitoWrapper]
0 commit comments