Skip to content

Commit

Permalink
2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Apr 28, 2022
1 parent 0efd531 commit a73d334
Show file tree
Hide file tree
Showing 14 changed files with 463 additions and 1,092 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/build-debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-dotnet@v1
with:
dotnet-version: 2.1.x
- uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.x

dotnet-version: |
5.0.x
6.0.x
- run: dotnet build -c Debug
- run: dotnet test -c Debug --no-build
13 changes: 6 additions & 7 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ on:
env:
GIT_TAG: ${{ github.event.inputs.tag }}
DRY_RUN: ${{ github.event.inputs.dry_run }}
DOTNET_SDK_VERISON_2: 2.1.x
DOTNET_SDK_VERISON_3: 3.1.x

jobs:
build-dotnet:
Expand All @@ -28,10 +26,9 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-dotnet@v1
with:
dotnet-version: ${{ env.DOTNET_SDK_VERISON_2 }}
- uses: actions/setup-dotnet@v1
with:
dotnet-version: ${{ env.DOTNET_SDK_VERISON_3 }}
dotnet-version: |
5.0.x
6.0.x
# pack nuget
- run: dotnet build -c Release -p:Version=${{ env.GIT_TAG }}
- run: dotnet test -c Release --no-build -p:Version=${{ env.GIT_TAG }}
Expand All @@ -53,7 +50,9 @@ jobs:
# setup dotnet for nuget push
- uses: actions/setup-dotnet@v1
with:
dotnet-version: ${{ env.DOTNET_SDK_VERISON_3 }}
dotnet-version: |
5.0.x
6.0.x
# tag
- uses: actions/checkout@v2
- name: tag
Expand Down
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
LitJWT
===

Lightweight, Fast [JWT(JSON Web Token)](https://jwt.io/) implementation for .NET Core. This library mainly focus on performance, 5 times faster encoding/decoding and very low allocation.
Lightweight, Fast [JWT(JSON Web Token)](https://jwt.io/) implementation for .NET. This library mainly focus on performance, 5 times faster encoding/decoding and very low allocation.

![image](https://user-images.githubusercontent.com/46207/58414904-c4c31300-80b7-11e9-9bd2-12f794518494.png)

NuGet: [LitJWT](https://www.nuget.org/packages/LitJWT), Currently only supports `.NET Core 2.1`(for performance reason, this libs uses many `Span<T>` based API).
NuGet: [LitJWT](https://www.nuget.org/packages/LitJWT)

Supported platform is `netstandard 2.1`, `net5.0` or greater.

```
Install-Package LitJWT
Expand Down Expand Up @@ -40,16 +42,15 @@ var key = HS256Algorithm.GenerateRandomRecommendedKey();
var encoder = new JwtEncoder(new HS256Algorithm(key));

// Encode with payload, expire, and use specify payload serializer.
var token = encoder.Encode(new { foo = "pay", bar = "load" }, TimeSpan.FromMinutes(30),
(x, writer) => writer.Write(Utf8Json.JsonSerializer.SerializeUnsafe(x)));
var token = encoder.Encode(new { foo = "pay", bar = "load" }, TimeSpan.FromMinutes(30));
```

```csharp
// Create decoder, JwtDecoder is also thread-safe so recommend to store static/singleton.
var decoder = new JwtDecoder(encoder.SignAlgorithm);

// Decode and verify, you can check the result.
var result = decoder.TryDecode(token, x => Utf8Json.JsonSerializer.Deserialize<PayloadSample>(x.ToArray()), out var payload);
var result = decoder.TryDecode(token, out var payload);
if (result == DecodeResult.Success)
{
Console.WriteLine((payload.foo, payload.bar));
Expand All @@ -58,7 +59,9 @@ if (result == DecodeResult.Success)

Custom Serializer
---
Encode method receives `Action<T, JwtWriter> payloadWriter`. You have to invoke `writer.Write(ReadOnlySpan<byte> payload)` method to serialize. `ReadOnlySpan<byte>` must be Utf8 binary, so if you use utf8 based serializer(or Json writer) such as [Utf8Json](https://github.com/neuecc/Utf8Json), achives maximum performance.
In default. LitJWT is using `System.Text.Json.JsonSerializer`. If you want to use custom `JsonSerializerOptions`, `JwtEncoder` and `JwtDecoder` have `JsonSerializerOptions serializerOptions` constructor overload.

If you want to use another serializer, encode method receives `Action<T, JwtWriter> payloadWriter`. You have to invoke `writer.Write(ReadOnlySpan<byte> payload)` method to serialize. `ReadOnlySpan<byte>` must be Utf8 binary.

Here is the sample of use JSON.NET, this have encoding overhead.

Expand All @@ -67,7 +70,7 @@ var token = encoder.Encode(new PayloadSample { foo = "pay", bar = "load" }, Time
(x, writer) => writer.Write(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(x))));
```

Decode method receives `delegate T PayloadParser<T>(ReadOnlySpan<byte> payload)`. `ReadOnlySpan<byte>` is utf8 json. Yes, utf8 based serialize is best but you can also use JSON.NET(but have encoding penalty).
Decode method receives `delegate T PayloadParser<T>(ReadOnlySpan<byte> payload)`. `ReadOnlySpan<byte>` is utf8 json. Yes, utf8 based serializer is best but you can also use JSON.NET(but have encoding penalty).

```
var result = decoder.TryDecode(token, x => JsonConvert.DeserializeObject<PayloadSample>(Encoding.UTF8.GetString(x)), out var payload);
Expand Down
2 changes: 1 addition & 1 deletion sandbox/Benchmark/Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion sandbox/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion sandbox/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static void Main(string[] args)
var rs256 = new LitJWT.JwtEncoder(new LitJWT.Algorithms.RS256Algorithm(() => RSA.Create(rsaParams), () => RSA.Create(rsaParams)));
var foo = rs256.Encode(payload, null, (x, writer) => writer.Write(Utf8Json.JsonSerializer.SerializeUnsafe(x)));



}
}
Expand Down
Loading

0 comments on commit a73d334

Please sign in to comment.