#jwt #token #web #json #rsa

medallion

JWT library for rust using serde, serde_json and openssl

17 stable releases

new 2.4.2 Dec 3, 2021
2.4.1 Apr 20, 2021
2.4.0 Jun 22, 2020
2.3.1 Nov 18, 2018
1.1.1 Feb 20, 2017

#98 in Cryptography

Download history 22/week @ 2021-08-15 13/week @ 2021-08-22 1/week @ 2021-08-29 10/week @ 2021-09-05 9/week @ 2021-09-12 8/week @ 2021-09-19 157/week @ 2021-09-26 29/week @ 2021-10-03 27/week @ 2021-10-10 45/week @ 2021-10-17 12/week @ 2021-10-24 18/week @ 2021-10-31 17/week @ 2021-11-07 5/week @ 2021-11-14 6/week @ 2021-11-21 34/week @ 2021-11-28

63 downloads per month
Used in gauthz

MIT license

29KB
598 lines

Medallion

Documentation dependency status

A JWT library for rust using serde, serde_json and openssl.

Usage

The library provides a Token type that wraps headers and claims.

extern crate medallion;

use medallion::{
    Header,
    DefaultPayload,
    Token,
};

fn main() {
    // will default to Algorithm::HS256
    let header: Header = Header::default();
    let payload = DefaultPayload {
        iss: Some("example.com".into()),
        sub: Some("Random User".into()),
        ..DefaultPayload::default()
    };
    let token = Token::new(header, payload);

    token.sign(b"secret_key").unwrap();
}

The Header struct contains all of the headers of the JWT. It requires that a supported algorithm (HS256, HS384, HS512, RS256, RS384, and RS512) be specified. It requires a type for additional header fields. That type must implement serde's Serialize and Deserialize as well as PartialEq. These traits can usually be derived, e.g. #[derive(PartialEq, Serialize, Deserialize).

extern crate medallion;

use serde::{Serialize, Deserialize};

use medallion::{Header, DefaultPayload, Token};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct CustomHeaders {
    kid: String,
    typ: String,
}

fn main() {
    let header = Header {
        headers: CustomHeaders {
            kid: "0001",)
            typ: "JWT",)
        }
        ..Header::default()
    }
    let payload = DefaultPayload {
        iss: Some("example.com".into()),
        sub: Some("Random User".into()),
        ..DefaultPayload::default()
    };
    let token = Token::new(header, payload);

    token.sign(b"secret_key").unwrap();
}

The Payload struct contains all of the claims of the JWT. It provides the set of registered, public claims. Additional claims can be added by constructing the Payload with a generically typed value. That value's type must implement serde's Serialize and Deserialize as well as PartialEq. These traits can usually be derived, e.g. #[derive(PartialEq, Serialize, Deserialize). A convenience type, DefaultPayload, is provided that binds the generic parameter of Payload to an empty tuple type.

extern crate medallion;

use serde::{Serialize, Deserialize};

use medallion::{Header, DefaultPayload, Token};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct CustomHeaders {
    kid: String,
    typ: String,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct CustomClaims {
    user_id: u64,
    email: String,
}

fn main() {
    let header = Header {
        headers: CustomHeaders {
            kid: "0001",)
            typ: "JWT",)
        }
        ..Header::default()
    }
    let payload = DefaultPayload {
        iss: Some("example.com".into()),
        sub: Some("Random User".into()),
        claims: CustomClaims {
            user_id: 1234,
            email: "random@example.com",
        }
        ..DefaultPayload::default()
    };
    let token = Token::new(header, payload);

    token.sign(b"secret_key").unwrap();
}

See the examples for more detailed usage.

This library was originally forked from @mikkyang's rust-jwt.

Dependencies

~3–4MB
~85K SLoC