0
0
Fork 0
mirror of https://git.verdigado.com/NB-Public/simple-wkd.git synced 2024-12-06 14:52:41 +01:00

Remove sequoia-net dependency

This commit is contained in:
Delta1925 2023-05-20 16:37:31 +02:00
parent d3888e74bc
commit e62c174ca7
No known key found for this signature in database
GPG key ID: 1C21ACE44193CB25
6 changed files with 64 additions and 371 deletions

View file

@ -4,8 +4,8 @@ use log::{debug, error, warn};
use crate::errors::SpecialErrors;
use crate::management::{delete_key, Action, Pending};
use crate::settings::{MAILER, ROOT_FOLDER, SETTINGS};
use crate::utils::{extract_domain, get_email_from_cert, parse_pem, read_file};
use crate::settings::{MAILER, SETTINGS};
use crate::utils::{get_email_from_cert, insert_key, parse_pem, read_file};
use crate::{log_err, pending_path};
use anyhow::Result;
@ -26,11 +26,7 @@ pub fn confirm_action(token: &str) -> Result<(Action, String)> {
Action::Add => {
let cert = parse_pem(key.data())?;
let email = get_email_from_cert(&cert)?;
let domain = extract_domain(&email)?;
log_err!(
sequoia_net::wkd::insert(ROOT_FOLDER, domain, SETTINGS.variant, &cert),
warn
)?;
log_err!(insert_key(&cert), warn)?;
email
}
Action::Delete => {

View file

@ -1,12 +1,12 @@
use crate::log_err;
use crate::settings::{ERROR_TEXT, ROOT_FOLDER};
use crate::utils::{get_user_file_path, pending_path, read_file};
use crate::settings::ERROR_TEXT;
use crate::utils::{email_to_file_path, pending_path, read_file};
use anyhow::Result;
use chrono::Utc;
use log::{debug, warn};
use serde::{Deserialize, Serialize};
use std::{fmt::Display, fs, path::Path};
use std::{fmt::Display, fs};
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum Action {
@ -100,7 +100,7 @@ pub fn clean_stale(max_age: i64) {
}
pub fn delete_key(email: &str) -> Result<()> {
let path = Path::new(&ROOT_FOLDER).join(get_user_file_path(email)?);
let path = email_to_file_path(email)?;
log_err!(fs::remove_file(path), warn)?;
Ok(())
}

View file

@ -1,7 +1,6 @@
use lettre::{transport::smtp::authentication::Credentials, AsyncSmtpTransport, Tokio1Executor};
use log::{debug, error};
use once_cell::sync::Lazy;
use sequoia_net::wkd::Variant;
use sequoia_openpgp::policy::StandardPolicy;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
@ -11,7 +10,6 @@ use crate::{log_err, utils::read_file};
#[derive(Serialize, Deserialize, Debug)]
pub struct Settings {
#[serde(with = "VariantDef")]
pub variant: Variant,
pub max_age: i64,
pub cleanup_interval: u64,
@ -34,8 +32,7 @@ pub struct MailSettings {
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(remote = "Variant")]
pub enum VariantDef {
pub enum Variant {
Advanced,
Direct,
}
@ -55,14 +52,13 @@ fn get_settings() -> Settings {
panic!("Unable to access settings file!")
}
};
let settings = match log_err!(toml::from_str(&content), error) {
match log_err!(toml::from_str(&content), error) {
Ok(settings) => settings,
Err(_) => {
error!("Unable to parse settings from file!");
panic!("Unable to parse settings from file!")
}
};
settings
}
}
fn get_mailer() -> AsyncSmtpTransport<Tokio1Executor> {
@ -79,7 +75,7 @@ fn get_mailer() -> AsyncSmtpTransport<Tokio1Executor> {
AsyncSmtpTransport::<Tokio1Executor>::starttls_relay(&SETTINGS.mail_settings.smtp_host)
}
};
let mailer = match builder {
match builder {
Ok(builder) => builder,
Err(_) => {
error!("Unable to set up smtp");
@ -88,8 +84,7 @@ fn get_mailer() -> AsyncSmtpTransport<Tokio1Executor> {
}
.credentials(creds)
.port(SETTINGS.mail_settings.smtp_port)
.build();
mailer
.build()
}
pub const ERROR_TEXT: &str = "An error occoured:";

View file

@ -1,6 +1,7 @@
use crate::errors::CompatErr;
use crate::errors::SpecialErrors;
use crate::log_err;
use crate::settings::Variant;
use crate::settings::ROOT_FOLDER;
use crate::settings::SETTINGS;
@ -14,7 +15,8 @@ use flexi_logger::{style, DeferredNow, FileSpec, FlexiLoggerError, Logger, Logge
use log::debug;
use log::error;
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use sequoia_net::wkd::Url;
use sequoia_openpgp::serialize::Marshal;
use sequoia_openpgp::types::HashAlgorithm;
use sequoia_openpgp::{parse::Parse, Cert};
use std::{
fs,
@ -31,6 +33,48 @@ macro_rules! validate_cert {
};
}
pub fn encode_local(local: &str) -> String {
let mut digest = vec![0; 20];
let mut algo = HashAlgorithm::SHA1.context().unwrap();
algo.update(local.as_bytes());
let _ = algo.digest(&mut digest);
zbase32::encode_full_bytes(&digest[..])
}
pub fn email_to_file_path(email: &str) -> Result<PathBuf> {
let address_data: Vec<&str> = email.split('@').collect();
if address_data.len() != 2 {
Err(SpecialErrors::MalformedEmail)?;
}
let domain = address_data[1];
let local_encoded = encode_local(address_data[0]);
let directory = match SETTINGS.variant {
Variant::Advanced => format!(".well-known/openpgpkey/{}/hu/{}", domain, local_encoded),
Variant::Direct => format!(".well-known/openpgpkey/hu/{}", local_encoded),
};
Ok(PathBuf::from(ROOT_FOLDER).join(directory))
}
pub fn insert_key(cert: &Cert) -> Result<()> {
let validcert = validate_cert!(cert)?;
let path = email_to_file_path(&get_email_from_cert(cert)?)?;
fs::create_dir_all(path.parent().unwrap())?;
let mut file = fs::File::create(&path)?;
validcert.export(&mut file)?;
fs::OpenOptions::new()
.write(true)
.create(true)
.open(path.parent().unwrap().parent().unwrap().join("policy"))?;
Ok(())
}
pub fn pending_path() -> PathBuf {
Path::new(&ROOT_FOLDER).join("pending")
}
@ -92,13 +136,8 @@ pub fn extract_domain(email: &str) -> Result<String> {
Ok(domain)
}
pub fn get_user_file_path(email: &str) -> Result<PathBuf> {
let wkd_url = log_err!(Url::from(email), debug)?;
wkd_url.to_file_path(SETTINGS.variant)
}
pub fn key_exists(email: &str) -> Result<bool> {
let path = get_user_file_path(email)?;
let path = email_to_file_path(email)?;
if !Path::new(&ROOT_FOLDER).join(path).is_file() {
debug!("No key found for user {}", email);
Err(SpecialErrors::InexistingUser)?