-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decouple benchmark tests from test projects.
Adjusted tests to use the same claims, keys and more realistic settings in runs.
- Loading branch information
1 parent
e25592a
commit 21d2f6d
Showing
9 changed files
with
210 additions
and
96 deletions.
There are no files selected for viewing
109 changes: 99 additions & 10 deletions
109
benchmark/Microsoft.IdentityModel.Benchmarks/BenchmarkUtils.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,113 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.IdentityModel.Tokens; | ||
using Microsoft.IdentityModel.TestUtils; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Security.Cryptography; | ||
using Microsoft.IdentityModel.Protocols; | ||
using Microsoft.IdentityModel.JsonWebTokens; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace Microsoft.IdentityModel.Benchmarks | ||
{ | ||
public class BenchmarkUtils | ||
{ | ||
public static Dictionary<string, object> SimpleClaims | ||
public const string Issuer = "http://www.contoso.com"; | ||
|
||
public const string Audience = "http://www.contoso.com/protected"; | ||
|
||
private static RSA _rsa; | ||
private static SymmetricSecurityKey _symmetricKey; | ||
|
||
public static RSA RSA | ||
{ | ||
get | ||
{ | ||
if (_rsa == null) | ||
{ | ||
_rsa = RSA.Create(); | ||
_rsa.KeySize = 2048; | ||
} | ||
|
||
return _rsa; | ||
} | ||
} | ||
|
||
public static RSAParameters RsaParameters => RSA.ExportParameters(true); | ||
|
||
public static RSAParameters RsaParametersPublic => RSA.ExportParameters(false); | ||
|
||
public static RsaSecurityKey RsaSecurityKey => new(RsaParameters) { KeyId = "RsaPrivate" }; | ||
|
||
public static RsaSecurityKey RsaSecurityKeyPublic => new(RsaParametersPublic) { KeyId = "RsaPublic" }; | ||
|
||
public static Dictionary<string, object> Claims | ||
{ | ||
get | ||
{ | ||
DateTime now = DateTime.UtcNow; | ||
return new Dictionary<string, object>() | ||
{ | ||
{ "role", new List<string>() { "role1", "Developer", "Sales"} }, | ||
{ JwtRegisteredClaimNames.Email, "[email protected]" }, | ||
{ JwtRegisteredClaimNames.Exp, EpochTime.GetIntDate(now + TimeSpan.FromDays(1)) }, | ||
{ JwtRegisteredClaimNames.Nbf, EpochTime.GetIntDate(now) }, | ||
{ JwtRegisteredClaimNames.Iat, EpochTime.GetIntDate(now) }, | ||
{ JwtRegisteredClaimNames.GivenName, "Bob" }, | ||
{ JwtRegisteredClaimNames.Iss, Issuer }, | ||
{ JwtRegisteredClaimNames.Aud, Audience } | ||
}; | ||
} | ||
} | ||
|
||
public static SigningCredentials SigningCredentialsRsaSha256 => new(RsaSecurityKey, SecurityAlgorithms.RsaSha256, SecurityAlgorithms.Sha256); | ||
|
||
public static EncryptingCredentials EncryptingCredentialsAes256Sha512 => new(SymmetricEncryptionKey512, "dir", SecurityAlgorithms.Aes256CbcHmacSha512); | ||
|
||
public static SymmetricSecurityKey SymmetricEncryptionKey512 | ||
{ | ||
get | ||
{ | ||
_symmetricKey ??= new SymmetricSecurityKey(SHA512.Create().ComputeHash(Guid.NewGuid().ToByteArray())); | ||
return _symmetricKey; | ||
} | ||
} | ||
|
||
public static string CreateCnfClaim(RsaSecurityKey key, string algorithm) | ||
{ | ||
get => new Dictionary<string, object>() | ||
return "{\"jwk\":" + CreateJwkClaim(key, algorithm) + "}"; | ||
} | ||
|
||
public static string CreateJwkClaim(RsaSecurityKey key, string algorithm) | ||
{ | ||
RSAParameters rsaParameters = ((key.Rsa == null) ? key.Parameters : key.Rsa.ExportParameters(includePrivateParameters: false)); | ||
return "{\"kty\":\"RSA\",\"n\":\"" + | ||
Base64UrlEncoder.Encode(rsaParameters.Modulus) + | ||
"\",\"e\":\"" + | ||
Base64UrlEncoder.Encode(rsaParameters.Exponent) + | ||
"\",\"alg\":\"" + | ||
algorithm + | ||
"\",\"kid\":\"" + | ||
key.KeyId + | ||
"\"}"; | ||
} | ||
|
||
public static string CreateAccessTokenWithCnf() | ||
{ | ||
Dictionary<string, object> claims = new Dictionary<string, object>(Claims); | ||
claims.Add("cnf", CreateCnfClaim(RsaSecurityKeyPublic, SecurityAlgorithms.RsaSha256)); | ||
return new JsonWebTokenHandler().CreateToken(new SecurityTokenDescriptor | ||
{ | ||
{ "role", new List<string>() { "role1", "Developer", "Sales"} }, | ||
{ "email", "[email protected]" }, | ||
{ "exp", EpochTime.GetIntDate(Default.Expires).ToString() }, | ||
{ "nbf", EpochTime.GetIntDate(Default.NotBefore).ToString() }, | ||
{ "iat", EpochTime.GetIntDate(Default.IssueInstant).ToString() } | ||
}; | ||
SigningCredentials = SigningCredentialsRsaSha256, | ||
Claims = claims, | ||
TokenType = JwtHeaderParameterNames.Jwk | ||
}); | ||
} | ||
|
||
public static HttpRequestData HttpRequestData => new() | ||
{ | ||
Method = "GET", | ||
Uri = new Uri("https://www.relyingparty.com") | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.