Created
August 22, 2019 07:37
-
-
Save rust-play/b246e9281c0e6b70500cb1ccf05dc300 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub type Payload<R> = (Role<R>, Target); | |
#[derive(Debug, Clone, Serialize, Deserialize)] | |
struct SGSecret(String); | |
impl Default for SGSecret { | |
fn default() -> Self{ Self(String::default()) } | |
} | |
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] | |
pub enum Role<R> { | |
User, | |
CustomRole(R), | |
} | |
impl<R> Default for Role<R> { | |
fn default() -> Self{ Role::User } | |
} | |
#[derive(Debug, Serialize, Deserialize)] | |
pub enum Target { | |
Global, | |
CustomTarget(String), | |
} | |
#[derive(Debug, Serialize, Deserialize)] | |
pub struct AuthPayload<R> { | |
role: Role<R>, | |
random_key: String, | |
} | |
#[derive(Debug, Serialize, Deserialize)] | |
pub struct AuthEngine<R> { | |
bearer: SGSecret, | |
payload: AuthPayload<R>, | |
} | |
impl<R> for AuthEngine<R> { | |
pub fn rm<'d>(self, raw_key: String) -> Option<Payload> where R: serde::Deserialize<'d> { | |
let dual = raw_key.split(":::").collect::<Vec<&str>>(); | |
let key = bincode::serialize(dual[0])?; | |
let check_key = db.remove(key)?; // Remove key from DB | |
if let Some(binary) = check_key { | |
let data = bincode::deserialize::<AuthPayload<R>>(&binary)?; | |
Some((data.role, data.target, data.random_key)) | |
}else { | |
Ok(None) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment