Crate simple_jwt [] [src]

Introduction

A very simple crate to deal with json web token, this lib use the rust-openssl, so you may want to check the rust-openssl to find the set-up of openssl runtime lib.

Support Algorithm

Example

use simple_jwt::{encode, decode, Claim, Algorithm};

let mut claim = Claim::default();
claim.set_iss("some iss");
claim.set_payload_field("stringhh", 12);
let result = encode(&claim, "secret", Algorithm::HS256).unwrap();
println!("hashed result is {}", result);
let new_claim = decode(&result, "secret").unwrap();
assert_eq!(claim, new_claim);

Or simple use your custom struct

#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate simple_jwt;

use serde::{Serialize, Deserialize};
use simple_jwt::{encode, decode, Claim, Algorithm};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct MyStruct {
    field_u32: u32,
    field_str: String
}

fn main() {
    let myStruct = MyStruct {field_str: String::from("hello"), field_u32: 32};

    let result = encode(&myStruct, "secret", Algorithm::HS256).unwrap();
    println!("hashed result is {}", result);
    let newStruct = decode(&result, "secret").unwrap();
    assert_eq!(myStruct, newStruct);
}

The test in lib.rs contains more example

Structs

Claim
Error

The Error type.

Header

Enums

Algorithm
ErrorKind

The kind of an error.

Traits

JWTStringConvertable

trait that can be convert to/from base64 string impl for serde::Serialize+serde::Deserialize are already defined

ResultExt

Additional methods for Result, for easy interaction with this crate.

Functions

decode

decode a jwt string using algorithm in the jwt header field

encode

encode a Claim to jwt string, if you are using RS256/384/512, secret should be your private key

Type Definitions

Result

Convenient wrapper around std::Result.