mirror of
https://git.verdigado.com/NB-Public/simple-wkd.git
synced 2024-10-30 05:05:52 +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> {
|
pub fn confirm_action(token: &str) -> Result<(), Error> {
|
||||||
let pending_path = pending_path!().join(token);
|
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) {
|
match fs::read_to_string(&pending_path) {
|
||||||
Ok(data) => data,
|
Ok(content) => content,
|
||||||
Err(_) => return Err(Error::Inaccessible),
|
Err(_) => return Err(Error::Inaccessible),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::MissingPath);
|
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,
|
Ok(key) => key,
|
||||||
Err(_) => return Err(Error::ParseStored),
|
Err(_) => return Err(Error::ParseStored),
|
||||||
};
|
};
|
||||||
|
|
|
@ -27,7 +27,7 @@ struct Pem {
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct Token {
|
struct Token {
|
||||||
data: String,
|
value: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
|
@ -63,7 +63,7 @@ async fn submit(pem: web::Form<Pem>) -> Result<String> {
|
||||||
|
|
||||||
#[get("/api/confirm/{data}")]
|
#[get("/api/confirm/{data}")]
|
||||||
async fn confirm(token: web::Path<Token>) -> Result<String> {
|
async fn confirm(token: web::Path<Token>) -> Result<String> {
|
||||||
confirm_action(&token.data)?;
|
confirm_action(&token.value)?;
|
||||||
Ok(String::from("Confirmation successfull!"))
|
Ok(String::from("Confirmation successfull!"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -48,24 +48,25 @@ impl Pending {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn store_pending(pending: &Pending, token: &str) -> Result<(), Error> {
|
fn store_pending(pending: &Pending, token: &str) -> Result<(), Error> {
|
||||||
match serde_json::to_string(pending) {
|
let serialized = match serde_json::to_string(pending) {
|
||||||
Ok(serialized) => match fs::write(pending_path!().join(token), serialized) {
|
Ok(serialized) => serialized,
|
||||||
Ok(_) => Ok(()),
|
Err(_) => return Err(Error::SerializeData),
|
||||||
Err(_) => Err(Error::Inaccessible),
|
};
|
||||||
},
|
match fs::write(pending_path!().join(token), serialized) {
|
||||||
Err(_) => Err(Error::SerializeData),
|
Ok(_) => Ok(()),
|
||||||
|
Err(_) => Err(Error::Inaccessible),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn store_pending_addition(pem: String, token: &str) -> Result<(), Error> {
|
pub fn store_pending_addition(pem: String, token: &str) -> Result<(), Error> {
|
||||||
let data = Pending::build_add(pem);
|
let pending = Pending::build_add(pem);
|
||||||
store_pending(&data, token)?;
|
store_pending(&pending, token)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn store_pending_deletion(email: String, token: &str) -> Result<(), Error> {
|
pub fn store_pending_deletion(email: String, token: &str) -> Result<(), Error> {
|
||||||
let data = Pending::build_delete(email);
|
let pending = Pending::build_delete(email);
|
||||||
store_pending(&data, token)?;
|
store_pending(&pending, token)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,23 +74,20 @@ pub fn clean_stale(max_age: i64) -> Result<(), Error> {
|
||||||
for path in fs::read_dir(pending_path!()).unwrap().flatten() {
|
for path in fs::read_dir(pending_path!()).unwrap().flatten() {
|
||||||
let file_path = path.path();
|
let file_path = path.path();
|
||||||
if file_path.exists() {
|
if file_path.exists() {
|
||||||
match fs::read_to_string(&file_path) {
|
let content = match fs::read_to_string(&file_path) {
|
||||||
Ok(data) => match serde_json::from_str::<Pending>(&data) {
|
Ok(content) => content,
|
||||||
Ok(key) => {
|
|
||||||
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),
|
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.display());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
37
src/utils.rs
37
src/utils.rs
|
@ -13,9 +13,9 @@ macro_rules! pending_path {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_pem(data: &str) -> Result<Cert, Error> {
|
pub fn parse_pem(pemfile: &str) -> Result<Cert, Error> {
|
||||||
match sequoia_openpgp::Cert::from_bytes(data.as_bytes()) {
|
match sequoia_openpgp::Cert::from_bytes(pemfile.as_bytes()) {
|
||||||
Ok(data) => Ok(data),
|
Ok(cert) => Ok(cert),
|
||||||
Err(_) => Err(Error::ParseCert),
|
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> {
|
pub fn get_email_from_cert(cert: &Cert) -> Result<String, Error> {
|
||||||
match cert.userids().next() {
|
let userid_opt = match cert.userids().next() {
|
||||||
Some(data) => match data.email() {
|
Some(userid_opt) => userid_opt,
|
||||||
Ok(data) => match data {
|
None => return Err(Error::ParseCert),
|
||||||
Some(data) => Ok(data),
|
};
|
||||||
None => Err(Error::MissingMail),
|
let email_opt = match userid_opt.email() {
|
||||||
},
|
Ok(email_opt) => email_opt,
|
||||||
Err(_) => Err(Error::ParseMail),
|
Err(_) => return Err(Error::ParseMail),
|
||||||
},
|
};
|
||||||
None => Err(Error::ParseCert),
|
match email_opt {
|
||||||
|
Some(email) => Ok(email),
|
||||||
|
None => Err(Error::MissingMail),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_user_file_path(email: &str) -> Result<PathBuf, Error> {
|
pub fn get_user_file_path(email: &str) -> Result<PathBuf, Error> {
|
||||||
match Url::from(email) {
|
let wkd_url = match Url::from(email) {
|
||||||
Ok(data) => match data.to_file_path(VARIANT) {
|
Ok(wkd_url) => wkd_url,
|
||||||
Ok(data) => Ok(data),
|
Err(_) => return Err(Error::ParseMail),
|
||||||
Err(_) => Err(Error::ParseMail),
|
};
|
||||||
},
|
match wkd_url.to_file_path(VARIANT) {
|
||||||
|
Ok(path) => Ok(path),
|
||||||
Err(_) => Err(Error::ParseMail),
|
Err(_) => Err(Error::ParseMail),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue