mirror of
https://git.verdigado.com/NB-Public/simple-wkd.git
synced 2024-12-06 14:52:41 +01:00
Add a webpage
This commit is contained in:
parent
e621a735f5
commit
a91b213dbf
9 changed files with 107 additions and 47 deletions
|
@ -105,6 +105,13 @@ pub fn confirm_action(token: &str) -> Result<(Action, String), Error> {
|
|||
pub fn send_confirmation_email(address: &str, action: &Action, token: &str) -> Result<(), Error> {
|
||||
debug!("Sending email to {}", address);
|
||||
let template = fs::read_to_string(Path::new("assets").join("mail-template.html")).unwrap();
|
||||
let mut url = SETTINGS
|
||||
.external_url
|
||||
.join("api/")
|
||||
.unwrap()
|
||||
.join("confirm")
|
||||
.unwrap();
|
||||
url.set_query(Some(&format!("token={}", token)));
|
||||
let email = Message::builder()
|
||||
.from(match SETTINGS.mail_settings.mail_from.parse() {
|
||||
Ok(mailbox) => mailbox,
|
||||
|
@ -129,18 +136,7 @@ pub fn send_confirmation_email(address: &str, action: &Action, token: &str) -> R
|
|||
.header(ContentType::TEXT_HTML)
|
||||
.body(
|
||||
template
|
||||
.replace(
|
||||
"{{%u}}",
|
||||
SETTINGS
|
||||
.external_url
|
||||
.join("api/")
|
||||
.unwrap()
|
||||
.join("confirm/")
|
||||
.unwrap()
|
||||
.join(token)
|
||||
.unwrap()
|
||||
.as_ref(),
|
||||
)
|
||||
.replace("{{%u}}", url.as_ref())
|
||||
.replace("{{%a}}", &action.to_string().to_lowercase()),
|
||||
);
|
||||
|
||||
|
|
|
@ -3,33 +3,33 @@ use thiserror::Error;
|
|||
|
||||
#[derive(Error, Debug, Clone, Copy)]
|
||||
pub enum Error {
|
||||
#[error("(0x00) Cert is invalid")]
|
||||
#[error("(0x01) Cert is invalid")]
|
||||
InvalidCert,
|
||||
#[error("(0x01) Error while parsing cert")]
|
||||
#[error("(0x02) Error while parsing cert")]
|
||||
ParseCert,
|
||||
#[error("(0x02) Error while parsing an E-Mail address")]
|
||||
#[error("(0x03) Error while parsing an E-Mail address")]
|
||||
ParseEmail,
|
||||
#[error("(0x03) There is no pending request associated to this token")]
|
||||
#[error("(0x04) There is no pending request associated to this token")]
|
||||
MissingPending,
|
||||
#[error("(0x04) Requested key does not exist")]
|
||||
#[error("(0x05) Requested key does not exist")]
|
||||
MissingKey,
|
||||
#[error("(0x05) No E-Mail found in the certificate")]
|
||||
#[error("(0x06) No E-Mail found in the certificate")]
|
||||
MissingMail,
|
||||
#[error("(0x06) Error while sending the E-Mail")]
|
||||
#[error("(0x07) Error while sending the E-Mail")]
|
||||
SendMail,
|
||||
#[error("(0x07) rror while serializing data")]
|
||||
#[error("(0x08) rror while serializing data")]
|
||||
SerializeData,
|
||||
#[error("(0x08) Error while deserializing data")]
|
||||
#[error("(0x09) Error while deserializing data")]
|
||||
DeserializeData,
|
||||
#[error("(0x09) The file is inaccessible")]
|
||||
#[error("(0x0A) The file is inaccessible")]
|
||||
Inaccessible,
|
||||
#[error("(0x0A) Error while adding a key to the wkd")]
|
||||
#[error("(0x0B) Error while adding a key to the wkd")]
|
||||
AddingKey,
|
||||
#[error("(0x0B) Error while generating the wkd path")]
|
||||
#[error("(0x0C) Error while generating the wkd path")]
|
||||
PathGeneration,
|
||||
#[error("(0x0C) Error while generating the email")]
|
||||
#[error("(0x0D) Error while generating the email")]
|
||||
MailGeneration,
|
||||
#[error("(0x0D) Wrong email domain")]
|
||||
#[error("(0x0E) Wrong email domain")]
|
||||
WrongDomain,
|
||||
}
|
||||
|
||||
|
|
30
src/main.rs
30
src/main.rs
|
@ -23,18 +23,18 @@ use utils::init_logger;
|
|||
const PENDING_FOLDER: &str = "pending";
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Pem {
|
||||
struct Key {
|
||||
key: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Token {
|
||||
value: String,
|
||||
token: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Email {
|
||||
address: String,
|
||||
email: String,
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
|
@ -67,7 +67,7 @@ async fn main() -> std::io::Result<()> {
|
|||
}
|
||||
|
||||
#[post("/api/submit")]
|
||||
async fn submit(pem: web::Form<Pem>) -> Result<String> {
|
||||
async fn submit(pem: web::Form<Key>) -> Result<String> {
|
||||
let cert = parse_pem(&pem.key)?;
|
||||
let email = get_email_from_cert(&cert)?;
|
||||
is_email_allowed(&email)?;
|
||||
|
@ -75,24 +75,24 @@ async fn submit(pem: web::Form<Pem>) -> Result<String> {
|
|||
store_pending_addition(pem.key.clone(), &email, &token)?;
|
||||
send_confirmation_email(&email, &Action::Add, &token)?;
|
||||
info!("User {} submitted a key!", &email);
|
||||
Ok(String::from("Key submitted successfully!"))
|
||||
Ok(String::from("(0x00) Key submitted successfully!"))
|
||||
}
|
||||
|
||||
#[get("/api/confirm/{value}")]
|
||||
async fn confirm(token: web::Path<Token>) -> Result<String> {
|
||||
let (action, email) = confirm_action(&token.value)?;
|
||||
#[get("/api/confirm")]
|
||||
async fn confirm(token: web::Query<Token>) -> Result<String> {
|
||||
let (action, email) = confirm_action(&token.token)?;
|
||||
match action {
|
||||
Action::Add => info!("Key for user {} was added successfully!", email),
|
||||
Action::Delete => info!("Key for user {} was deleted successfully!", email),
|
||||
}
|
||||
Ok(String::from("Confirmation successful!"))
|
||||
Ok(String::from("(0x00) Confirmation successful!"))
|
||||
}
|
||||
|
||||
#[get("/api/delete/{address}")]
|
||||
async fn delete(email: web::Path<Email>) -> Result<String> {
|
||||
#[get("/api/delete")]
|
||||
async fn delete(email: web::Query<Email>) -> Result<String> {
|
||||
let token = gen_random_token();
|
||||
store_pending_deletion(email.address.clone(), &token)?;
|
||||
send_confirmation_email(&email.address, &Action::Delete, &token)?;
|
||||
info!("User {} requested the deletion of his key!", email.address);
|
||||
Ok(String::from("Deletion request submitted successfully!"))
|
||||
store_pending_deletion(email.email.clone(), &token)?;
|
||||
send_confirmation_email(&email.email, &Action::Delete, &token)?;
|
||||
info!("User {} requested the deletion of his key!", email.email);
|
||||
Ok(String::from("(0x00) Deletion request submitted successfully!"))
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::PENDING_FOLDER;
|
|||
use crate::{errors::Error, utils::get_filename};
|
||||
|
||||
use chrono::Utc;
|
||||
use log::{debug, warn};
|
||||
use log::{debug, error, warn};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{fmt::Display, fs, path::Path};
|
||||
|
||||
|
@ -74,10 +74,13 @@ pub fn store_pending_addition(pem: String, email: &str, token: &str) -> Result<(
|
|||
}
|
||||
|
||||
pub fn store_pending_deletion(email: String, token: &str) -> Result<(), Error> {
|
||||
match key_exists(&email) {
|
||||
Err(Error::PathGeneration) => debug!("Error while generating path for user {}", email),
|
||||
Err(Error::MissingKey) => debug!("There is no key for user {}", email),
|
||||
_ => (),
|
||||
if let Err(error) = key_exists(&email) {
|
||||
match error {
|
||||
Error::PathGeneration => debug!("Error while generating path for user {}", email),
|
||||
Error::MissingKey => debug!("There is no key for user {}", email),
|
||||
_ => error!("An unexpected error occoured!"),
|
||||
}
|
||||
return Err(error);
|
||||
}
|
||||
let pending = Pending::build_delete(email.clone());
|
||||
store_pending(&pending, token)?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue