Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the JSON deserialization error in Cognito triggered lambda - Issue #1644 #1646

Merged
merged 4 commits into from
Jan 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Description>Amazon Lambda .NET Core support - CognitoEvents package.</Description>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net8.0</TargetFrameworks>
<AssemblyTitle>Amazon.Lambda.CognitoEvents</AssemblyTitle>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>3.0.0</VersionPrefix>
<AssemblyName>Amazon.Lambda.CognitoEvents</AssemblyName>
<PackageId>Amazon.Lambda.CognitoEvents</PackageId>
<PackageTags>AWS;Amazon;Lambda</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class CognitoDefineAuthChallengeResponse : CognitoTriggerResponse
#if NETCOREAPP3_1_OR_GREATER
[System.Text.Json.Serialization.JsonPropertyName("issueTokens")]
#endif
public bool IssueTokens { get; set; }
public bool? IssueTokens { get; set; }

/// <summary>
/// Set to true if you want to terminate the current authentication process, or false otherwise.
Expand All @@ -32,6 +32,6 @@ public class CognitoDefineAuthChallengeResponse : CognitoTriggerResponse
#if NETCOREAPP3_1_OR_GREATER
[System.Text.Json.Serialization.JsonPropertyName("failAuthentication")]
#endif
public bool FailAuthentication { get; set; }
public bool? FailAuthentication { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public class CognitoVerifyAuthChallengeResponse : CognitoTriggerResponse
#if NETCOREAPP3_1_OR_GREATER
[System.Text.Json.Serialization.JsonPropertyName("answerCorrect")]
#endif
public bool AnswerCorrect { get; set; }
public bool? AnswerCorrect { get; set; }
}
}
73 changes: 73 additions & 0 deletions Libraries/test/EventsTests.Shared/EventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,47 @@ public void CognitoDefineAuthChallengeEventTest(Type serializerType)
}
}

[Theory]
[InlineData(typeof(JsonSerializer))]
#if NETCOREAPP3_1_OR_GREATER
[InlineData(typeof(Amazon.Lambda.Serialization.SystemTextJson.LambdaJsonSerializer))]
[InlineData(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
#endif
public void CognitoDefineAuthChallengeEventWithNullValuesTest(Type serializerType)
{
var serializer = Activator.CreateInstance(serializerType) as ILambdaSerializer;
using (var fileStream = LoadJsonTestFile("cognito-defineauthchallenge-event-with-null-values.json"))
{
var cognitoDefineAuthChallengeEvent = serializer.Deserialize<CognitoDefineAuthChallengeEvent>(fileStream);

AssertBaseClass(cognitoDefineAuthChallengeEvent);

Assert.Equal(2, cognitoDefineAuthChallengeEvent.Request.ClientMetadata.Count);
Assert.Equal("metadata_1", cognitoDefineAuthChallengeEvent.Request.ClientMetadata.ToArray()[0].Key);
Assert.Equal("metadata_value_1", cognitoDefineAuthChallengeEvent.Request.ClientMetadata.ToArray()[0].Value);
Assert.Equal("metadata_2", cognitoDefineAuthChallengeEvent.Request.ClientMetadata.ToArray()[1].Key);
Assert.Equal("metadata_value_2", cognitoDefineAuthChallengeEvent.Request.ClientMetadata.ToArray()[1].Value);

Assert.Equal(2, cognitoDefineAuthChallengeEvent.Request.Session.Count);

var session0 = cognitoDefineAuthChallengeEvent.Request.Session[0];
Assert.Equal("challenge1", session0.ChallengeName);
Assert.True(session0.ChallengeResult);
Assert.Null(session0.ChallengeMetadata);

var session1 = cognitoDefineAuthChallengeEvent.Request.Session[1];
Assert.Equal("challenge2", session1.ChallengeName);
Assert.False(session1.ChallengeResult);
Assert.Null(session1.ChallengeMetadata);

Assert.True(cognitoDefineAuthChallengeEvent.Request.UserNotFound);

Assert.Null(cognitoDefineAuthChallengeEvent.Response.ChallengeName);
Assert.Null(cognitoDefineAuthChallengeEvent.Response.IssueTokens);
Assert.Null(cognitoDefineAuthChallengeEvent.Response.FailAuthentication);
}
}

[Theory]
[InlineData(typeof(JsonSerializer))]
#if NETCOREAPP3_1_OR_GREATER
Expand Down Expand Up @@ -1007,6 +1048,38 @@ public void CognitoVerifyAuthChallengeEventTest(Type serializerType)
}
}


[Theory]
[InlineData(typeof(JsonSerializer))]
#if NETCOREAPP3_1_OR_GREATER
[InlineData(typeof(Amazon.Lambda.Serialization.SystemTextJson.LambdaJsonSerializer))]
[InlineData(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
#endif
public void CognitoVerifyAuthChallengeEventWithNullValuesTest(Type serializerType)
{
var serializer = Activator.CreateInstance(serializerType) as ILambdaSerializer;
using (var fileStream = LoadJsonTestFile("cognito-verifyauthchallenge-event-with-null-values.json"))
{
var cognitoVerifyAuthChallengeEvent = serializer.Deserialize<CognitoVerifyAuthChallengeEvent>(fileStream);

AssertBaseClass(cognitoVerifyAuthChallengeEvent);

Assert.Equal("answer_value", cognitoVerifyAuthChallengeEvent.Request.ChallengeAnswer);

Assert.Null(cognitoVerifyAuthChallengeEvent.Request.ClientMetadata);

Assert.Equal(2, cognitoVerifyAuthChallengeEvent.Request.PrivateChallengeParameters.Count);
Assert.Equal("private_1", cognitoVerifyAuthChallengeEvent.Request.PrivateChallengeParameters.ToArray()[0].Key);
Assert.Equal("private_value_1", cognitoVerifyAuthChallengeEvent.Request.PrivateChallengeParameters.ToArray()[0].Value);
Assert.Equal("private_2", cognitoVerifyAuthChallengeEvent.Request.PrivateChallengeParameters.ToArray()[1].Key);
Assert.Equal("private_value_2", cognitoVerifyAuthChallengeEvent.Request.PrivateChallengeParameters.ToArray()[1].Value);

Assert.True(cognitoVerifyAuthChallengeEvent.Request.UserNotFound);

Assert.Null(cognitoVerifyAuthChallengeEvent.Response.AnswerCorrect);
}
}

[Theory]
[InlineData(typeof(JsonSerializer))]
#if NETCOREAPP3_1_OR_GREATER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
<Content Include="$(MSBuildThisFileDirectory)cognito-customsmssender-event.json" />
<Content Include="$(MSBuildThisFileDirectory)cognito-customemailsender-event.json" />
<Content Include="$(MSBuildThisFileDirectory)cognito-custommessage-event.json" />
<Content Include="$(MSBuildThisFileDirectory)cognito-defineauthchallenge-event-with-null-values.json" />
<Content Include="$(MSBuildThisFileDirectory)cognito-migrateuser-event.json" />
<Content Include="$(MSBuildThisFileDirectory)cognito-pretokengeneration-event.json" />
<Content Include="$(MSBuildThisFileDirectory)cognito-verifyauthchallenge-event-with-null-values.json" />
<Content Include="$(MSBuildThisFileDirectory)cognito-pretokengenerationv2-event.json" />
<Content Include="$(MSBuildThisFileDirectory)cognito-verifyauthchallenge-event.json" />
<Content Include="$(MSBuildThisFileDirectory)cognito-postconfirmation-event.json" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"version": "1",
"region": "us-east-1",
"userPoolId": "us-east-1_id",
"userName": "username_uuid",
"callerContext": {
"awsSdkVersion": "version",
"clientId": "client_id"
},
"triggerSource": "trigger_source",
"request": {
"userAttributes": {
"attribute_1": "attribute_value_1",
"attribute_2": "attribute_value_2"
},
"session": [
{
"challengeName": "challenge1",
"challengeResult": true,
"challengeMetadata": null
},
{
"challengeName": "challenge2",
"challengeResult": false,
"challengeMetadata": null
}
],
"clientMetadata": {
"metadata_1": "metadata_value_1",
"metadata_2": "metadata_value_2"
},
"userNotFound": true
},
"response": {
"challengeName": null,
"issueTokens": null,
"failAuthentication": null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"version": "1",
"region": "us-east-1",
"userPoolId": "us-east-1_id",
"userName": "username_uuid",
"callerContext": {
"awsSdkVersion": "version",
"clientId": "client_id"
},
"triggerSource": "trigger_source",
"request": {
"userAttributes": {
"attribute_1": "attribute_value_1",
"attribute_2": "attribute_value_2"
},
"privateChallengeParameters": {
"private_1": "private_value_1",
"private_2": "private_value_2"
},
"challengeAnswer": "answer_value",
"clientMetadata": null,
"userNotFound": true
},
"response": {
"answerCorrect": null
}
}
Loading