Skip to content

Commit

Permalink
Support 6x where JsonWebToken.GetPayloadValue<string>("aud")
Browse files Browse the repository at this point in the history
Throws if multiple audiences exist.
  • Loading branch information
Brent Schmaltz authored and brentschmaltz committed Sep 8, 2023
1 parent 675a7fc commit 650e55d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,13 @@ internal T GetValue<T>(string key, bool throwEx, out bool found)
if (objType == typeof(DateTime))
return (T)((object)((DateTime)obj).ToString("o", CultureInfo.InvariantCulture));

return (T)((object)obj.ToString());
if (obj is List<string> list)
{
if (list.Count == 1)
return (T)((object)(list[0]));
}
else
return (T)((object)obj.ToString());
}
else if (typeof(T) == typeof(bool))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using Microsoft.IdentityModel.TestUtils;
using Microsoft.IdentityModel.Tokens;

namespace Microsoft.IdentityModel.JsonWebTokens.Tests
{
Expand All @@ -12,12 +11,10 @@ public class GetPayloadValueTheoryData : TheoryDataBase
public GetPayloadValueTheoryData(string testId) : base(testId)
{ }

public SecurityTokenDescriptor SecurityTokenDescriptor { get; set; }
public object ClaimValue { get; set; }

public string PropertyName { get; set; }

public Type PropertyOut { get; set; }

public Type PropertyType { get; set; }

public object PropertyValue { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,19 @@ public void CheckAudienceValues(GetPayloadValueTheoryData theoryData)
try
{
JsonWebToken jsonWebToken = new JsonWebToken(theoryData.Json);
IdentityComparer.AreEqual(jsonWebToken.Audiences, theoryData.PropertyValue, context);
MethodInfo method = typeof(JsonWebToken).GetMethod("GetPayloadValue");
MethodInfo generic = method.MakeGenericMethod(theoryData.PropertyType);
object[] parameters = new object[] { theoryData.PropertyName };
var audiences = generic.Invoke(jsonWebToken, parameters);

if (!IdentityComparer.AreEqual(jsonWebToken.Audiences, theoryData.PropertyValue, context))
context.AddDiff($"jsonWebToken.Audiences != theoryData.PropertyValue: '{jsonWebToken.Audiences}' != '{theoryData.PropertyValue}'.");

if (theoryData.ClaimValue != null)
if (!IdentityComparer.AreEqual(audiences, theoryData.ClaimValue, context))
context.AddDiff($"audiences != theoryData.ClaimValue: '{audiences}' != '{theoryData.ClaimValue}'.");

theoryData.ExpectedException.ProcessNoException(context);
}
catch (Exception ex)
{
Expand All @@ -263,31 +275,76 @@ public static TheoryData<GetPayloadValueTheoryData> CheckAudienceValuesTheoryDat
{
var theoryData = new TheoryData<GetPayloadValueTheoryData>();

theoryData.Add(new GetPayloadValueTheoryData("stringFromSingleInList")
{
ClaimValue = "audience",
PropertyName = "aud",
PropertyType = typeof(string),
PropertyValue = new List<string> { "audience" },
Json = JsonUtilities.CreateUnsignedToken("aud", new List<string> { "audience" })
});

theoryData.Add(new GetPayloadValueTheoryData("stringFromMultipeInList")
{
ClaimValue = "audience",
ExpectedException = ExpectedException.ArgumentException("IDX14305:"),
PropertyName = "aud",
PropertyValue = new List<string> { "audience", "audience2" },
PropertyType = typeof(string),
Json = JsonUtilities.CreateUnsignedToken("aud", new List<string> { "audience", "audience2" })
});

theoryData.Add(new GetPayloadValueTheoryData("stringTwoNulloneNonNull")
{
ClaimValue = "audience1",
PropertyName = "aud",
PropertyValue = new List<string> { "audience1" },
PropertyType = typeof(string),
Json = JsonUtilities.CreateUnsignedToken("aud", new List<string> { null, "audience1", null })
});

theoryData.Add(new GetPayloadValueTheoryData("stringFromCollection")
{
ClaimValue = "audience",
PropertyName = "aud",
PropertyType = typeof(string),
PropertyValue = new Collection<string> { "audience" },
Json = JsonUtilities.CreateUnsignedToken("aud", new Collection<string> { "audience" })
});

theoryData.Add(new GetPayloadValueTheoryData("singleNull")
{
ClaimValue = new List<string>(),
PropertyName = "aud",
PropertyValue = new List<string>(),
PropertyType = typeof(List<string>),
Json = JsonUtilities.CreateUnsignedToken("aud", null)
});

theoryData.Add(new GetPayloadValueTheoryData("twoNull")
{
ClaimValue = new List<string>(),
PropertyName = "aud",
PropertyValue = new List<string>(),
PropertyType = typeof(List<string>),
Json = JsonUtilities.CreateUnsignedToken("aud", new List<string>{ null, null })
});

theoryData.Add(new GetPayloadValueTheoryData("singleNonNull")
{
ClaimValue = new List<string> { "audience" },
PropertyName = "aud",
PropertyValue = new List<string> { "audience"},
PropertyType = typeof(List<string>),
Json = JsonUtilities.CreateUnsignedToken("aud", "audience")
});

theoryData.Add(new GetPayloadValueTheoryData("twoNulloneNonNull")
{
ClaimValue = new List<string> { "audience1" },
PropertyName = "aud",
PropertyValue = new List<string> { "audience1"},
PropertyType = typeof(List<string>),
Json = JsonUtilities.CreateUnsignedToken("aud", new List<string> { null, "audience1", null })
});

Expand Down Expand Up @@ -540,7 +597,7 @@ public static TheoryData<GetPayloadValueTheoryData> GetPayloadValueTheoryData
});
#endregion

#region collection of strings form simple types
#region collection of strings from simple types

#region string[]
theoryData.Add(new GetPayloadValueTheoryData("string[]dateTime")
Expand Down

0 comments on commit 650e55d

Please sign in to comment.