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

Null reference error when trying to Decode #486

Closed
BernieJ opened this issue Dec 13, 2023 · 2 comments · Fixed by #487
Closed

Null reference error when trying to Decode #486

BernieJ opened this issue Dec 13, 2023 · 2 comments · Fixed by #487
Assignees
Labels

Comments

@BernieJ
Copy link

BernieJ commented Dec 13, 2023

I am getting a null reference error when trying to decode a jwt token without adding a key paramater. In the example code it is shown. I am Using package JWT v 10.1.1

Code generating the token:

public string CreateJWT(Type claims)
{
  var payload = new Dictionary<string, object>
  {
  ...
  { "exp", DateTimeOffset.UtcNow.Add(TokenExpiry).ToUnixTimeSeconds() }
  };

  IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
  IJsonSerializer serializer = new JsonNetSerializer();
  IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
  IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
  string key = string.Empty; // cant encode without a key

  var token = encoder.Encode(payload, key);
  return token;
}

Test code:

 [Test]
 public void TestExpiredToken()
 {
   var expiry = new TimeSpan(0, 0, -10, 0);
   var tokenManager = new TokenManagement(expiry);
   var token = tokenManager.CreateJWT(response);

   Assert.Throws<TokenExpiredException>(() =>
   {
     IJsonSerializer serializer = new JsonNetSerializer();
     IDateTimeProvider provider = new UtcDateTimeProvider();

     IJwtValidator validator = new JwtValidator(serializer, provider);
     IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
     IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
     IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder, algorithm);

    // NullReference error gets thrown
     var decodedClaims = decoder.DecodeToObject<IDictionary<string, object>>(token);
   });
 }
@abatishchev
Copy link
Member

Thanks for reporting the issue!
Can you please post the resulting exception stack trace too?

@BernieJ
Copy link
Author

BernieJ commented Dec 14, 2023

Stack trace

System.NullReferenceException: Object reference not set to an instance of an object.
   at JWT.JwtDecoder.<>c.<AllKeysHaveValues>b__20_0(Byte[] key)
   at System.Array.TrueForAll[T](T[] array, Predicate`1 match)
   at JWT.JwtDecoder.AllKeysHaveValues(Byte[][] keys)
   at JWT.JwtDecoder.ValidSymmetricAlgorithm(Byte[][] keys, String decodedPayload, IJwtAlgorithm algorithm, Byte[] bytesToSign, Byte[] decodedSignature)
   at JWT.JwtDecoder.Validate(JwtParts jwt, Byte[][] keys)
   at JWT.JwtDecoder.Decode(JwtParts jwt, Byte[][] keys, Boolean verify)
   at JWT.JwtDecoder.Decode(JwtParts jwt, Byte[] key, Boolean verify)
   at JWT.JwtDecoder.Decode(JwtParts jwt, Boolean verify)
   at JWT.JwtDecoder.DecodeToObject(Type type, JwtParts jwt, Boolean verify)
   at JWT.JwtDecoderExtensions.DecodeToObject[T](IJwtDecoder decoder, JwtParts jwt, Boolean verify)
   at JWT.JwtDecoderExtensions.DecodeToObject[T](IJwtDecoder decoder, String token)
   at POC.Tests.Tests.<>c__DisplayClass2_0.<TestExpiredToken>b__0() in C:\Projects\.net 8 Rest POC\Tests\TokenManagementTests.cs:line 98
   at NUnit.Framework.Assert.Throws(IResolveConstraint expression, TestDelegate code, String message, Object[] args)>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants