2017-02-17 17:59:55 +00:00
|
|
|
Medallion
|
|
|
|
=========
|
2018-11-16 22:29:24 +00:00
|
|
|
[![Build Status](https://travis-ci.org/cmdln/medallion.svg?branch=master)](https://travis-ci.org/cmdln/medallion)
|
2017-02-17 16:29:57 +00:00
|
|
|
[![Crates.io Status](http://meritbadge.herokuapp.com/medallion)](https://crates.io/crates/medallion)
|
|
|
|
[![Documentation](https://docs.rs/medallion/badge.svg)](https://docs.rs/medallion)
|
2020-06-23 16:25:19 +00:00
|
|
|
[![dependency status](https://deps.rs/crate/medallion/2.4.0/status.svg)](https://deps.rs/crate/medallion/2.4.0)
|
2018-11-16 22:29:24 +00:00
|
|
|
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/cmdln/medallion/master/LICENSE)
|
2017-02-13 23:40:07 +00:00
|
|
|
|
|
|
|
A JWT library for rust using serde, serde_json and openssl.
|
|
|
|
|
2017-02-15 20:42:06 +00:00
|
|
|
## Documentation
|
|
|
|
|
2018-11-16 22:29:24 +00:00
|
|
|
- [Documentation](https://cmdln.github.io/medallion/)
|
2017-02-15 20:42:06 +00:00
|
|
|
|
2017-02-13 23:40:07 +00:00
|
|
|
## Usage
|
|
|
|
|
2017-03-07 19:03:24 +00:00
|
|
|
The library provides a `Token` type that wraps headers and claims.
|
|
|
|
|
|
|
|
```rust
|
|
|
|
extern crate medallion;
|
|
|
|
|
|
|
|
use medallion::{
|
|
|
|
Header,
|
|
|
|
DefaultPayload,
|
|
|
|
Token,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// will default to Algorithm::HS256
|
2018-11-18 15:54:02 +00:00
|
|
|
let header: Header = Header::default();
|
2017-03-07 19:03:24 +00:00
|
|
|
let payload = DefaultPayload {
|
|
|
|
iss: Some("example.com".into()),
|
|
|
|
sub: Some("Random User".into()),
|
2018-11-18 15:54:02 +00:00
|
|
|
..DefaultPayload::default()
|
2017-03-07 19:03:24 +00:00
|
|
|
};
|
|
|
|
let token = Token::new(header, payload);
|
|
|
|
|
|
|
|
token.sign(b"secret_key").unwrap();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-11-18 15:54:02 +00:00
|
|
|
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)`.
|
2017-03-07 19:03:24 +00:00
|
|
|
|
|
|
|
```rust
|
|
|
|
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",)
|
|
|
|
}
|
2018-11-18 15:54:02 +00:00
|
|
|
..Header::default()
|
2017-03-07 19:03:24 +00:00
|
|
|
}
|
|
|
|
let payload = DefaultPayload {
|
|
|
|
iss: Some("example.com".into()),
|
|
|
|
sub: Some("Random User".into()),
|
2018-11-18 15:54:02 +00:00
|
|
|
..DefaultPayload::default()
|
2017-03-07 19:03:24 +00:00
|
|
|
};
|
|
|
|
let token = Token::new(header, payload);
|
|
|
|
|
|
|
|
token.sign(b"secret_key").unwrap();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-11-18 15:54:02 +00:00
|
|
|
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.
|
2017-03-07 19:03:24 +00:00
|
|
|
|
|
|
|
```rust
|
|
|
|
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",)
|
|
|
|
}
|
2018-11-18 15:54:02 +00:00
|
|
|
..Header::default()
|
2017-03-07 19:03:24 +00:00
|
|
|
}
|
|
|
|
let payload = DefaultPayload {
|
|
|
|
iss: Some("example.com".into()),
|
|
|
|
sub: Some("Random User".into()),
|
|
|
|
claims: CustomClaims {
|
|
|
|
user_id: 1234,
|
|
|
|
email: "random@example.com",
|
|
|
|
}
|
2018-11-18 15:54:02 +00:00
|
|
|
..DefaultPayload::default()
|
2017-03-07 19:03:24 +00:00
|
|
|
};
|
|
|
|
let token = Token::new(header, payload);
|
|
|
|
|
|
|
|
token.sign(b"secret_key").unwrap();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
See the examples for more detailed usage.
|
2017-02-13 23:40:07 +00:00
|
|
|
|
|
|
|
This library was originally forked from @mikkyang's rust-jwt.
|