mirror of
https://git.verdigado.com/NB-Public/simple-wkd.git
synced 2024-10-30 03:05:51 +01:00
Improve file checking
This commit is contained in:
parent
9efbaefd92
commit
4b445a6a96
5 changed files with 18 additions and 4 deletions
|
@ -9,7 +9,7 @@ use std::path::Path;
|
|||
|
||||
pub fn confirm_action(token: &str) -> Result<(), Error> {
|
||||
let pending_path = pending_path!().join(token);
|
||||
let content = if pending_path.exists() {
|
||||
let content = if pending_path.is_file() {
|
||||
match fs::read_to_string(&pending_path) {
|
||||
Ok(content) => content,
|
||||
Err(_) => return Err(Error::Inaccessible),
|
||||
|
|
|
@ -19,6 +19,8 @@ pub enum Error {
|
|||
DeserializeData,
|
||||
#[error("File or directory does not exist")]
|
||||
MissingPath,
|
||||
#[error("Requested key does not exist")]
|
||||
MissingKey,
|
||||
#[error("The file is inaccessible")]
|
||||
Inaccessible,
|
||||
#[error("Error while adding a key to the wkd")]
|
||||
|
@ -29,6 +31,7 @@ impl actix_web::ResponseError for Error {
|
|||
fn status_code(&self) -> actix_web::http::StatusCode {
|
||||
match self {
|
||||
Self::MissingPath => StatusCode::from_u16(404).unwrap(),
|
||||
Self::MissingKey => StatusCode::from_u16(404).unwrap(),
|
||||
_ => StatusCode::from_u16(500).unwrap(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ mod errors;
|
|||
mod management;
|
||||
mod utils;
|
||||
|
||||
use crate::utils::key_exists;
|
||||
|
||||
use self::confirmation::{confirm_action, send_confirmation_email};
|
||||
use self::management::{clean_stale, store_pending_addition, store_pending_deletion, Action};
|
||||
use self::utils::{gen_random_token, get_email_from_cert, parse_pem};
|
||||
|
@ -69,6 +71,7 @@ async fn confirm(token: web::Path<Token>) -> Result<String> {
|
|||
|
||||
#[get("/api/delete/{address}")]
|
||||
async fn delete(email: web::Path<Email>) -> Result<String> {
|
||||
key_exists(&email.address)?;
|
||||
let token = gen_random_token();
|
||||
store_pending_deletion(email.address.clone(), &token)?;
|
||||
send_confirmation_email(&email.address, &Action::Delete, &token);
|
||||
|
|
|
@ -73,7 +73,7 @@ pub fn store_pending_deletion(email: String, token: &str) -> Result<(), Error> {
|
|||
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() {
|
||||
if file_path.is_file() {
|
||||
let content = match fs::read_to_string(&file_path) {
|
||||
Ok(content) => content,
|
||||
Err(_) => return Err(Error::Inaccessible),
|
||||
|
|
12
src/utils.rs
12
src/utils.rs
|
@ -1,10 +1,10 @@
|
|||
use crate::errors::Error;
|
||||
use crate::VARIANT;
|
||||
use crate::{PATH, PENDING, VARIANT};
|
||||
|
||||
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
||||
use sequoia_net::wkd::Url;
|
||||
use sequoia_openpgp::{parse::Parse, Cert};
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! pending_path {
|
||||
|
@ -50,3 +50,11 @@ pub fn get_user_file_path(email: &str) -> Result<PathBuf, Error> {
|
|||
Err(_) => Err(Error::ParseMail),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn key_exists(email: &str) -> Result<bool, Error> {
|
||||
let path = get_user_file_path(email)?;
|
||||
if !pending_path!().join(path).is_file() {
|
||||
return Err(Error::MissingKey);
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue