jwt_hs256_parse

Crates.iojwt_hs256_parse
lib.rsjwt_hs256_parse
version0.1.1
sourcesrc
created_at2022-08-16 13:14:54.78448
updated_at2022-08-16 16:39:22.839877
descriptionfork form https://github.com/h-i-v-e/jwt_hs256_parse
homepagehttps://github.com/h-i-v-e/jwt_hs256_parse
repositoryhttps://github.com/Tsingshen/jwt_hs256_parse
max_upload_size
id646655
size9,989
changqing_shen (changqings)

documentation

README

A simple library for generating and parsing JWT tokens using HMAC SHA256 as per https://jwt.io/introduction

###Encoding example

use jwt_hs256_parse;
use serde::Serialize;

#[derive(Serialize)]
struct Claims{
    sub: String,
    name: String,
    admin: bool
}

fn main(){
    let secret = "I'm a secret".as_bytes();
    let claims = Claims{
        sub: "1234567890".to_string(),
        name: "John Doe".to_string(),
        admin: true
    };
    match jwt_hs256_parse::create(secret, &claims) {
        Ok(token) => println!("Token: {}", token),
        Err(error) => println!("This can't be happening {}", error)
    }
}

###Decoding example

use jwt_hs256_parse;
use serde::Deserialize;

#[derive(Deserialize)]
struct Claims{
   sub: String
}

fn main(){
   let secret = "I'm a secret".as_bytes();
   match jwt_hs256_parse::parse::<Claims>(
       secret,
       "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.azXPRJHeWcZ_B5WHtA98gsnowX5gifvMJX2hoH_4YPs"
  ){
         Ok(claims) => println!("Sub: {}", claims.sub),
         Err(error) => match error{
             jwt_hs256_parse::Error::InvalidChecksum => println!("Secret doesn't match"),
             _ => println!("Probably not a valid JWT: {}", error)
         }
     }
}

As illustrated in the examples above you can fill your claims with bloat for the client and have the back end only extract the useful bits when parsing.

Commit count: 7

cargo fmt