mirror of
https://git.verdigado.com/NB-Public/simple-wkd.git
synced 2024-10-30 03:05:51 +01:00
Refactor even more
This commit is contained in:
parent
7dc106ffd4
commit
9efbaefd92
4 changed files with 49 additions and 48 deletions
|
@ -9,15 +9,15 @@ use std::path::Path;
|
|||
|
||||
pub fn confirm_action(token: &str) -> Result<(), Error> {
|
||||
let pending_path = pending_path!().join(token);
|
||||
let data = if pending_path.exists() {
|
||||
let content = if pending_path.exists() {
|
||||
match fs::read_to_string(&pending_path) {
|
||||
Ok(data) => data,
|
||||
Ok(content) => content,
|
||||
Err(_) => return Err(Error::Inaccessible),
|
||||
}
|
||||
} else {
|
||||
return Err(Error::MissingPath);
|
||||
};
|
||||
let key = match serde_json::from_str::<Pending>(&data) {
|
||||
let key = match serde_json::from_str::<Pending>(&content) {
|
||||
Ok(key) => key,
|
||||
Err(_) => return Err(Error::ParseStored),
|
||||
};
|
||||
|
|
|
@ -27,7 +27,7 @@ struct Pem {
|
|||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Token {
|
||||
data: String,
|
||||
value: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
|
@ -63,7 +63,7 @@ async fn submit(pem: web::Form<Pem>) -> Result<String> {
|
|||
|
||||
#[get("/api/confirm/{data}")]
|
||||
async fn confirm(token: web::Path<Token>) -> Result<String> {
|
||||
confirm_action(&token.data)?;
|
||||
confirm_action(&token.value)?;
|
||||
Ok(String::from("Confirmation successfull!"))
|
||||
}
|
||||
|
||||
|
|
|
@ -48,24 +48,25 @@ impl Pending {
|
|||
}
|
||||
|
||||
fn store_pending(pending: &Pending, token: &str) -> Result<(), Error> {
|
||||
match serde_json::to_string(pending) {
|
||||
Ok(serialized) => match fs::write(pending_path!().join(token), serialized) {
|
||||
let serialized = match serde_json::to_string(pending) {
|
||||
Ok(serialized) => serialized,
|
||||
Err(_) => return Err(Error::SerializeData),
|
||||
};
|
||||
match fs::write(pending_path!().join(token), serialized) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(Error::Inaccessible),
|
||||
},
|
||||
Err(_) => Err(Error::SerializeData),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn store_pending_addition(pem: String, token: &str) -> Result<(), Error> {
|
||||
let data = Pending::build_add(pem);
|
||||
store_pending(&data, token)?;
|
||||
let pending = Pending::build_add(pem);
|
||||
store_pending(&pending, token)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn store_pending_deletion(email: String, token: &str) -> Result<(), Error> {
|
||||
let data = Pending::build_delete(email);
|
||||
store_pending(&data, token)?;
|
||||
let pending = Pending::build_delete(email);
|
||||
store_pending(&pending, token)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -73,23 +74,20 @@ pub fn clean_stale(max_age: i64) -> Result<(), Error> {
|
|||
for path in fs::read_dir(pending_path!()).unwrap().flatten() {
|
||||
let file_path = path.path();
|
||||
if file_path.exists() {
|
||||
match fs::read_to_string(&file_path) {
|
||||
Ok(data) => match serde_json::from_str::<Pending>(&data) {
|
||||
Ok(key) => {
|
||||
let content = match fs::read_to_string(&file_path) {
|
||||
Ok(content) => content,
|
||||
Err(_) => return Err(Error::Inaccessible),
|
||||
};
|
||||
let key = match serde_json::from_str::<Pending>(&content) {
|
||||
Ok(key) => key,
|
||||
Err(_) => return Err(Error::DeserializeData),
|
||||
};
|
||||
let now = Utc::now().timestamp();
|
||||
if now - key.timestamp() > max_age {
|
||||
if fs::remove_file(&file_path).is_err() {
|
||||
return Err(Error::Inaccessible);
|
||||
}
|
||||
println!(
|
||||
"Deleted {}, since it was stale",
|
||||
&file_path.to_str().unwrap()
|
||||
);
|
||||
}
|
||||
}
|
||||
Err(_) => return Err(Error::DeserializeData),
|
||||
},
|
||||
Err(_) => return Err(Error::Inaccessible),
|
||||
println!("Deleted {}, since it was stale", &file_path.display());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
35
src/utils.rs
35
src/utils.rs
|
@ -13,9 +13,9 @@ macro_rules! pending_path {
|
|||
};
|
||||
}
|
||||
|
||||
pub fn parse_pem(data: &str) -> Result<Cert, Error> {
|
||||
match sequoia_openpgp::Cert::from_bytes(data.as_bytes()) {
|
||||
Ok(data) => Ok(data),
|
||||
pub fn parse_pem(pemfile: &str) -> Result<Cert, Error> {
|
||||
match sequoia_openpgp::Cert::from_bytes(pemfile.as_bytes()) {
|
||||
Ok(cert) => Ok(cert),
|
||||
Err(_) => Err(Error::ParseCert),
|
||||
}
|
||||
}
|
||||
|
@ -26,24 +26,27 @@ pub fn gen_random_token() -> String {
|
|||
}
|
||||
|
||||
pub fn get_email_from_cert(cert: &Cert) -> Result<String, Error> {
|
||||
match cert.userids().next() {
|
||||
Some(data) => match data.email() {
|
||||
Ok(data) => match data {
|
||||
Some(data) => Ok(data),
|
||||
let userid_opt = match cert.userids().next() {
|
||||
Some(userid_opt) => userid_opt,
|
||||
None => return Err(Error::ParseCert),
|
||||
};
|
||||
let email_opt = match userid_opt.email() {
|
||||
Ok(email_opt) => email_opt,
|
||||
Err(_) => return Err(Error::ParseMail),
|
||||
};
|
||||
match email_opt {
|
||||
Some(email) => Ok(email),
|
||||
None => Err(Error::MissingMail),
|
||||
},
|
||||
Err(_) => Err(Error::ParseMail),
|
||||
},
|
||||
None => Err(Error::ParseCert),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_user_file_path(email: &str) -> Result<PathBuf, Error> {
|
||||
match Url::from(email) {
|
||||
Ok(data) => match data.to_file_path(VARIANT) {
|
||||
Ok(data) => Ok(data),
|
||||
Err(_) => Err(Error::ParseMail),
|
||||
},
|
||||
let wkd_url = match Url::from(email) {
|
||||
Ok(wkd_url) => wkd_url,
|
||||
Err(_) => return Err(Error::ParseMail),
|
||||
};
|
||||
match wkd_url.to_file_path(VARIANT) {
|
||||
Ok(path) => Ok(path),
|
||||
Err(_) => Err(Error::ParseMail),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue