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

An exception occures when an invalid JWT is passed to the TryDecode method #7

Closed
xin9le opened this issue Dec 17, 2020 · 0 comments · Fixed by #12
Closed

An exception occures when an invalid JWT is passed to the TryDecode method #7

xin9le opened this issue Dec 17, 2020 · 0 comments · Fixed by #12

Comments

@xin9le
Copy link
Contributor

xin9le commented Dec 17, 2020

Summary

The decoding process in JWT is generally performed on the token sent from the client side. For this token, invalid values such as missing strings need to be considered. Generally, the TryXxx method doesn't throw any exceptions and returns the boolean value of success/failure. However, TryDecode method throws an exception when an invalid value is passed.

Reproduction Code

using LitJWT;
using LitJWT.Algorithms;
using Utf8Json;

void Main()
{
    var algo = new HS256Algorithm(HS256Algorithm.GenerateRandomRecommendedKey());
    var encoder = new JwtEncoder(algo);
    var decoder = new JwtDecoder(encoder.SignAlgorithm);

    var input = new Person{ Name = "xin9le", Age = 36 };
    var expiry = TimeSpan.FromSeconds(10);
    var jwt = encoder.Encode(input, expiry, (x, w) => w.Write(JsonSerializer.SerializeUnsafe(x)));
    jwt.Dump("JWT");
    
    var invalidJwt = jwt.AsSpan(0, 10);
    var result = decoder.TryDecode(invalidJwt, x => JsonSerializer.Deserialize<Person>(x.ToArray()), out var output);
    
    result.Dump("Result");
    output.Dump("Output");
}

public class Person
{
    public string Name { get; init; }
    public int Age { get; init; }
}

Expected behavior

  • No exception occurs
  • Should return enum value like DecodeResult.InvalidFormat
neuecc added a commit that referenced this issue Jan 28, 2022
neuecc added a commit that referenced this issue Jan 28, 2022
Fix #7, TryDecode supports invalid json format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant