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

Consolidating test statics #2956

Merged
merged 6 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
119 changes: 53 additions & 66 deletions test/Microsoft.IdentityModel.TestUtils/Default.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -398,38 +397,6 @@ public static string AadPayloadString
}.ToString();
}

public static string PayloadString
{
get => new JObject()
{
{ JwtRegisteredClaimNames.Aud, Audience },
{ JwtRegisteredClaimNames.Azp, Azp },
{ JwtRegisteredClaimNames.Email, "[email protected]" },
{ 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, "[email protected]" },
{ 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<Claim> PayloadClaims
{
get => new List<Claim>()
Expand Down Expand Up @@ -484,34 +451,17 @@ public static List<Claim> PayloadJsonClaims

public static Dictionary<string, object> PayloadJsonDictionary
{
get => new Dictionary<string, object>()
{
{ 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
{
get => new CaseSensitiveClaimsIdentity(PayloadClaims, "AuthenticationTypes.Federation");
}

public static Dictionary<string, object> PayloadDictionary
public static JObject PayloadAsJObject
{
get => new Dictionary<string, object>()
get => new JObject()
{
{ JwtRegisteredClaimNames.Aud, Audience },
{ JwtRegisteredClaimNames.Azp, Azp },
Expand All @@ -525,11 +475,21 @@ public static Dictionary<string, object> PayloadDictionary
};
}

public static Dictionary<string, object> PayloadDictionaryMultipleAudiences
public static string PayloadString
{
get => PayloadAsJObject.ToString(Formatting.None);
}

public static Dictionary<string, object> PayloadDictionary
{
get => PayloadAsJObject.ToObject<Dictionary<string, object>>();
}

public static JObject PayloadAsJObjectMultipleAudiences
{
get => new Dictionary<string, object>()
get => new JObject()
{
{ JwtRegisteredClaimNames.Aud, JsonSerializerPrimitives.CreateJsonElement(Default.Audiences) },
{ JwtRegisteredClaimNames.Aud, JArray.FromObject(Audiences) },
{ JwtRegisteredClaimNames.Azp, Azp },
{ JwtRegisteredClaimNames.Email, "[email protected]" },
{ JwtRegisteredClaimNames.Exp, EpochTime.GetIntDate(Expires).ToString() },
Expand All @@ -541,6 +501,16 @@ public static Dictionary<string, object> PayloadDictionaryMultipleAudiences
};
}

public static string PayloadStringMultipleAudiences
{
get => PayloadAsJObjectMultipleAudiences.ToString(Formatting.None);
}

public static Dictionary<string, object> PayloadDictionaryMultipleAudiences
{
get => PayloadAsJObjectMultipleAudiences.ToObject<Dictionary<string, object>>();
}

public static Dictionary<string, object> RemoveClaim(this Dictionary<string, object> payloadClaims, string claimName)
{
payloadClaims.Remove(claimName);
Expand Down Expand Up @@ -859,20 +829,37 @@ public static List<Claim> SamlClaims

/// <summary>
/// SamlClaims require the ability to split into name / namespace
/// The <see cref="ClaimTypes.Role"/> claims are added as a list
/// </summary>
public static Dictionary<string, object> SamlClaimsDictionary
{
get => new Dictionary<string, object>()
get
{
{ ClaimTypes.Country, "USA"},
{ ClaimTypes.NameIdentifier, "Bob" },
{ ClaimTypes.Email, "[email protected]" },
{ ClaimTypes.GivenName, "Bob" },
{ ClaimTypes.HomePhone, "555.1212" },
{ ClaimTypes.Role, new List<string>{"Developer", "Sales" } },
{ ClaimTypes.StreetAddress, "123AnyWhereStreet\r\nSomeTown/r/nUSA" },
{ ClaimsIdentity.DefaultNameClaimType, "Jean-S�bastien" }
};
Dictionary<string, object> dictionary = new Dictionary<string, object>();

foreach (Claim claim in SamlClaims)
{
if (dictionary.ContainsKey(claim.Type))
trwalke marked this conversation as resolved.
Show resolved Hide resolved
{
if (claim.Type == ClaimTypes.Role)
{
((List<string>)dictionary[claim.Type]).Add(claim.Value);
}
}
else
{
if (claim.Type == ClaimTypes.Role)
{
dictionary[claim.Type] = new List<string> { claim.Value };
}
else
{
dictionary[claim.Type] = claim.Value;
}
}
}
return dictionary;
}
}

/// <summary>
Expand Down
25 changes: 9 additions & 16 deletions test/Microsoft.IdentityModel.TestUtils/ReferenceTokens.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,39 +373,32 @@ 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<string, object> PayloadDictionary
public static JObject PayloadAsJObject
{
get => new Dictionary<string, object>()
get => new JObject()
{
{ JwtRegisteredClaimNames.Email, "[email protected]" },
{ 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() }
{ JwtRegisteredClaimNames.Exp, EpochTime.GetIntDate(Expires).ToString() },
trwalke marked this conversation as resolved.
Show resolved Hide resolved
};
}

public static string PayloadString
{
get => new JObject()
{
{ JwtRegisteredClaimNames.Email, "[email protected]" },
{ 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<string, object> PayloadDictionary
{
get => PayloadAsJObject.ToObject<Dictionary<string, object>>();
}

public static string Audience
Expand Down
Loading