0
0
Fork 0
mirror of https://git.verdigado.com/NB-Public/simple-wkd.git synced 2024-10-30 07:05:51 +01:00

Move validate_cert from macro to function

This commit is contained in:
RisingOpsNinja 2024-09-04 11:37:31 +02:00
parent a61f03f412
commit 711d626aa9
3 changed files with 11 additions and 14 deletions

View file

@ -5,8 +5,8 @@ use log::{debug, error, warn};
use crate::errors::SpecialErrors;
use crate::management::{delete_key, Action, Pending};
use crate::settings::{MAILER, SETTINGS};
use crate::utils::{get_email_from_cert, insert_key, parse_pem, read_file};
use crate::{log_err, pending_path, validate_cert};
use crate::utils::{get_email_from_cert, insert_key, parse_pem, read_file, validate_cert};
use crate::{log_err, pending_path};
use anyhow::Result;
use lettre::{AsyncTransport, Message};
@ -25,7 +25,7 @@ pub fn confirm_action(token: &str) -> Result<(Action, String)> {
let address = match key.action() {
Action::Add => {
let cert = parse_pem(key.data())?;
let validcert = validate_cert!(cert)?;
let validcert = validate_cert(&cert)?;
let email = get_email_from_cert(&validcert)?;
log_err!(insert_key(&validcert), warn)?;
email

View file

@ -11,7 +11,7 @@ use crate::management::{clean_stale, store_pending_addition, store_pending_delet
use crate::settings::{ROOT_FOLDER, SETTINGS};
use crate::utils::{
gen_random_token, get_email_from_cert, is_email_allowed, key_exists, parse_pem, read_file,
return_outcome,
return_outcome, validate_cert
};
use actix_files::Files;
@ -102,7 +102,7 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, CompatErr> {
#[post("/api/submit")]
async fn submit(pem: web::Form<Key>) -> Result<HttpResponse, CompatErr> {
let cert = parse_pem(&pem.key)?;
let validcert = validate_cert!(cert)?;
let validcert = validate_cert(&cert)?;
if validcert.is_tsk() {
Err(SpecialErrors::ContainsSecret)?
}

View file

@ -24,14 +24,11 @@ use std::{
path::{Path, PathBuf},
};
#[macro_export]
macro_rules! validate_cert {
( $x:expr ) => {
match log_err!($x.with_policy($crate::settings::POLICY, None), debug) {
Ok(validcert) => Ok(validcert),
Err(_) => Err($crate::errors::SpecialErrors::InvalidCert),
}
};
pub fn validate_cert(cert: &Cert) -> Result<ValidCert> {
match log_err!(cert.with_policy(crate::settings::POLICY, None), debug) {
Ok(validcert) => Ok(validcert),
Err(_) => Err(SpecialErrors::InvalidCert)?,
}
}
pub fn encode_local(local: &str) -> String {
@ -106,7 +103,7 @@ pub fn parse_pem(pemfile: &str) -> Result<Cert> {
Ok(cert) => cert,
Err(_) => Err(SpecialErrors::MalformedCert)?,
};
validate_cert!(cert)?;
validate_cert(&cert)?;
Ok(cert)
}