Clean up examples

This commit is contained in:
Thomas Gideon 2018-11-18 10:46:19 -05:00
parent b312bae4c7
commit a03e09282d
4 changed files with 27 additions and 18 deletions

View file

@ -3,8 +3,7 @@
extern crate serde_derive; extern crate serde_derive;
extern crate medallion; extern crate medallion;
use std::default::Default; use medallion::{Header, Payload, Token};
use medallion::{Payload, Header, Token};
#[derive(Default, Serialize, Deserialize, PartialEq, Debug)] #[derive(Default, Serialize, Deserialize, PartialEq, Debug)]
struct Custom { struct Custom {
@ -24,16 +23,16 @@ fn new_token(user_id: &str, password: &str) -> Option<String> {
return None; return None;
} }
let header: Header<()> = Default::default(); let header: Header = Header::default();
let payload = Payload { let payload = Payload {
// custom claims will be application specific, they may come from open standards such as // custom claims will be application specific, they may come from open standards such as
// openidconnect where they may be referred to as registered claims // openidconnect where they may be referred to as registered claims
claims: Some(Custom { claims: Some(Custom {
user_id: user_id.into(), user_id: user_id.into(),
rhino: true, rhino: true,
..Default::default() ..Custom::default()
}), }),
..Default::default() ..Payload::default()
}; };
let token = Token::new(header, payload); let token = Token::new(header, payload);

View file

@ -3,8 +3,7 @@
extern crate serde_derive; extern crate serde_derive;
extern crate medallion; extern crate medallion;
use std::default::Default; use medallion::{DefaultPayload, DefaultToken, Header};
use medallion::{DefaultPayload, Header, DefaultToken};
#[derive(Default, Serialize, Deserialize, PartialEq, Debug)] #[derive(Default, Serialize, Deserialize, PartialEq, Debug)]
struct Custom { struct Custom {
@ -27,10 +26,16 @@ fn new_token(sub: &str, password: &str) -> Option<String> {
// customer headers generally are about the token itself, like here describing the type of // customer headers generally are about the token itself, like here describing the type of
// token, as opposed to claims which are about the authenticated user or some output of // token, as opposed to claims which are about the authenticated user or some output of
// the authentication process // the authentication process
headers: Some(Custom { typ: "JWT".into(), ..Default::default() }), headers: Some(Custom {
typ: "JWT".into(),
..Custom::default()
}),
..Default::default() ..Default::default()
}; };
let payload = DefaultPayload { sub: Some(sub.into()), ..Default::default() }; let payload = DefaultPayload {
sub: Some(sub.into()),
..DefaultPayload::default()
};
let token = DefaultToken::new(header, payload); let token = DefaultToken::new(header, payload);
token.sign(b"secret_key").ok() token.sign(b"secret_key").ok()

View file

@ -1,7 +1,6 @@
extern crate medallion; extern crate medallion;
use std::default::Default; use medallion::{DefaultPayload, DefaultToken, Header};
use medallion::{Header, DefaultPayload, DefaultToken};
fn new_token(user_id: &str, password: &str) -> Option<String> { fn new_token(user_id: &str, password: &str) -> Option<String> {
// dummy auth, in a real application using something like openidconnect, this would be some // dummy auth, in a real application using something like openidconnect, this would be some
@ -13,11 +12,11 @@ fn new_token(user_id: &str, password: &str) -> Option<String> {
} }
// can satisfy Header's generic parameter with an empty type // can satisfy Header's generic parameter with an empty type
let header: Header<()> = Default::default(); let header: Header = Header::default();
let payload = DefaultPayload { let payload = DefaultPayload {
iss: Some("example.com".into()), iss: Some("example.com".into()),
sub: Some(user_id.into()), sub: Some(user_id.into()),
..Default::default() ..DefaultPayload::default()
}; };
let token = DefaultToken::new(header, payload); let token = DefaultToken::new(header, payload);

View file

@ -1,9 +1,8 @@
extern crate medallion; extern crate medallion;
extern crate openssl; extern crate openssl;
use std::default::Default; use medallion::{Algorithm, DefaultPayload, DefaultToken, Header};
use openssl::rsa; use openssl::rsa;
use medallion::{Algorithm, Header, DefaultPayload, DefaultToken};
fn new_token(private_key: &[u8], user_id: &str, password: &str) -> Option<String> { fn new_token(private_key: &[u8], user_id: &str, password: &str) -> Option<String> {
// dummy auth, in a real application using something like openidconnect, this would be some // dummy auth, in a real application using something like openidconnect, this would be some
@ -15,11 +14,14 @@ fn new_token(private_key: &[u8], user_id: &str, password: &str) -> Option<String
} }
// can satisfy Header's type parameter with an empty tuple // can satisfy Header's type parameter with an empty tuple
let header: Header<()> = Header { alg: Algorithm::RS256, ..Default::default() }; let header: Header = Header {
alg: Algorithm::RS256,
..Header::default()
};
let payload: DefaultPayload = DefaultPayload { let payload: DefaultPayload = DefaultPayload {
iss: Some("example.com".into()), iss: Some("example.com".into()),
sub: Some(user_id.into()), sub: Some(user_id.into()),
..Default::default() ..DefaultPayload::default()
}; };
let token = DefaultToken::new(header, payload); let token = DefaultToken::new(header, payload);
@ -40,7 +42,11 @@ fn main() {
// alternatively can read .pem files from fs or fetch from a server or... // alternatively can read .pem files from fs or fetch from a server or...
let keypair = rsa::Rsa::generate(2048).unwrap(); let keypair = rsa::Rsa::generate(2048).unwrap();
let token = new_token(&keypair.private_key_to_pem().unwrap(), "Random User", "password").unwrap(); let token = new_token(
&keypair.private_key_to_pem().unwrap(),
"Random User",
"password",
).unwrap();
let logged_in_user = login(&keypair.public_key_to_pem().unwrap(), &*token).unwrap(); let logged_in_user = login(&keypair.public_key_to_pem().unwrap(), &*token).unwrap();