Skip to content

Commit

Permalink
Add two test cases for (de)serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyencuong2596 committed Jun 17, 2024
1 parent 9c97398 commit 6aacac0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ public static OpenIdConnectConfiguration Read(ref Utf8JsonReader reader, OpenIdC
else if (reader.ValueTextEquals(Utf8Bytes.EndSessionEndpoint))
config.EndSessionEndpoint = JsonPrimitives.ReadString(ref reader, MetadataName.EndSessionEndpoint, ClassName, true);

else if (reader.ValueTextEquals(Encoding.UTF8.GetBytes(JsonWebKeySetParameterNames.Keys)))
{
reader.Read();
if (config.JsonWebKeySet == null)
config.JsonWebKeySet = new JsonWebKeySet();
JsonWebKeySetSerializer.Read(ref reader, config.JsonWebKeySet);
}

// FrontchannelLogoutSessionSupported and FrontchannelLogoutSupported are per spec 'boolean'.
// We shipped pervious versions accepting a string and transforming to a boolean.
else if (reader.ValueTextEquals(Utf8Bytes.FrontchannelLogoutSessionSupported))
Expand Down Expand Up @@ -282,9 +290,6 @@ public static OpenIdConnectConfiguration Read(ref Utf8JsonReader reader, OpenIdC
else if (reader.ValueTextEquals(Utf8Bytes.UserInfoSigningAlgValuesSupported))
JsonPrimitives.ReadStrings(ref reader, config.UserInfoEndpointSigningAlgValuesSupported, MetadataName.UserInfoSigningAlgValuesSupported, ClassName, true);

else if (reader.ValueTextEquals(JsonWebKeySetParameterNames.Keys))
JsonWebKeySetSerializer.ReadKeys(ref reader, config.JsonWebKeySet);

#endregion
else
{
Expand Down Expand Up @@ -359,91 +364,91 @@ public static OpenIdConnectConfiguration Read(ref Utf8JsonReader reader, OpenIdC
else if (propertyName.Equals(MetadataName.IdTokenEncryptionEncValuesSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.IdTokenEncryptionEncValuesSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName.IdTokenSigningAlgValuesSupported , StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.IdTokenSigningAlgValuesSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.IdTokenSigningAlgValuesSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. IntrospectionEndpoint, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.IntrospectionEndpoint, StringComparison.OrdinalIgnoreCase))
config.IntrospectionEndpoint = JsonPrimitives.ReadString(ref reader, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. IntrospectionEndpointAuthMethodsSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.IntrospectionEndpointAuthMethodsSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.IntrospectionEndpointAuthMethodsSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. IntrospectionEndpointAuthSigningAlgValuesSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.IntrospectionEndpointAuthSigningAlgValuesSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.IntrospectionEndpointAuthSigningAlgValuesSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. Issuer, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.Issuer, StringComparison.OrdinalIgnoreCase))
config.Issuer = JsonPrimitives.ReadString(ref reader, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. JwksUri, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.JwksUri, StringComparison.OrdinalIgnoreCase))
config.JwksUri = JsonPrimitives.ReadString(ref reader, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. LogoutSessionSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.LogoutSessionSupported, StringComparison.OrdinalIgnoreCase))
config.LogoutSessionSupported = JsonPrimitives.ReadBoolean(ref reader, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. OpPolicyUri, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.OpPolicyUri, StringComparison.OrdinalIgnoreCase))
config.OpPolicyUri = JsonPrimitives.ReadString(ref reader, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. OpTosUri, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.OpTosUri, StringComparison.OrdinalIgnoreCase))
config.OpTosUri = JsonPrimitives.ReadString(ref reader, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. RegistrationEndpoint, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.RegistrationEndpoint, StringComparison.OrdinalIgnoreCase))
config.RegistrationEndpoint = JsonPrimitives.ReadString(ref reader, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. RequestObjectEncryptionAlgValuesSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.RequestObjectEncryptionAlgValuesSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.RequestObjectEncryptionAlgValuesSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. RequestObjectEncryptionEncValuesSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.RequestObjectEncryptionEncValuesSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.RequestObjectEncryptionEncValuesSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. RequestObjectSigningAlgValuesSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.RequestObjectSigningAlgValuesSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.RequestObjectSigningAlgValuesSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. RequestParameterSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.RequestParameterSupported, StringComparison.OrdinalIgnoreCase))
config.RequestParameterSupported = JsonPrimitives.ReadBoolean(ref reader, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. RequestUriParameterSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.RequestUriParameterSupported, StringComparison.OrdinalIgnoreCase))
config.RequestUriParameterSupported = JsonPrimitives.ReadBoolean(ref reader, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. RequireRequestUriRegistration, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.RequireRequestUriRegistration, StringComparison.OrdinalIgnoreCase))
config.RequireRequestUriRegistration = JsonPrimitives.ReadBoolean(ref reader, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. ResponseModesSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.ResponseModesSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.ResponseModesSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. ResponseTypesSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.ResponseTypesSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.ResponseTypesSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. ScopesSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.ScopesSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.ScopesSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. ServiceDocumentation, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.ServiceDocumentation, StringComparison.OrdinalIgnoreCase))
config.ServiceDocumentation = JsonPrimitives.ReadString(ref reader, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. SubjectTypesSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.SubjectTypesSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.SubjectTypesSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. TokenEndpoint, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.TokenEndpoint, StringComparison.OrdinalIgnoreCase))
config.TokenEndpoint = JsonPrimitives.ReadString(ref reader, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. TokenEndpointAuthMethodsSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.TokenEndpointAuthMethodsSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.TokenEndpointAuthMethodsSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. TokenEndpointAuthSigningAlgValuesSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.TokenEndpointAuthSigningAlgValuesSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.TokenEndpointAuthSigningAlgValuesSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. UILocalesSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.UILocalesSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.UILocalesSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. UserInfoEncryptionAlgValuesSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.UserInfoEncryptionAlgValuesSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.UserInfoEndpointEncryptionAlgValuesSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. UserInfoEncryptionEncValuesSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.UserInfoEncryptionEncValuesSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.UserInfoEndpointEncryptionEncValuesSupported, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. UserInfoEndpoint, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.UserInfoEndpoint, StringComparison.OrdinalIgnoreCase))
config.UserInfoEndpoint = JsonPrimitives.ReadString(ref reader, propertyName, ClassName);

else if (propertyName.Equals(MetadataName. UserInfoSigningAlgValuesSupported, StringComparison.OrdinalIgnoreCase))
else if (propertyName.Equals(MetadataName.UserInfoSigningAlgValuesSupported, StringComparison.OrdinalIgnoreCase))
JsonPrimitives.ReadStrings(ref reader, config.UserInfoEndpointSigningAlgValuesSupported, propertyName, ClassName);

}
Expand Down Expand Up @@ -585,7 +590,10 @@ public static void Write(ref Utf8JsonWriter writer, OpenIdConnectConfiguration c
JsonPrimitives.WriteStrings(ref writer, Utf8Bytes.ResponseTypesSupported, config.ResponseTypesSupported);

if (config.ShouldSerializeJsonWebKeys && config.JsonWebKeySet != null && config.JsonWebKeySet.Keys.Count > 0)
{
writer.WritePropertyName(Encoding.UTF8.GetBytes(JsonWebKeySetParameterNames.Keys));
JsonWebKeySetSerializer.Write(ref writer, config.JsonWebKeySet);
}

if (config.ScopesSupported.Count > 0)
JsonPrimitives.WriteStrings(ref writer, Utf8Bytes.ScopesSupported, config.ScopesSupported);
Expand Down Expand Up @@ -619,7 +627,7 @@ public static void Write(ref Utf8JsonWriter writer, OpenIdConnectConfiguration c

if (config.UserInfoEndpointSigningAlgValuesSupported.Count > 0)
JsonPrimitives.WriteStrings(ref writer, Utf8Bytes.UserInfoSigningAlgValuesSupported, config.UserInfoEndpointSigningAlgValuesSupported);

if (config.AdditionalData.Count > 0)
JsonPrimitives.WriteObjects(ref writer, config.AdditionalData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,17 +594,6 @@ public static OpenIdConnectConfiguration Default
get => SetDefaultConfiguration(new OpenIdConnectConfiguration());
}

public static OpenIdConnectConfiguration WithJsonWebKeySet
{
get
{
var config = Default;
config.JsonWebKeySet = JsonUtilities.FullyPopulatedJsonWebKeySet();
config.ShouldSerializeJsonWebKeys = true;
return config;
}
}

private static OpenIdConnectConfiguration SetDefaultConfiguration(OpenIdConnectConfiguration config)
{
AddToCollection(config.AcrValuesSupported, "acr_value1", "acr_value2", "acr_value3");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ public void DeserializeOpenIdConnectConfigurationWithSigningKeys()
TestUtilities.AssertFailIfErrors(context);
}

[Fact]
public void DeserializeOpenIdConnectConfigurationWithJsonWebKeySet()
{
TestUtilities.WriteHeader($"{this}.DeserializeOpenIdConnectConfigurationWithJsonWebKeySet");
var context = new CompareContext();
var config = OpenIdConfigData.FullyPopulatedWithKeys;
config.ShouldSerializeJsonWebKeys = true;
var json = OpenIdConnectConfiguration.Write(config);
var actualConfig = OpenIdConnectConfiguration.Create(json);

// "JsonWebKeySet" should be identical
IdentityComparer.AreEqual(OpenIdConfigData.FullyPopulatedWithKeys.JsonWebKeySet, actualConfig.JsonWebKeySet, context);

TestUtilities.AssertFailIfErrors(context);
}

[Fact]
public void GetSets()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static TheoryData<OpenIdConnectTheoryData> DesrializeTheoryData
get
{
TheoryData<OpenIdConnectTheoryData> theoryData = new TheoryData<OpenIdConnectTheoryData>();

// the reason to replace AdditionalData with upper case is because the test deserializes uppercase and lowercase.
// we wanted to leave the data sets in original form from discovery to be used in other tests.
theoryData.Add(new OpenIdConnectTheoryData("AADCommonV1")
Expand Down Expand Up @@ -138,15 +138,16 @@ public static TheoryData<OpenIdConnectTheoryData> DesrializeTheoryData
CompareTo = OpenIdConfigData.NullConfig,
Json = JsonData.NullObject
});

theoryData.Add(new OpenIdConnectTheoryData("SerializeJsonWebKeySet")
{
CompareTo = OpenIdConfigData.WithJsonWebKeySet,
Json = ""
CompareTo = OpenIdConfigData.FullyPopulatedWithKeys,
Json = OpenIdConfigData.JsonAllValues
});

return theoryData;
}
}

}
}

0 comments on commit 6aacac0

Please sign in to comment.