Skip to content

Commit 3e5ccd0

Browse files
authored
Feat/support user context data (#168)
1 parent e27bf2a commit 3e5ccd0

File tree

5 files changed

+92
-50
lines changed

5 files changed

+92
-50
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Projects": [
3+
{
4+
"Name": "Amazon.Extensions.CognitoAuthentication",
5+
"Type": "Patch",
6+
"ChangelogMessages": [
7+
"Add support for UserContextData"
8+
]
9+
}
10+
]
11+
}

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,4 @@ Thanks [DmitryProskurin](https://github.com/DmitryProskurin) for the above chang
8383

8484
### Amazon.Extensions.CognitoAuthentication (2.1.0)
8585
* Added support for TOTP challenges, supports the existing way by defaulting to SMS, but also has an additional override method to allow setting the challenge type.
86-
* Make the methods of CognitoUser virtual so that mock test cases could be written for CognitoUser class.
86+
* Make the methods of CognitoUser virtual so that mock test cases could be written for CognitoUser class.

src/Amazon.Extensions.CognitoAuthentication/CognitoAuthenticationClasses.cs

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -69,50 +69,61 @@ public AuthFlowResponse(string sessionId, AuthenticationResultType authenticatio
6969
/// </summary>
7070
public IDictionary<string, string> ClientMetadata { get; }
7171

72-
/// <summary>
73-
/// The analytics metadata for collecting Amazon Pinpoint metrics.
74-
/// </summary>
72+
/// <summary>
73+
/// The analytics metadata for collecting Amazon Pinpoint metrics.
74+
/// </summary>
7575
public AnalyticsMetadataType AnalyticsMetadata { get; set; }
76+
77+
/// <summary>
78+
/// Additional UserContextDataType
79+
/// </summary>
80+
public UserContextDataType UserContextData { get; set; }
7681
}
7782

7883
/// <summary>
79-
/// Class containing the necessary properities to initiate SRP authentication flow
84+
/// Class containing the necessary properties to initiate SRP authentication flow
8085
/// </summary>
8186
public class InitiateSrpAuthRequest
8287
{
8388
/// <summary>
8489
/// The password for the corresponding CognitoUser.
8590
/// </summary>
86-
public string Password { get; set; }
87-
/// <summary>
88-
/// The password for the device associated with the corresponding CognitoUser
89-
/// </summary>
90-
public string DevicePass { get; set; }
91-
/// <summary>
92-
/// The device password verifier for the device associated with the corresponding CognitoUser
93-
/// </summary>
94-
public string DeviceVerifier { get; set; }
95-
/// <summary>
96-
/// The Device Key Group for the device associated with the corresponding CognitoUser
97-
/// </summary>
98-
public string DeviceGroupKey { get; set; }
91+
public string Password { get; set; }
92+
/// <summary>
93+
/// The password for the device associated with the corresponding CognitoUser
94+
/// </summary>
95+
public string DevicePass { get; set; }
96+
/// <summary>
97+
/// The device password verifier for the device associated with the corresponding CognitoUser
98+
/// </summary>
99+
public string DeviceVerifier { get; set; }
100+
/// <summary>
101+
/// The Device Key Group for the device associated with the corresponding CognitoUser
102+
/// </summary>
103+
public string DeviceGroupKey { get; set; }
99104
/// <summary>
100105
/// The client metadata for the current authentication flow.
101106
/// </summary>
102-
public IDictionary<string, string> ClientMetadata { get; set; }
103-
/// <summary>
104-
/// The analytics metadata for collecting Amazon Pinpoint metrics.
105-
/// </summary>
107+
public IDictionary<string, string> ClientMetadata { get; set; }
108+
/// <summary>
109+
/// The analytics metadata for collecting Amazon Pinpoint metrics.
110+
/// </summary>
106111
public AnalyticsMetadataType AnalyticsMetadata { get; set; }
107-
/// <summary>
108-
/// Enable custom auth flow
109-
/// https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-authentication-flow.html#Using-SRP-password-verification-in-custom-authentication-flow
112+
113+
/// <summary>
114+
/// Additional UserContextDataType
115+
/// </summary>
116+
public UserContextDataType UserContextData { get; set; }
117+
118+
/// <summary>
119+
/// Enable custom auth flow
120+
/// https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-authentication-flow.html#Using-SRP-password-verification-in-custom-authentication-flow
110121
/// </summary>
111122
public bool IsCustomAuthFlow { get; set; }
112123
}
113124

114125
/// <summary>
115-
/// Class containing the necessary properities to initiate custom authentication flow
126+
/// Class containing the necessary properties to initiate custom authentication flow
116127
/// </summary>
117128
public class InitiateCustomAuthRequest
118129
{
@@ -124,16 +135,21 @@ public class InitiateCustomAuthRequest
124135
/// <summary>
125136
/// The client metadata for the current authentication flow.
126137
/// </summary>
127-
public IDictionary<string, string> ClientMetadata { get; set; }
128-
129-
/// <summary>
130-
/// The analytics metadata for collecting Amazon Pinpoint metrics.
131-
/// </summary>
138+
public IDictionary<string, string> ClientMetadata { get; set; }
139+
140+
/// <summary>
141+
/// The analytics metadata for collecting Amazon Pinpoint metrics.
142+
/// </summary>
132143
public AnalyticsMetadataType AnalyticsMetadata { get; set; }
144+
145+
/// <summary>
146+
/// Additional UserContextDataType
147+
/// </summary>
148+
public UserContextDataType UserContextData { get; set; }
133149
}
134150

135151
/// <summary>
136-
/// Class containing the necessary properities to initiate either REFRESH_TOKEN or
152+
/// Class containing the necessary properties to initiate either REFRESH_TOKEN or
137153
/// REFRESH_TOKEN_AUTH authentication
138154
/// </summary>
139155
public class InitiateRefreshTokenAuthRequest
@@ -146,7 +162,7 @@ public class InitiateRefreshTokenAuthRequest
146162
}
147163

148164
/// <summary>
149-
/// Class containing the necessary properities to respond to an MFA authentication challenge
165+
/// Class containing the necessary properties to respond to an MFA authentication challenge
150166
/// </summary>
151167
public class RespondToMfaRequest
152168
{
@@ -167,7 +183,7 @@ public class RespondToMfaRequest
167183
}
168184

169185
/// <summary>
170-
/// Class containing the necessary properities to respond to an MFA authentication challenge
186+
/// Class containing the necessary properties to respond to an MFA authentication challenge
171187
/// </summary>
172188
public class RespondToSmsMfaRequest : RespondToMfaRequest
173189
{
@@ -178,7 +194,7 @@ public class RespondToSmsMfaRequest : RespondToMfaRequest
178194
}
179195

180196
/// <summary>
181-
/// Class containing the necessary properities to respond to a new password required authentication challenge
197+
/// Class containing the necessary properties to respond to a new password required authentication challenge
182198
/// </summary>
183199
public class RespondToNewPasswordRequiredRequest
184200
{
@@ -194,7 +210,7 @@ public class RespondToNewPasswordRequiredRequest
194210
}
195211

196212
/// <summary>
197-
/// Class containing the necessary properities to respond to a custom authentication challenge
213+
/// Class containing the necessary properties to respond to a custom authentication challenge
198214
/// </summary>
199215
public class RespondToCustomChallengeRequest
200216
{
@@ -208,15 +224,20 @@ public class RespondToCustomChallengeRequest
208224
/// </summary>
209225
public IDictionary<string, string> ClientMetadata { get; set; } = new Dictionary<string, string>();
210226

211-
/// <summary>
212-
/// The analytics metadata for collecting Amazon Pinpoint metrics.
213-
/// </summary>
227+
/// <summary>
228+
/// The analytics metadata for collecting Amazon Pinpoint metrics.
229+
/// </summary>
214230
public AnalyticsMetadataType AnalyticsMetadata { get; set; }
215231

216232
/// <summary>
217233
/// The sessionID for the current authentication flow.
218234
/// </summary>
219235
public string SessionID { get; set; }
236+
237+
/// <summary>
238+
/// Additional UserContextDataType
239+
/// </summary>
240+
public UserContextDataType UserContextData { get; set; }
220241
}
221242

222243
/// <summary>
@@ -232,11 +253,11 @@ public class InitiateAdminNoSrpAuthRequest
232253
/// <summary>
233254
/// Optional client metadata to provide in the Initiate Admin Authentication API call
234255
/// </summary>
235-
public IDictionary<string, string> ClientMetadata { get; set; }
236-
237-
/// <summary>
238-
/// Optional analytics metadata for collecting Amazon Pinpoint metrics.
239-
/// </summary>
240-
public AnalyticsMetadataType AnalyticsMetadata { get; set; }
256+
public IDictionary<string, string> ClientMetadata { get; set; }
257+
258+
/// <summary>
259+
/// Optional analytics metadata for collecting Amazon Pinpoint metrics.
260+
/// </summary>
261+
public AnalyticsMetadataType AnalyticsMetadata { get; set; }
241262
}
242263
}

src/Amazon.Extensions.CognitoAuthentication/CognitoUserAuthentication.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public virtual async Task<AuthFlowResponse> StartWithSrpAuthAsync(InitiateSrpAut
6868
initiateRequest.ClientMetadata = new Dictionary<string, string>(srpRequest.ClientMetadata);
6969
}
7070

71+
initiateRequest.UserContextData = srpRequest.UserContextData;
72+
7173
initiateRequest.AnalyticsMetadata = srpRequest.AnalyticsMetadata;
7274

7375
if (srpRequest.IsCustomAuthFlow)
@@ -86,6 +88,8 @@ public virtual async Task<AuthFlowResponse> StartWithSrpAuthAsync(InitiateSrpAut
8688
challengeRequest.ClientMetadata = new Dictionary<string, string>(srpRequest.ClientMetadata);
8789
}
8890

91+
challengeRequest.UserContextData = srpRequest.UserContextData;
92+
8993
challengeRequest.AnalyticsMetadata = srpRequest.AnalyticsMetadata;
9094

9195
bool challengeResponsesValid = challengeRequest != null && challengeRequest.ChallengeResponses != null;
@@ -243,7 +247,8 @@ public virtual async Task<AuthFlowResponse> StartWithCustomAuthAsync(InitiateCus
243247
AuthParameters = new Dictionary<string, string>(customRequest.AuthParameters),
244248
ClientId = ClientID,
245249
ClientMetadata = new Dictionary<string, string>(customRequest.ClientMetadata),
246-
AnalyticsMetadata = customRequest.AnalyticsMetadata
250+
AnalyticsMetadata = customRequest.AnalyticsMetadata,
251+
UserContextData = customRequest.UserContextData
247252
};
248253

249254
InitiateAuthResponse initiateResponse = await Provider.InitiateAuthAsync(authRequest, cancellationToken).ConfigureAwait(false);
@@ -289,7 +294,8 @@ public virtual async Task<AuthFlowResponse> RespondToCustomAuthAsync(RespondToCu
289294
ChallengeResponses = new Dictionary<string, string>(customRequest.ChallengeParameters),
290295
ClientMetadata = new Dictionary<string, string>(customRequest.ClientMetadata),
291296
AnalyticsMetadata = customRequest.AnalyticsMetadata,
292-
Session = customRequest.SessionID
297+
Session = customRequest.SessionID,
298+
UserContextData = customRequest.UserContextData
293299
};
294300

295301
RespondToAuthChallengeResponse authResponse =
@@ -546,7 +552,6 @@ public virtual async Task<AuthFlowResponse> RespondToNewPasswordRequiredAsync(Re
546552
{ CognitoConstants.ChlgParamNewPassword, newPasswordRequest.NewPassword},
547553
{ CognitoConstants.ChlgParamUsername, Username }
548554
};
549-
550555
if (requiredAttributes != null)
551556
{
552557
foreach (KeyValuePair<string, string> attribute in requiredAttributes)
@@ -754,7 +759,7 @@ private AdminInitiateAuthRequest CreateAdminAuthRequest(InitiateAdminNoSrpAuthRe
754759
{
755760
returnRequest.ClientMetadata = new Dictionary<string, string>(adminRequest.ClientMetadata);
756761
}
757-
762+
758763
returnRequest.AnalyticsMetadata = adminRequest.AnalyticsMetadata;
759764

760765
return returnRequest;

test/Amazon.Extensions.CognitoAuthentication.IntegrationTests/CognitoAWSCredentialsTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
using Amazon;
2323
using Amazon.CognitoIdentity;
24+
using Amazon.CognitoIdentityProvider.Model;
2425
using Amazon.CognitoIdentity.Model;
2526
using Amazon.Extensions.CognitoAuthentication;
2627
using Amazon.IdentityManagement;
@@ -54,7 +55,11 @@ public async Task TestGetCognitoAWSCredentials()
5455
AuthFlowResponse context =
5556
await user.StartWithSrpAuthAsync(new InitiateSrpAuthRequest()
5657
{
57-
Password = password
58+
Password = password,
59+
UserContextData = new UserContextDataType {
60+
EncodedData = "AmazonCognitoAdvancedSecurityData_object",
61+
IpAddress = "192.0.2.1"
62+
}
5863
});
5964

6065
//Create identity pool

0 commit comments

Comments
 (0)