Skip to content

Commit

Permalink
Add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaytak committed Jun 18, 2024
1 parent fb3639d commit 7e4f21e
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Linq;
using System.Reflection;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using Microsoft.IdentityModel.TestUtils;
Expand Down Expand Up @@ -1514,6 +1515,56 @@ public static TheoryData<ParseTimeValuesTheoryData> ParseTimeValuesTheoryData
}
}

#if NET8_0_OR_GREATER
[Fact]
public void ParseToken_WithByteProperties()
{
// Arrange
var escapedAzp = "azp\u0027azp";
var unescapedAzp = "azp'azp";

RSA rsa = RSA.Create(2048);
RSAParameters rsaParameters = rsa.ExportParameters(true);
RsaSecurityKey rsaSecurityKey = new(rsaParameters) { KeyId = "RsaPrivate" };

var jwsTokenDescriptor = new SecurityTokenDescriptor
{
SigningCredentials = new(rsaSecurityKey, SecurityAlgorithms.RsaSha256, SecurityAlgorithms.Sha256),
TokenType = JwtHeaderParameterNames.Jwk,
Claims = new Dictionary<string, object>()
{
{ JwtRegisteredClaimNames.Exp, EpochTime.GetIntDate(Default.Expires) },
{ JwtRegisteredClaimNames.Nbf, EpochTime.GetIntDate(Default.NotBefore) },
{ JwtRegisteredClaimNames.Iat, EpochTime.GetIntDate(Default.IssueInstant) },
{ JwtRegisteredClaimNames.Iss, Default.Issuer },
{ JwtRegisteredClaimNames.Aud, Default.Audience },
{ JwtRegisteredClaimNames.Azp, escapedAzp },
{ JwtRegisteredClaimNames.Jti, Default.Jti },
{ "uknown_claim", "unknown_claim_value" },
}
};

var tokenStr = new JsonWebTokenHandler().CreateToken(jwsTokenDescriptor);

// Act
var jwt = new JsonWebToken(tokenStr);
jwt.TryGetPayloadValue(JwtRegisteredClaimNames.Iss, out string issuerFromPayload);
jwt.TryGetPayloadValue(JwtRegisteredClaimNames.Jti, out string jtiFromPayload);
jwt.TryGetPayloadValue(JwtRegisteredClaimNames.Azp, out string azpFromPayload);

// Assert
Assert.Equal(Default.Issuer, issuerFromPayload);
Assert.Equal(Default.Issuer, jwt.Issuer);
Assert.True(jwt.IssuerBytes.SequenceEqual(Encoding.UTF8.GetBytes(Default.Issuer)));
Assert.Equal(Default.Jti, jtiFromPayload);
Assert.Equal(Default.Jti, jwt.Id);
Assert.True(jwt.IdBytes.SequenceEqual(Encoding.UTF8.GetBytes(Default.Jti)));
Assert.Equal(unescapedAzp, azpFromPayload);
Assert.Equal(unescapedAzp, jwt.Azp);
Assert.True(jwt.AzpBytes.SequenceEqual(Encoding.UTF8.GetBytes(unescapedAzp)));
}
#endif

// Test ensures that we only try to populate a JsonWebToken from a string if it is a properly formatted JWT.
// More specifically, we only want to try and decode
// a JWT token if it has the correct number of (JWE or JWS) token parts.
Expand Down

0 comments on commit 7e4f21e

Please sign in to comment.