GithubHelp home page GithubHelp logo

zoxexivo / jwtutils Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 1.0 1.17 MB

Fastest, low-allocation JWT library

License: Apache License 2.0

C# 100.00%
jwt jwt-token jwt-bearer-tokens jwt-decode jwt-encode jwt-auth jwt-authentication

jwtutils's Introduction

JwtUtils

Build Status

Fastest, low-allocation, simple API library to work with JWT-tokens

It wrap most useful API to work with JWT (Create, Validate, Read)

Now you don't need to use strange and inconvenient API in other packages

Just install package from Nuget:

Install-Package JwtUtils
Available algorithms:

Symmetric: HS256, HS384, HS512

Asymmetric: RS256, RS384, RS512

How to generate RSA key?

ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key

Don't add passphrase

openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub

Benchmarks:

Comparing JwtUtils (Current) VS JWT-Dotnet VS System.IdentityModel.Tokens.Jwt

[HS256] Symmetric algorithms

[RS256] Symmetric algorithms

Usage

Symmetric algorithms: HS256, HS384, HS512

Create (Untyped payload)

var claims =  new Dictionary<string, object>
{
   { "exp", 1639942616 },
   { "uname", "i.a.ivanov" },
   { "claim1", "claim1_value" },   
   { "claims_array", new [] {"claim_item1", "claim_item2"}}
};
       
var token = JWT.HS256.Create(claims, "{TOKEN_SECRET}");

Create (Typed payload)

public class JwtPayload
{
   [JsonPropertyName("uid")] 
   public int UserId { get; set; }
}
       
var payload =  new JwtPayload
{
   UserId = 123
};
       
var token = JWT.HS256.Create(payload, "{TOKEN_SECRET}");

Validate

string token = "{YOUR_JWT_TOKEN}";
string tokenSecret = "{TOKEN_SECRET}";

if (JWT.HS256.ValidateSignature(token, tokenSecret))
{
   // Token signature valid
}

Read

string token = "{JWT-TOKEN}";

// Default - no typed
var tokenResult = JWT.Read(token);
var expiration = token.Exp();

// You can map Jwt to your own model
var tokenResult = JWT.Read<CustomJwtModel>(token);

Asymmetric algorithms: RS256, RS384, RS512

Create (Untyped payload)

// Private key in default PEM format
string privateKey = "@"MIIJKgIBAAKCAgEA9GF97STxVGbXpBFmudS/RRT58mfiR/+t2zb4f/uF3qmYb/yu
                       b3CtKwYPtGkQncSvba2HSurYArAxsCU2QeSAYbmCgtiXcF2Hw8Xt/ADY711iBDwq
                       .............................................
                       O9wqUEJy2v8xOMbHvMkoKLPLc590zGV88HNvzJHkF5N5HWTB9ZZEWcehf6RcTA==";
       
var payload =  new Dictionary<string, object>
{
   { "exp", 1639942616 },
   { "uname", "i.a.ivanov" },
   { "claim1", "claim1_value" },
   { "claim2", "claim2_value" },
   { "claim3", "claim3_value" },
   { "claims_array", new [] {"claim_item1", "claim_item2"}}
};
       
var token = JWT.RS256.Create(payload, privateKey);

Create (Typed payload)

// Private key in default PEM format
string privateKey = "@"MIIJKgIBAAKCAgEA9GF97STxVGbXpBFmudS/RRT58mfiR/+t2zb4f/uF3qmYb/yu
                       b3CtKwYPtGkQncSvba2HSurYArAxsCU2QeSAYbmCgtiXcF2Hw8Xt/ADY711iBDwq
                       .............................................
                       O9wqUEJy2v8xOMbHvMkoKLPLc590zGV88HNvzJHkF5N5HWTB9ZZEWcehf6RcTA==";
       
public class JwtPayload
{
  [JsonPropertyName("uid")] 
  public int UserId { get; set; }
}
       
var payload =  new JwtPayload
{
   UserId = 123
};
       
var token = JWT.RS256.Create(payload, privateKey);

Validate

// Public key with PEM format
string publicKey = "@"MIIJKgIBAAKCAgEA9GF97STxVGbXpBFmudS/RRT58mfiR/+t2zb4f/uF3qmYb/yu
                       b3CtKwYPtGkQncSvba2HSurYArAxsCU2QeSAYbmCgtiXcF2Hw8Xt/ADY711iBDwq
                       .............................................
                       O9wqUEJy2v8xOMbHvMkoKLPLc590zGV88HNvzJHkF5N5HWTB9ZZEWcehf6RcTA==";

if (JWT.RS256.ValidateSignature("{YOUR_JWT_TOKEN}", publicKey))
{
   // Token signature valid
}

Read

string token = "{JWT-TOKEN}";

// Default - untyped Dictionary<string, object>
var tokenResult = JWT.Read(token);
var expiration = token.Exp();

// You can map Jwt to your own model
var tokenResult = JWT.Read<CustomJwtModel>(token); 

jwtutils's People

Contributors

tbrillet avatar zoxexivo avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

thomasbrillet1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.