Skip to content

Commit 650e55d

Browse files
Brent Schmaltzbrentschmaltz
Brent Schmaltz
authored andcommitted
Support 6x where JsonWebToken.GetPayloadValue<string>("aud")
Throws if multiple audiences exist.
1 parent 675a7fc commit 650e55d

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

src/Microsoft.IdentityModel.JsonWebTokens/Json/JsonClaimSet.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,13 @@ internal T GetValue<T>(string key, bool throwEx, out bool found)
216216
if (objType == typeof(DateTime))
217217
return (T)((object)((DateTime)obj).ToString("o", CultureInfo.InvariantCulture));
218218

219-
return (T)((object)obj.ToString());
219+
if (obj is List<string> list)
220+
{
221+
if (list.Count == 1)
222+
return (T)((object)(list[0]));
223+
}
224+
else
225+
return (T)((object)obj.ToString());
220226
}
221227
else if (typeof(T) == typeof(bool))
222228
{

test/Microsoft.IdentityModel.JsonWebTokens.Tests/GetPayloadValueTheoryData.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using Microsoft.IdentityModel.TestUtils;
6-
using Microsoft.IdentityModel.Tokens;
76

87
namespace Microsoft.IdentityModel.JsonWebTokens.Tests
98
{
@@ -12,12 +11,10 @@ public class GetPayloadValueTheoryData : TheoryDataBase
1211
public GetPayloadValueTheoryData(string testId) : base(testId)
1312
{ }
1413

15-
public SecurityTokenDescriptor SecurityTokenDescriptor { get; set; }
14+
public object ClaimValue { get; set; }
1615

1716
public string PropertyName { get; set; }
1817

19-
public Type PropertyOut { get; set; }
20-
2118
public Type PropertyType { get; set; }
2219

2320
public object PropertyValue { get; set; }

test/Microsoft.IdentityModel.JsonWebTokens.Tests/JsonWebTokenTests.cs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,19 @@ public void CheckAudienceValues(GetPayloadValueTheoryData theoryData)
247247
try
248248
{
249249
JsonWebToken jsonWebToken = new JsonWebToken(theoryData.Json);
250-
IdentityComparer.AreEqual(jsonWebToken.Audiences, theoryData.PropertyValue, context);
250+
MethodInfo method = typeof(JsonWebToken).GetMethod("GetPayloadValue");
251+
MethodInfo generic = method.MakeGenericMethod(theoryData.PropertyType);
252+
object[] parameters = new object[] { theoryData.PropertyName };
253+
var audiences = generic.Invoke(jsonWebToken, parameters);
254+
255+
if (!IdentityComparer.AreEqual(jsonWebToken.Audiences, theoryData.PropertyValue, context))
256+
context.AddDiff($"jsonWebToken.Audiences != theoryData.PropertyValue: '{jsonWebToken.Audiences}' != '{theoryData.PropertyValue}'.");
257+
258+
if (theoryData.ClaimValue != null)
259+
if (!IdentityComparer.AreEqual(audiences, theoryData.ClaimValue, context))
260+
context.AddDiff($"audiences != theoryData.ClaimValue: '{audiences}' != '{theoryData.ClaimValue}'.");
261+
262+
theoryData.ExpectedException.ProcessNoException(context);
251263
}
252264
catch (Exception ex)
253265
{
@@ -263,31 +275,76 @@ public static TheoryData<GetPayloadValueTheoryData> CheckAudienceValuesTheoryDat
263275
{
264276
var theoryData = new TheoryData<GetPayloadValueTheoryData>();
265277

278+
theoryData.Add(new GetPayloadValueTheoryData("stringFromSingleInList")
279+
{
280+
ClaimValue = "audience",
281+
PropertyName = "aud",
282+
PropertyType = typeof(string),
283+
PropertyValue = new List<string> { "audience" },
284+
Json = JsonUtilities.CreateUnsignedToken("aud", new List<string> { "audience" })
285+
});
286+
287+
theoryData.Add(new GetPayloadValueTheoryData("stringFromMultipeInList")
288+
{
289+
ClaimValue = "audience",
290+
ExpectedException = ExpectedException.ArgumentException("IDX14305:"),
291+
PropertyName = "aud",
292+
PropertyValue = new List<string> { "audience", "audience2" },
293+
PropertyType = typeof(string),
294+
Json = JsonUtilities.CreateUnsignedToken("aud", new List<string> { "audience", "audience2" })
295+
});
296+
297+
theoryData.Add(new GetPayloadValueTheoryData("stringTwoNulloneNonNull")
298+
{
299+
ClaimValue = "audience1",
300+
PropertyName = "aud",
301+
PropertyValue = new List<string> { "audience1" },
302+
PropertyType = typeof(string),
303+
Json = JsonUtilities.CreateUnsignedToken("aud", new List<string> { null, "audience1", null })
304+
});
305+
306+
theoryData.Add(new GetPayloadValueTheoryData("stringFromCollection")
307+
{
308+
ClaimValue = "audience",
309+
PropertyName = "aud",
310+
PropertyType = typeof(string),
311+
PropertyValue = new Collection<string> { "audience" },
312+
Json = JsonUtilities.CreateUnsignedToken("aud", new Collection<string> { "audience" })
313+
});
314+
266315
theoryData.Add(new GetPayloadValueTheoryData("singleNull")
267316
{
317+
ClaimValue = new List<string>(),
268318
PropertyName = "aud",
269319
PropertyValue = new List<string>(),
320+
PropertyType = typeof(List<string>),
270321
Json = JsonUtilities.CreateUnsignedToken("aud", null)
271322
});
272323

273324
theoryData.Add(new GetPayloadValueTheoryData("twoNull")
274325
{
326+
ClaimValue = new List<string>(),
275327
PropertyName = "aud",
276328
PropertyValue = new List<string>(),
329+
PropertyType = typeof(List<string>),
277330
Json = JsonUtilities.CreateUnsignedToken("aud", new List<string>{ null, null })
278331
});
279332

280333
theoryData.Add(new GetPayloadValueTheoryData("singleNonNull")
281334
{
335+
ClaimValue = new List<string> { "audience" },
282336
PropertyName = "aud",
283337
PropertyValue = new List<string> { "audience"},
338+
PropertyType = typeof(List<string>),
284339
Json = JsonUtilities.CreateUnsignedToken("aud", "audience")
285340
});
286341

287342
theoryData.Add(new GetPayloadValueTheoryData("twoNulloneNonNull")
288343
{
344+
ClaimValue = new List<string> { "audience1" },
289345
PropertyName = "aud",
290346
PropertyValue = new List<string> { "audience1"},
347+
PropertyType = typeof(List<string>),
291348
Json = JsonUtilities.CreateUnsignedToken("aud", new List<string> { null, "audience1", null })
292349
});
293350

@@ -540,7 +597,7 @@ public static TheoryData<GetPayloadValueTheoryData> GetPayloadValueTheoryData
540597
});
541598
#endregion
542599

543-
#region collection of strings form simple types
600+
#region collection of strings from simple types
544601

545602
#region string[]
546603
theoryData.Add(new GetPayloadValueTheoryData("string[]dateTime")

0 commit comments

Comments
 (0)