medallion/examples/rs256.rs
Thomas Gideon 3c9fd6b13b Refactor header claims (#1)
Simplify customization, bump to 2.0.0.
2017-03-07 14:03:24 -05:00

51 lines
1.5 KiB
Rust

extern crate medallion;
use std::default::Default;
use std::fs::File;
use std::io::{Error, Read};
use medallion::{Algorithm, Header, DefaultPayload, DefaultToken};
fn load_pem(keypath: &str) -> Result<String, Error> {
let mut key_file = File::open(keypath)?;
let mut key = String::new();
key_file.read_to_string(&mut key)?;
Ok(key)
}
fn new_token(user_id: &str, password: &str) -> Option<String> {
// Dummy auth
if password != "password" {
return None;
}
// can satisfy Header's generic parameter with an empty type
let header: Header<()> = Header { alg: Algorithm::RS256, ..Default::default() };
let payload: DefaultPayload = DefaultPayload {
iss: Some("example.com".into()),
sub: Some(user_id.into()),
..Default::default()
};
let token = DefaultToken::new(header, payload);
// this key was generated explicitly for these examples and is not used anywhere else
token.sign(load_pem("./privateKey.pem").unwrap().as_bytes()).ok()
}
fn login(token: &str) -> Option<String> {
let token: DefaultToken<()> = DefaultToken::parse(token).unwrap();
// this key was generated explicitly for these examples and is not used anywhere else
if token.verify(load_pem("./publicKey.pub").unwrap().as_bytes()).unwrap() {
token.payload.sub
} else {
None
}
}
fn main() {
let token = new_token("Random User", "password").unwrap();
let logged_in_user = login(&*token).unwrap();
assert_eq!(logged_in_user, "Random User");
}