Update to serde 1.0 (#4)
* Try updating service to 1.0 * Strip bounds from structs * Clean bounds from Default impl * Remove pem files, clean up some documentation * Add badges, categories
This commit is contained in:
parent
a265a703d6
commit
42824a133e
11 changed files with 65 additions and 92 deletions
|
@ -16,13 +16,18 @@ struct Custom {
|
|||
}
|
||||
|
||||
fn new_token(user_id: &str, password: &str) -> Option<String> {
|
||||
// Dummy auth
|
||||
// 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
|
||||
if password != "password" {
|
||||
return None;
|
||||
}
|
||||
|
||||
let header: Header<()> = Default::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,
|
||||
|
|
|
@ -15,12 +15,18 @@ struct Custom {
|
|||
}
|
||||
|
||||
fn new_token(sub: &str, password: &str) -> Option<String> {
|
||||
// Dummy auth
|
||||
// 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
|
||||
if password != "password" {
|
||||
return None;
|
||||
}
|
||||
|
||||
let header = Header {
|
||||
// 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() }),
|
||||
..Default::default()
|
||||
};
|
||||
|
|
|
@ -4,7 +4,10 @@ use std::default::Default;
|
|||
use medallion::{Header, DefaultPayload, DefaultToken};
|
||||
|
||||
fn new_token(user_id: &str, password: &str) -> Option<String> {
|
||||
// Dummy auth
|
||||
// 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
|
||||
if password != "password" {
|
||||
return None;
|
||||
}
|
||||
|
@ -24,6 +27,7 @@ fn new_token(user_id: &str, password: &str) -> Option<String> {
|
|||
fn login(token: &str) -> Option<String> {
|
||||
let token: DefaultToken<()> = DefaultToken::parse(token).unwrap();
|
||||
|
||||
// the key for HMAC is some secret known to trusted/trusting parties
|
||||
if token.verify(b"secret_key").unwrap() {
|
||||
token.payload.sub
|
||||
} else {
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEpAIBAAKCAQEAqfGTaNo/PBWs3frGcNpboXCh2/NV2edFwkXMnEjGVw9tyLEu
|
||||
62Tv/LD+hHGQ/EtRfy+iueamtqwRnpeQDnThdYgc7a1nfava7j7ecUCJNnjCJSBT
|
||||
8qWX5U7lsFrxm2NEhGIMgO40wQRxFylb4izxrloBvFKr+XtfgIY5CaUTaKdvZ2mo
|
||||
OXgzZ5kC8Qr7SASpEJiMRM432N9QDTTg3kv23htec+FzSucEdJHrkIxr2LjHAmtm
|
||||
XcT8vRikpN2GThbRUwjLEW3YsWJSTn9GGzF2XZ2rRhdDMviPPjjYPH7Sau2ROnk8
|
||||
sgGMtEzZNS7i/Lm5qsAoPw85mnnnTvJ6ArId2QIDAQABAoIBADPz4S+lwL0alz1J
|
||||
Q88OQgLpjuHR0wYJeL76XaHNcaz9z38SA5j8w40JgtV0bnFiiSiLpICWbZLcqYpF
|
||||
JUn2G1K16LoUT9YQap5448HVi9z2L8vvxRoh23zDkN5H/yKUx0Z9PvtPVxtGw1fk
|
||||
Ue2j9cJqS6uJzn83YyvEXL2BFJziS1mQ7W6raD9fWTBFOlcNwR2+djlECEvxoMcf
|
||||
SQqYa5oBUFWFmaJdCjOxqQxNRgWFxPEVlAz62PUoC1WUoP3uKAg5mvKhH/PjNSGl
|
||||
9K6JhpVgvlBfVUu7dWGAmaUZOLu8l6EUO9MWXDfk10qFKo1bZT9orrARHESPzL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OCAQ8AMIIBCgKCAQEAqfGTaNo/PBWs3frGcNpb
|
||||
oXCh2/NV2edFwkXMnEjGVw9tyLEu62Tv/LD+hHGQ/EtRfy+iueamtqwRnpeQDnTh
|
||||
dYgc7a1nfava7j7ecUCJNnjCJSBT8qWX5U7lsFrxm2NEhGIMgO40wQRxFylb4izx
|
||||
rloBvFKr+XtfgIY5CaUTaKdvZ2moOXgzZ5kC8Qr7SASpEJiMRM432N9QDTTg3kv2
|
||||
3htec+FzSucEdJHrkIxr2LjHAmtmXcT8vRikpN2GThbRUwjLEW3YsWJSTn9GGzF2
|
||||
XZ2rRhdDMviPPjjYPH7Sau2ROnk8sgGMtEzZNS7i/Lm5qsAoPw85mnnnTvJ6ArId
|
||||
2QIDAQAB
|
||||
-----END PUBLIC KEY-----
|
|
@ -1,24 +1,20 @@
|
|||
extern crate medallion;
|
||||
extern crate openssl;
|
||||
|
||||
use std::default::Default;
|
||||
use std::fs::File;
|
||||
use std::io::{Error, Read};
|
||||
use openssl::rsa;
|
||||
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
|
||||
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
|
||||
// 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
|
||||
if password != "password" {
|
||||
return None;
|
||||
}
|
||||
|
||||
// can satisfy Header's generic parameter with an empty type
|
||||
// can satisfy Header's type parameter with an empty tuple
|
||||
let header: Header<()> = Header { alg: Algorithm::RS256, ..Default::default() };
|
||||
let payload: DefaultPayload = DefaultPayload {
|
||||
iss: Some("example.com".into()),
|
||||
|
@ -27,15 +23,13 @@ fn new_token(user_id: &str, password: &str) -> Option<String> {
|
|||
};
|
||||
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()
|
||||
token.sign(private_key).ok()
|
||||
}
|
||||
|
||||
fn login(token: &str) -> Option<String> {
|
||||
fn login(public_key: &[u8], 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() {
|
||||
if token.verify(public_key).unwrap() {
|
||||
token.payload.sub
|
||||
} else {
|
||||
None
|
||||
|
@ -43,9 +37,12 @@ fn login(token: &str) -> Option<String> {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
let token = new_token("Random User", "password").unwrap();
|
||||
// alternatively can read .pem files from fs or fetch from a server or...
|
||||
let keypair = rsa::Rsa::generate(2048).unwrap();
|
||||
|
||||
let logged_in_user = login(&*token).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();
|
||||
|
||||
assert_eq!(logged_in_user, "Random User");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue