#
mcaptcha/api/v1/mcaptcha/
delete.rs

    1// Copyright (C) 2022  Aravinth Manivannan <realaravinth@batsense.net>
    2// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
    3//
    4// SPDX-License-Identifier: AGPL-3.0-or-later
    5
    6use actix_identity::Identity;
    7use actix_web::{web, HttpResponse, Responder};
    8use libmcaptcha::master::messages::RemoveCaptcha;
    9use serde::{Deserialize, Serialize};
    10
    11use db_core::Login;
    12
    13use crate::errors::*;
    14use crate::AppData;
    15
    16#[derive(Clone, Debug, Deserialize, Serialize)]
    17pub struct DeleteCaptcha {
    18    pub key: String,
    19    pub password: String,
    20}
    21
    22#[my_codegen::post(
    23    path = "crate::V1_API_ROUTES.captcha.delete",
    24    wrap = "crate::api::v1::get_middleware()"
    25)]
    26async fn delete(
    27    payload: web::Json<DeleteCaptcha>,
    28    data: AppData,
    29    id: Identity,
    30) -> ServiceResult<impl Responder> {
    31    use argon2_creds::Config;
    32
    33    let username = id.identity().unwrap();
    34
    35    let hash = data.db.get_password(&Login::Username(&username)).await?;
    36
    37    if !Config::verify(&hash.hash, &payload.password)? {
    38        return Err(ServiceError::WrongPassword);
    39    }
    40    let payload = payload.into_inner();
    41    data.db.delete_captcha(&username, &payload.key).await?;
    42
    43    if let Err(err) = data.captcha.remove(RemoveCaptcha(payload.key)).await {
    44        log::error!("Error while trying to remove captcha from cache {}", err);
    45    }
    46    Ok(HttpResponse::Ok())
    47}