jwt_rs

Crates.iojwt_rs
lib.rsjwt_rs
version0.1.0
sourcesrc
created_at2022-03-15 00:42:27.54681
updated_at2022-03-15 00:42:27.54681
descriptionA Json Web Token (JWT) implementation for Rust
homepagehttps://github.com/xavierhamel/jwt-rs
repositoryhttps://github.com/xavierhamel/jwt-rs
max_upload_size
id550274
size18,429
Xavier Hamel (xavierhamel)

documentation

README

JWT.rs

A simple Json Web Token crate. The crate let's you create, sign, verify and extract data from JWT tokens. Data is serialized with serde and serde_json.

Examples

Verification

Extract the payload from a JWT token if the token is valid.

#[derive(serde::Serialize, serde::Deserialize, PartialEq)]
struct TestPayload {
    is_admin: bool,
    name: String,
    age: u8,
}

const SECRET: &'static str = "This is a very secret secret";

let token_str = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc19hZG1pbiI6dHJ1ZSwibmFtZSI6IkpvaG4gRG9lIiwiYWdlIjoxOH0.0mV5XVAmarscyZEwl8PoX4vqVn_JCZSVJRsgnSJTo94";
let token = Token::from_str(token_str)?;
// payload is `Ok(Some(<payload>))`
let payload = token.get_if_valid::<TestPayload>(SECRET);

Signing

It's also possible to create and sign a new token:

const SECRET: &'static str = "This is a very secret secret";

#[derive(serde::Serialize, serde::Deserialize)]
struct TestPayload {
    is_admin: bool,
    name: String,
    age: u8,
}
let payload = TestPayload {
    is_admin: true,
    name: String::from("John Doe"),
    age: 18
};
let token = Token::try_new(Algorithm::HS256, payload, SECRET).unwrap();
println!("{}", token); // Convert the token to a string.
Commit count: 6

cargo fmt