diff --git a/test/Microsoft.IdentityModel.TestUtils/Default.cs b/test/Microsoft.IdentityModel.TestUtils/Default.cs index 0080ecdc2c..fae32a4119 100644 --- a/test/Microsoft.IdentityModel.TestUtils/Default.cs +++ b/test/Microsoft.IdentityModel.TestUtils/Default.cs @@ -9,7 +9,6 @@ using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using Microsoft.IdentityModel.Tokens; -using Microsoft.IdentityModel.Tokens.Json; using Microsoft.IdentityModel.Tokens.Saml; using Microsoft.IdentityModel.Tokens.Saml2; using Microsoft.IdentityModel.Xml; @@ -398,38 +397,6 @@ public static string AadPayloadString }.ToString(); } - public static string PayloadString - { - get => new JObject() - { - { JwtRegisteredClaimNames.Aud, Audience }, - { JwtRegisteredClaimNames.Azp, Azp }, - { JwtRegisteredClaimNames.Email, "Bob@contoso.com" }, - { JwtRegisteredClaimNames.Exp, EpochTime.GetIntDate(Expires).ToString() }, - { JwtRegisteredClaimNames.GivenName, "Bob" }, - { JwtRegisteredClaimNames.Iss, Issuer }, - { JwtRegisteredClaimNames.Iat, EpochTime.GetIntDate(IssueInstant).ToString() }, - { JwtRegisteredClaimNames.Jti, Jti }, - { JwtRegisteredClaimNames.Nbf, EpochTime.GetIntDate(NotBefore).ToString()}, - }.ToString(Formatting.None); - } - - public static string PayloadStringMultipleAudiences - { - get => new JObject() - { - { JwtRegisteredClaimNames.Aud, JArray.FromObject(Audiences) }, - { JwtRegisteredClaimNames.Azp, Azp }, - { JwtRegisteredClaimNames.Email, "Bob@contoso.com" }, - { JwtRegisteredClaimNames.Exp, EpochTime.GetIntDate(Expires).ToString() }, - { JwtRegisteredClaimNames.GivenName, "Bob" }, - { JwtRegisteredClaimNames.Iss, Issuer }, - { JwtRegisteredClaimNames.Iat, EpochTime.GetIntDate(IssueInstant).ToString() }, - { JwtRegisteredClaimNames.Jti, Jti }, - { JwtRegisteredClaimNames.Nbf, EpochTime.GetIntDate(NotBefore).ToString()}, - }.ToString(Formatting.None); - } - public static List PayloadClaims { get => new List() @@ -484,24 +451,7 @@ public static List PayloadJsonClaims public static Dictionary PayloadJsonDictionary { - get => new Dictionary() - { - { JwtRegisteredClaimNames.Aud, Audience }, - { JwtRegisteredClaimNames.Iss, Issuer }, - { "ClaimValueTypes.String", "ClaimValueTypes.String.Value" }, - { "ClaimValueTypes.Boolean.true", true }, - { "ClaimValueTypes.Boolean.false", false }, - { "ClaimValueTypes.Double", 123.4 }, - { "ClaimValueTypes.DateTime.IS8061", DateTime.TryParse("2019-11-15T14:31:21.6101326Z", out DateTime dateTimeValue1) ? dateTimeValue1.ToUniversalTime() : new DateTime()}, - { "ClaimValueTypes.DateTime", DateTime.TryParse("2019-11-15", out DateTime dateTimeValue2) ? dateTimeValue2 : new DateTime()}, - { "ClaimValueTypes.JsonClaimValueTypes.Json1", JObject.Parse(@"{""jsonProperty1"":""jsonvalue1""}") }, - { "ClaimValueTypes.JsonClaimValueTypes.Json2", JObject.Parse(@"{""jsonProperty2"":""jsonvalue2""}") }, - { "ClaimValueTypes.JsonClaimValueTypes.JsonNull", "" }, - { "ClaimValueTypes.JsonClaimValueTypes.JsonArray1", JArray.Parse(@"[1,2,3]") }, - { "ClaimValueTypes.JsonClaimValueTypes.JsonArray2", JArray.Parse(@"[1,""2"",3]") }, - { "ClaimValueTypes.JsonClaimValueTypes.Integer1", 1 }, - { JwtRegisteredClaimNames.Exp, EpochTime.GetIntDate(Expires).ToString() } - }; + get => PayloadJsonClaims.ToDictionary(x => x.Type, x => (object)x.Value); } public static ClaimsIdentity PayloadClaimsIdentity @@ -509,9 +459,9 @@ public static ClaimsIdentity PayloadClaimsIdentity get => new CaseSensitiveClaimsIdentity(PayloadClaims, "AuthenticationTypes.Federation"); } - public static Dictionary PayloadDictionary + public static JObject PayloadAsJObject { - get => new Dictionary() + get => new JObject() { { JwtRegisteredClaimNames.Aud, Audience }, { JwtRegisteredClaimNames.Azp, Azp }, @@ -525,11 +475,21 @@ public static Dictionary PayloadDictionary }; } - public static Dictionary PayloadDictionaryMultipleAudiences + public static string PayloadString + { + get => PayloadAsJObject.ToString(Formatting.None); + } + + public static Dictionary PayloadDictionary + { + get => PayloadAsJObject.ToObject>(); + } + + public static JObject PayloadAsJObjectMultipleAudiences { - get => new Dictionary() + get => new JObject() { - { JwtRegisteredClaimNames.Aud, JsonSerializerPrimitives.CreateJsonElement(Default.Audiences) }, + { JwtRegisteredClaimNames.Aud, JArray.FromObject(Audiences) }, { JwtRegisteredClaimNames.Azp, Azp }, { JwtRegisteredClaimNames.Email, "Bob@contoso.com" }, { JwtRegisteredClaimNames.Exp, EpochTime.GetIntDate(Expires).ToString() }, @@ -541,6 +501,16 @@ public static Dictionary PayloadDictionaryMultipleAudiences }; } + public static string PayloadStringMultipleAudiences + { + get => PayloadAsJObjectMultipleAudiences.ToString(Formatting.None); + } + + public static Dictionary PayloadDictionaryMultipleAudiences + { + get => PayloadAsJObjectMultipleAudiences.ToObject>(); + } + public static Dictionary RemoveClaim(this Dictionary payloadClaims, string claimName) { payloadClaims.Remove(claimName); @@ -859,20 +829,37 @@ public static List SamlClaims /// /// SamlClaims require the ability to split into name / namespace + /// The claims are added as a list /// public static Dictionary SamlClaimsDictionary { - get => new Dictionary() + get { - { ClaimTypes.Country, "USA"}, - { ClaimTypes.NameIdentifier, "Bob" }, - { ClaimTypes.Email, "Bob@contoso.com" }, - { ClaimTypes.GivenName, "Bob" }, - { ClaimTypes.HomePhone, "555.1212" }, - { ClaimTypes.Role, new List{"Developer", "Sales" } }, - { ClaimTypes.StreetAddress, "123AnyWhereStreet\r\nSomeTown/r/nUSA" }, - { ClaimsIdentity.DefaultNameClaimType, "Jean-S�bastien" } - }; + Dictionary dictionary = new Dictionary(); + + foreach (Claim claim in SamlClaims) + { + if (dictionary.ContainsKey(claim.Type)) + { + if (claim.Type == ClaimTypes.Role) + { + ((List)dictionary[claim.Type]).Add(claim.Value); + } + } + else + { + if (claim.Type == ClaimTypes.Role) + { + dictionary[claim.Type] = new List { claim.Value }; + } + else + { + dictionary[claim.Type] = claim.Value; + } + } + } + return dictionary; + } } /// diff --git a/test/Microsoft.IdentityModel.TestUtils/ReferenceTokens.cs b/test/Microsoft.IdentityModel.TestUtils/ReferenceTokens.cs index 4baa159ad7..52b1d217bf 100644 --- a/test/Microsoft.IdentityModel.TestUtils/ReferenceTokens.cs +++ b/test/Microsoft.IdentityModel.TestUtils/ReferenceTokens.cs @@ -373,16 +373,13 @@ public static string SamlToken_Formated // { "int", 123 }. public static string JWSWithSingleAdditionalHeaderClaim = "eyJhbGciOiJSUzI1NiIsImtpZCI6Ikpzb25XZWJLZXlSc2FfMjA0OCIsInR5cCI6IkpXVCIsImludCI6MTIzfQ.eyJlbWFpbCI6IkJvYkBjb250b3NvLmNvbSIsImdpdmVuX25hbWUiOiJCb2IiLCJpc3MiOiJodHRwOi8vRGVmYXVsdC5Jc3N1ZXIuY29tIiwiYXVkIjoiaHR0cDovL0RlZmF1bHQuQXVkaWVuY2UuY29tIiwiaWF0IjoiMTQ4OTc3NTYxNyIsIm5iZiI6IjE0ODk3NzU2MTciLCJleHAiOiIyNTM0MDIzMDA3OTkifQ.DhPiCtD9HWTjG5LDCW8YxSaBXffmPosGnnKINuey6ec50Yf72SzBnMDVZ4Cw9S_SyqSRIxVs0x87g0ZUP8fytUxr_D7ksf0cBI9tqh2MgoAZ2lY8T8oflfIBaTLraZHRmjRCMZGdOLmGj__xqM7mmD0Y1grwAkQgMCLlze2qgCXmym_8jAWfSLQcNc-XNUaDZBlbgebic7TZ0INa93QcJvm_ov6t_rg90Y0l4xCxL_VOdXctdbc5D87bEaaAdqThfVMA1325JZdS_CBWVelLf5zZYPldVDxnD9l93Fy0gqWTWJ0QxMP-BDMgXbQQdUDoSC5HrxXU2JRXnF8V_V4G2g"; - // This token is unsigned and includes one additional header claim: // { "int", 123 }. public static string UnsignedJWSWithSingleAdditionalHeaderClaim = "eyJhbGciOiJub25lIiwidHlwIjoiSldUIiwiaW50IjoxMjN9.eyJlbWFpbCI6IkJvYkBjb250b3NvLmNvbSIsImdpdmVuX25hbWUiOiJCb2IiLCJpc3MiOiJodHRwOi8vRGVmYXVsdC5Jc3N1ZXIuY29tIiwiYXVkIjoiaHR0cDovL0RlZmF1bHQuQXVkaWVuY2UuY29tIiwiaWF0IjoiMTQ4OTc3NTYxNyIsIm5iZiI6IjE0ODk3NzU2MTciLCJleHAiOiIyNTM0MDIzMDA3OTkifQ."; - // the following values are separate from the one in Default.cs, so we can change the Defaults - // Do not change any of these values either adding new values or order or the tests will break. - public static Dictionary PayloadDictionary + public static JObject PayloadAsJObject { - get => new Dictionary() + get => new JObject() { { JwtRegisteredClaimNames.Email, "Bob@contoso.com" }, { JwtRegisteredClaimNames.GivenName, "Bob" }, @@ -390,22 +387,18 @@ public static Dictionary PayloadDictionary { JwtRegisteredClaimNames.Aud, Audience }, { JwtRegisteredClaimNames.Iat, EpochTime.GetIntDate(IssueInstant).ToString() }, { JwtRegisteredClaimNames.Nbf, EpochTime.GetIntDate(NotBefore).ToString()}, - { JwtRegisteredClaimNames.Exp, EpochTime.GetIntDate(Expires).ToString() } + { JwtRegisteredClaimNames.Exp, EpochTime.GetIntDate(Expires).ToString() }, }; } public static string PayloadString { - get => new JObject() - { - { JwtRegisteredClaimNames.Email, "Bob@contoso.com" }, - { JwtRegisteredClaimNames.GivenName, "Bob" }, - { JwtRegisteredClaimNames.Iss, Issuer }, - { JwtRegisteredClaimNames.Aud, Audience }, - { JwtRegisteredClaimNames.Iat, EpochTime.GetIntDate(IssueInstant).ToString() }, - { JwtRegisteredClaimNames.Nbf, EpochTime.GetIntDate(NotBefore).ToString()}, - { JwtRegisteredClaimNames.Exp, EpochTime.GetIntDate(Expires).ToString() }, - }.ToString(Formatting.None); + get => PayloadAsJObject.ToString(Formatting.None); + } + + public static Dictionary PayloadDictionary + { + get => PayloadAsJObject.ToObject>(); } public static string Audience