Clean up examples
This commit is contained in:
parent
b312bae4c7
commit
a03e09282d
4 changed files with 27 additions and 18 deletions
|
@ -3,8 +3,7 @@
|
|||
extern crate serde_derive;
|
||||
extern crate medallion;
|
||||
|
||||
use std::default::Default;
|
||||
use medallion::{Payload, Header, Token};
|
||||
use medallion::{Header, Payload, Token};
|
||||
|
||||
#[derive(Default, Serialize, Deserialize, PartialEq, Debug)]
|
||||
struct Custom {
|
||||
|
@ -24,16 +23,16 @@ fn new_token(user_id: &str, password: &str) -> Option<String> {
|
|||
return None;
|
||||
}
|
||||
|
||||
let header: Header<()> = Default::default();
|
||||
let header: Header = Header::default();
|
||||
let payload = Payload {
|
||||
// custom claims will be application specific, they may come from open standards such as
|
||||
// openidconnect where they may be referred to as registered claims
|
||||
claims: Some(Custom {
|
||||
user_id: user_id.into(),
|
||||
rhino: true,
|
||||
..Default::default()
|
||||
..Custom::default()
|
||||
}),
|
||||
..Default::default()
|
||||
..Payload::default()
|
||||
};
|
||||
let token = Token::new(header, payload);
|
||||
|
||||
|
|
|
@ -3,8 +3,7 @@
|
|||
extern crate serde_derive;
|
||||
extern crate medallion;
|
||||
|
||||
use std::default::Default;
|
||||
use medallion::{DefaultPayload, Header, DefaultToken};
|
||||
use medallion::{DefaultPayload, DefaultToken, Header};
|
||||
|
||||
#[derive(Default, Serialize, Deserialize, PartialEq, Debug)]
|
||||
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
|
||||
// token, as opposed to claims which are about the authenticated user or some output of
|
||||
// the authentication process
|
||||
headers: Some(Custom { typ: "JWT".into(), ..Default::default() }),
|
||||
headers: Some(Custom {
|
||||
typ: "JWT".into(),
|
||||
..Custom::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);
|
||||
|
||||
token.sign(b"secret_key").ok()
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
extern crate medallion;
|
||||
|
||||
use std::default::Default;
|
||||
use medallion::{Header, DefaultPayload, DefaultToken};
|
||||
use medallion::{DefaultPayload, DefaultToken, Header};
|
||||
|
||||
fn new_token(user_id: &str, password: &str) -> Option<String> {
|
||||
// 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
|
||||
let header: Header<()> = Default::default();
|
||||
let header: Header = Header::default();
|
||||
let payload = DefaultPayload {
|
||||
iss: Some("example.com".into()),
|
||||
sub: Some(user_id.into()),
|
||||
..Default::default()
|
||||
..DefaultPayload::default()
|
||||
};
|
||||
let token = DefaultToken::new(header, payload);
|
||||
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
extern crate medallion;
|
||||
extern crate openssl;
|
||||
|
||||
use std::default::Default;
|
||||
use medallion::{Algorithm, DefaultPayload, DefaultToken, Header};
|
||||
use openssl::rsa;
|
||||
use medallion::{Algorithm, Header, DefaultPayload, DefaultToken};
|
||||
|
||||
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
|
||||
|
@ -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
|
||||
let header: Header<()> = Header { alg: Algorithm::RS256, ..Default::default() };
|
||||
let header: Header = Header {
|
||||
alg: Algorithm::RS256,
|
||||
..Header::default()
|
||||
};
|
||||
let payload: DefaultPayload = DefaultPayload {
|
||||
iss: Some("example.com".into()),
|
||||
sub: Some(user_id.into()),
|
||||
..Default::default()
|
||||
..DefaultPayload::default()
|
||||
};
|
||||
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...
|
||||
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();
|
||||
|
||||
|
|
Loading…
Reference in a new issue