2017-02-14 15:51:39 +00:00
|
|
|
// need this for custom derivation
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
extern crate medallion;
|
2017-02-13 23:40:07 +00:00
|
|
|
|
|
|
|
use std::default::Default;
|
2017-03-07 19:03:24 +00:00
|
|
|
use medallion::{Payload, Header, Token};
|
2017-02-13 23:40:07 +00:00
|
|
|
|
2017-03-07 19:03:24 +00:00
|
|
|
#[derive(Default, Serialize, Deserialize, PartialEq, Debug)]
|
2017-02-13 23:40:07 +00:00
|
|
|
struct Custom {
|
2017-03-07 19:03:24 +00:00
|
|
|
user_id: String,
|
|
|
|
// useful if you want a None to not appear in the serialized JSON
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
email: Option<String>,
|
2017-02-13 23:40:07 +00:00
|
|
|
rhino: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_token(user_id: &str, password: &str) -> Option<String> {
|
2017-08-10 13:39:35 +00:00
|
|
|
// dummy auth, in a real application using something like openidconnect, this would be some
|
|
|
|
// specific authentication scheme that takes place first then the JWT is generated as part of
|
|
|
|
// sucess and signed with the provider's private key so other services can validate trust for
|
|
|
|
// the claims in the token
|
2017-02-13 23:40:07 +00:00
|
|
|
if password != "password" {
|
2017-03-07 19:03:24 +00:00
|
|
|
return None;
|
2017-02-13 23:40:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 19:03:24 +00:00
|
|
|
let header: Header<()> = Default::default();
|
|
|
|
let payload = Payload {
|
2017-08-10 13:39:35 +00:00
|
|
|
// custom claims will be application specific, they may come from open standards such as
|
|
|
|
// openidconnect where they may be referred to as registered claims
|
2017-03-07 19:03:24 +00:00
|
|
|
claims: Some(Custom {
|
|
|
|
user_id: user_id.into(),
|
|
|
|
rhino: true,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2017-02-13 23:40:07 +00:00
|
|
|
..Default::default()
|
|
|
|
};
|
2017-03-07 19:03:24 +00:00
|
|
|
let token = Token::new(header, payload);
|
2017-02-13 23:40:07 +00:00
|
|
|
|
2017-03-07 19:03:24 +00:00
|
|
|
token.sign(b"secret_key").ok()
|
2017-02-13 23:40:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn login(token: &str) -> Option<String> {
|
2017-03-07 19:03:24 +00:00
|
|
|
let token = Token::<(), Custom>::parse(token).unwrap();
|
2017-02-13 23:40:07 +00:00
|
|
|
|
2017-02-17 16:53:12 +00:00
|
|
|
if token.verify(b"secret_key").unwrap() {
|
2017-03-07 19:03:24 +00:00
|
|
|
Some(token.payload.claims.unwrap().user_id)
|
2017-02-13 23:40:07 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2017-02-14 18:45:24 +00:00
|
|
|
let token = new_token("Random User", "password").unwrap();
|
2017-02-13 23:40:07 +00:00
|
|
|
|
|
|
|
let logged_in_user = login(&*token).unwrap();
|
|
|
|
|
2017-02-14 18:45:24 +00:00
|
|
|
assert_eq!(logged_in_user, "Random User");
|
2017-02-13 23:40:07 +00:00
|
|
|
}
|