diff --git a/src/api/v1/account/delete.rs b/src/api/v1/account/delete.rs index e7c58c75..ad365954 100644 --- a/src/api/v1/account/delete.rs +++ b/src/api/v1/account/delete.rs @@ -22,8 +22,7 @@ use super::auth::Password; use crate::errors::*; use crate::Data; -//#[post("/api/v1/account/delete", wrap = "CheckLogin")] -pub async fn delete_account( +async fn delete_account( id: Identity, payload: web::Json, data: web::Data, @@ -58,3 +57,15 @@ pub async fn delete_account( Err(_) => return Err(ServiceError::InternalServerError)?, } } + +pub fn services(cfg: &mut actix_web::web::ServiceConfig) { + use crate::define_resource; + use crate::V1_API_ROUTES; + + define_resource!( + cfg, + V1_API_ROUTES.account.delete, + Methods::ProtectPost, + delete_account + ); +} diff --git a/src/api/v1/account/email.rs b/src/api/v1/account/email.rs index 77142db4..475d787c 100644 --- a/src/api/v1/account/email.rs +++ b/src/api/v1/account/email.rs @@ -29,7 +29,6 @@ pub struct Email { pub email: String, } -//#[post("/api/v1/account/email/exists")] pub async fn email_exists( payload: web::Json, data: web::Data, @@ -53,9 +52,7 @@ pub async fn email_exists( } /// update email -//#[post("/api/v1/account/email/", wrap = "CheckLogin")] -//#[post("/api/v1/", wrap = "CheckLogin")] -pub async fn set_email( +async fn set_email( id: Identity, payload: web::Json, data: web::Data, @@ -85,3 +82,22 @@ pub async fn set_email( } Ok(HttpResponse::Ok()) } + +pub fn services(cfg: &mut actix_web::web::ServiceConfig) { + use crate::define_resource; + use crate::V1_API_ROUTES; + + define_resource!( + cfg, + V1_API_ROUTES.account.email_exists, + Methods::Post, + email_exists + ); + + define_resource!( + cfg, + V1_API_ROUTES.account.update_email, + Methods::Post, + set_email + ); +} diff --git a/src/api/v1/account/mod.rs b/src/api/v1/account/mod.rs index e674e231..90cf441a 100644 --- a/src/api/v1/account/mod.rs +++ b/src/api/v1/account/mod.rs @@ -69,48 +69,8 @@ pub struct AccountCheckResp { } pub fn services(cfg: &mut actix_web::web::ServiceConfig) { - use crate::define_resource; - use crate::V1_API_ROUTES; - - define_resource!( - cfg, - V1_API_ROUTES.account.delete, - Methods::ProtectPost, - delete::delete_account - ); - - define_resource!( - cfg, - V1_API_ROUTES.account.username_exists, - Methods::Post, - username::username_exists - ); - - define_resource!( - cfg, - V1_API_ROUTES.account.email_exists, - Methods::Post, - email::email_exists - ); - - define_resource!( - cfg, - V1_API_ROUTES.account.update_email, - Methods::Post, - email::set_email - ); - - define_resource!( - cfg, - V1_API_ROUTES.account.get_secret, - Methods::ProtectGet, - secret::get_secret - ); - - define_resource!( - cfg, - V1_API_ROUTES.account.update_secret, - Methods::ProtectPost, - secret::update_user_secret - ); + delete::services(cfg); + email::services(cfg); + username::services(cfg); + secret::services(cfg); } diff --git a/src/api/v1/account/secret.rs b/src/api/v1/account/secret.rs index 669754f9..5ed0e2c4 100644 --- a/src/api/v1/account/secret.rs +++ b/src/api/v1/account/secret.rs @@ -29,8 +29,7 @@ pub struct Secret { pub secret: String, } -//#[get("/api/v1/account/secret/", wrap = "CheckLogin")] -pub async fn get_secret(id: Identity, data: web::Data) -> ServiceResult { +async fn get_secret(id: Identity, data: web::Data) -> ServiceResult { let username = id.identity().unwrap(); let secret = sqlx::query_as!( @@ -44,11 +43,7 @@ pub async fn get_secret(id: Identity, data: web::Data) -> ServiceResult, -) -> ServiceResult { +async fn update_user_secret(id: Identity, data: web::Data) -> ServiceResult { let username = id.identity().unwrap(); let mut secret; @@ -79,3 +74,22 @@ pub async fn update_user_secret( } Ok(HttpResponse::Ok()) } + +pub fn services(cfg: &mut actix_web::web::ServiceConfig) { + use crate::define_resource; + use crate::V1_API_ROUTES; + + define_resource!( + cfg, + V1_API_ROUTES.account.get_secret, + Methods::ProtectGet, + get_secret + ); + + define_resource!( + cfg, + V1_API_ROUTES.account.update_secret, + Methods::ProtectPost, + update_user_secret + ); +} diff --git a/src/api/v1/account/username.rs b/src/api/v1/account/username.rs index c161ae35..186319d5 100644 --- a/src/api/v1/account/username.rs +++ b/src/api/v1/account/username.rs @@ -20,8 +20,7 @@ use super::{AccountCheckPayload, AccountCheckResp}; use crate::errors::*; use crate::Data; -//#[post("/api/v1/account/username/exists")] -pub async fn username_exists( +async fn username_exists( payload: web::Json, data: web::Data, ) -> ServiceResult { @@ -42,3 +41,15 @@ pub async fn username_exists( Ok(HttpResponse::Ok().json(resp)) } + +pub fn services(cfg: &mut actix_web::web::ServiceConfig) { + use crate::define_resource; + use crate::V1_API_ROUTES; + + define_resource!( + cfg, + V1_API_ROUTES.account.username_exists, + Methods::Post, + username_exists + ); +} diff --git a/src/api/v1/accountrs b/src/api/v1/accountrs deleted file mode 100644 index c459fefa..00000000 --- a/src/api/v1/accountrs +++ /dev/null @@ -1,246 +0,0 @@ -/* -* Copyright (C) 2021 Aravinth Manivannan -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU Affero General Public License as -* published by the Free Software Foundation, either version 3 of the -* License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU Affero General Public License for more details. -* -* You should have received a copy of the GNU Affero General Public License -* along with this program. If not, see . -*/ -use std::borrow::Cow; - -use actix_identity::Identity; -use actix_web::{get, post, web, HttpResponse, Responder}; -use serde::{Deserialize, Serialize}; - -use super::auth::Password; -use super::mcaptcha::get_random; -use crate::errors::*; -use crate::CheckLogin; -use crate::Data; - -pub mod routes { - - pub struct Account { - pub delete: &'static str, - pub email_exists: &'static str, - pub update_email: &'static str, - pub get_secret: &'static str, - pub update_secret: &'static str, - pub username_exists: &'static str, - } - - impl Account { - pub const fn new() -> Account { - let get_secret = "/api/v1/account/secret/"; - let update_secret = "/api/v1/account/secret"; - let delete = "/api/v1/account/delete"; - let email_exists = "/api/v1/account/email/exists"; - let username_exists = "/api/v1/account/username/exists"; - let update_email = "/api/v1/account/email/"; - Account { - get_secret, - update_secret, - username_exists, - update_email, - delete, - email_exists, - } - } - } -} - -#[post("/api/v1/account/delete", wrap = "CheckLogin")] -pub async fn delete_account( - id: Identity, - payload: web::Json, - data: web::Data, -) -> ServiceResult { - use argon2_creds::Config; - use sqlx::Error::RowNotFound; - - let username = id.identity().unwrap(); - - let rec = sqlx::query_as!( - Password, - r#"SELECT password FROM mcaptcha_users WHERE name = ($1)"#, - &username, - ) - .fetch_one(&data.db) - .await; - - id.forget(); - - match rec { - Ok(s) => { - if Config::verify(&s.password, &payload.password)? { - sqlx::query!("DELETE FROM mcaptcha_users WHERE name = ($1)", &username) - .execute(&data.db) - .await?; - Ok(HttpResponse::Ok()) - } else { - Err(ServiceError::WrongPassword) - } - } - Err(RowNotFound) => return Err(ServiceError::UsernameNotFound), - Err(_) => return Err(ServiceError::InternalServerError)?, - } -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct AccountCheckPayload { - pub val: String, -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct AccountCheckResp { - pub exists: bool, -} - -#[post("/api/v1/account/username/exists")] -pub async fn username_exists( - payload: web::Json, - data: web::Data, -) -> ServiceResult { - let res = sqlx::query!( - "SELECT EXISTS (SELECT 1 from mcaptcha_users WHERE name = $1)", - &payload.val, - ) - .fetch_one(&data.db) - .await?; - - let mut resp = AccountCheckResp { exists: false }; - - if let Some(x) = res.exists { - if x { - resp.exists = true; - } - } - - Ok(HttpResponse::Ok().json(resp)) -} - -#[post("/api/v1/account/email/exists")] -pub async fn email_exists( - payload: web::Json, - data: web::Data, -) -> ServiceResult { - let res = sqlx::query!( - "SELECT EXISTS (SELECT 1 from mcaptcha_users WHERE email = $1)", - &payload.val, - ) - .fetch_one(&data.db) - .await?; - - let mut resp = AccountCheckResp { exists: false }; - - if let Some(x) = res.exists { - if x { - resp.exists = true; - } - } - - Ok(HttpResponse::Ok().json(resp)) -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct Secret { - pub secret: String, -} - -#[get("/api/v1/account/secret/", wrap = "CheckLogin")] -pub async fn get_secret(id: Identity, data: web::Data) -> ServiceResult { - let username = id.identity().unwrap(); - - let secret = sqlx::query_as!( - Secret, - r#"SELECT secret FROM mcaptcha_users WHERE name = ($1)"#, - &username, - ) - .fetch_one(&data.db) - .await?; - - Ok(HttpResponse::Ok().json(secret)) -} - -#[post("/api/v1/account/secret/", wrap = "CheckLogin")] -pub async fn update_user_secret( - id: Identity, - data: web::Data, -) -> ServiceResult { - let username = id.identity().unwrap(); - - let mut secret; - - loop { - secret = get_random(32); - let res = sqlx::query!( - "UPDATE mcaptcha_users set secret = $1 - WHERE name = $2", - &secret, - &username, - ) - .execute(&data.db) - .await; - if res.is_ok() { - break; - } else { - if let Err(sqlx::Error::Database(err)) = res { - if err.code() == Some(Cow::from("23505")) - && err.message().contains("mcaptcha_users_secret_key") - { - continue; - } else { - Err(sqlx::Error::Database(err))?; - } - }; - } - } - Ok(HttpResponse::Ok()) -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct Email { - pub email: String, -} - -/// update email -//#[post("/api/v1/account/email/", wrap = "CheckLogin")] -#[post("/api/v1/", wrap = "CheckLogin")] -pub async fn set_email( - id: Identity, - payload: web::Json, - data: web::Data, -) -> ServiceResult { - let username = id.identity().unwrap(); - - data.creds.email(&payload.email)?; - - let res = sqlx::query!( - "UPDATE mcaptcha_users set email = $1 - WHERE name = $2", - &payload.email, - &username, - ) - .execute(&data.db) - .await; - if !res.is_ok() { - if let Err(sqlx::Error::Database(err)) = res { - if err.code() == Some(Cow::from("23505")) - && err.message().contains("mcaptcha_users_email_key") - { - Err(ServiceError::EmailTaken)? - } else { - Err(sqlx::Error::Database(err))? - } - }; - } - Ok(HttpResponse::Ok()) -} diff --git a/src/api/v1/auth.rs b/src/api/v1/auth.rs index 50259d97..fa01ea12 100644 --- a/src/api/v1/auth.rs +++ b/src/api/v1/auth.rs @@ -75,7 +75,6 @@ pub struct Password { pub password: String, } -//#[post("/api/v1/signup")] async fn signup( payload: web::Json, data: web::Data, @@ -143,7 +142,6 @@ async fn signup( Ok(HttpResponse::Ok()) } -//#[post("/api/v1/signin")] async fn signin( id: Identity, payload: web::Json, @@ -175,7 +173,6 @@ async fn signin( } } -//#[get("/logout", wrap = "CheckLogin")] async fn signout(id: Identity) -> impl Responder { if let Some(_) = id.identity() { id.forget(); diff --git a/src/api/v1/mcaptcha/duration.rs b/src/api/v1/mcaptcha/duration.rs index a00aad59..0b94311c 100644 --- a/src/api/v1/mcaptcha/duration.rs +++ b/src/api/v1/mcaptcha/duration.rs @@ -44,7 +44,6 @@ pub struct UpdateDuration { pub duration: i32, } -//#[post("/api/v1/mcaptcha/domain/token/duration/update", wrap = "CheckLogin")] async fn update_duration( payload: web::Json, data: web::Data, @@ -82,7 +81,6 @@ pub struct GetDuration { pub token: String, } -//#[post("/api/v1/mcaptcha/domain/token/duration/get", wrap = "CheckLogin")] async fn get_duration( payload: web::Json, data: web::Data, diff --git a/src/api/v1/mcaptcha/levels.rs b/src/api/v1/mcaptcha/levels.rs index cd197fa9..0c869fba 100644 --- a/src/api/v1/mcaptcha/levels.rs +++ b/src/api/v1/mcaptcha/levels.rs @@ -52,7 +52,7 @@ pub mod routes { #[derive(Serialize, Deserialize)] pub struct AddLevels { pub levels: Vec, - // name is config_name + /// name is config_name pub key: String, } @@ -90,7 +90,6 @@ pub fn services(cfg: &mut web::ServiceConfig) { // TODO try for non-existent token names -//#[post("/api/v1/mcaptcha/levels/add", wrap = "CheckLogin")] async fn add_levels( payload: web::Json, data: web::Data, @@ -130,7 +129,6 @@ async fn add_levels( Ok(HttpResponse::Ok()) } -//#[post("/api/v1/mcaptcha/levels/update", wrap = "CheckLogin")] async fn update_levels( payload: web::Json, data: web::Data, @@ -188,7 +186,6 @@ async fn update_levels( Ok(HttpResponse::Ok()) } -//#[post("/api/v1/mcaptcha/levels/delete", wrap = "CheckLogin")] async fn delete_levels( payload: web::Json, data: web::Data, @@ -215,7 +212,6 @@ async fn delete_levels( Ok(HttpResponse::Ok()) } -//#[post("/api/v1/mcaptcha/levels/get", wrap = "CheckLogin")] async fn get_levels( payload: web::Json, data: web::Data, @@ -281,23 +277,6 @@ mod tests { let (data, _, signin_resp, key) = add_levels_util(NAME, PASSWORD).await; let cookies = get_cookie!(signin_resp); let mut app = get_app!(data).await; - /* - - let add_level = AddLevels { - levels: levels.clone(), - key: key.key.clone(), - }; - - // 1. add level - let add_token_resp = test::call_service( - &mut app, - post_request!(&add_level, ADD_URL) - .cookie(cookies.clone()) - .to_request(), - ) - .await; - assert_eq!(add_token_resp.status(), StatusCode::OK); - */ // 2. get level diff --git a/src/api/v1/mcaptcha/mcaptcha.rs b/src/api/v1/mcaptcha/mcaptcha.rs index 6437857e..0b207db1 100644 --- a/src/api/v1/mcaptcha/mcaptcha.rs +++ b/src/api/v1/mcaptcha/mcaptcha.rs @@ -88,7 +88,6 @@ pub struct MCaptchaDetails { } // this should be called from within add levels -//#[post("/api/v1/mcaptcha/add", wrap = "CheckLogin")] async fn add_mcaptcha(data: web::Data, id: Identity) -> ServiceResult { let username = id.identity().unwrap(); let mut key; @@ -130,7 +129,6 @@ async fn add_mcaptcha(data: web::Data, id: Identity) -> ServiceResult, data: web::Data, @@ -181,7 +179,6 @@ async fn update_token_helper( Ok(()) } -//#[post("/api/v1/mcaptcha/get", wrap = "CheckLogin")] async fn get_token( payload: web::Json, data: web::Data, @@ -209,7 +206,6 @@ async fn get_token( Ok(HttpResponse::Ok().json(res)) } -//#[post("/api/v1/mcaptcha/delete", wrap = "CheckLogin")] async fn delete_mcaptcha( payload: web::Json, data: web::Data, @@ -264,10 +260,6 @@ mod tests { let cookies = get_cookie!(signin_resp); let mut app = get_app!(data).await; - // let mut domain = MCaptchaID { - // name: TOKEN_NAME.into(), - // }; - // 4. delete token let del_token = test::call_service( &mut app, diff --git a/src/api/v1/meta.rs b/src/api/v1/meta.rs index 7b63cf2c..f3c09455 100644 --- a/src/api/v1/meta.rs +++ b/src/api/v1/meta.rs @@ -92,13 +92,12 @@ mod tests { use actix_web::{http::StatusCode, test, App}; use super::*; - use crate::api::v1::new_services; + use crate::api::v1::services; use crate::*; #[actix_rt::test] async fn build_details_works() { - // const GET_URI: &str = "/api/v1/meta/build"; - let mut app = test::init_service(App::new().configure(new_services)).await; + let mut app = test::init_service(App::new().configure(services)).await; let resp = test::call_service( &mut app, @@ -112,8 +111,6 @@ mod tests { #[actix_rt::test] async fn health_works() { - // const GET_URI: &str = "/api/v1/meta/health"; - println!("{}", V1_API_ROUTES.meta.health); let data = Data::new().await; let mut app = get_app!(data).await; diff --git a/src/api/v1/mod.rs b/src/api/v1/mod.rs index 8cee6d11..a9045f27 100644 --- a/src/api/v1/mod.rs +++ b/src/api/v1/mod.rs @@ -26,87 +26,11 @@ mod routes; pub use routes::ROUTES; -pub fn services(_cfg: &mut ServiceConfig) { - // mcaptcha - //cfg.service(mcaptcha::mcaptcha::add_mcaptcha); - //cfg.service(mcaptcha::mcaptcha::delete_mcaptcha); - //cfg.service(mcaptcha::mcaptcha::update_token); - //cfg.service(mcaptcha::mcaptcha::get_token); - - // levels - //cfg.service(mcaptcha::levels::add_levels); - //cfg.service(mcaptcha::levels::update_levels); - //cfg.service(mcaptcha::levels::delete_levels); - //cfg.service(mcaptcha::levels::get_levels); - - // // duration - // cfg.service(mcaptcha::duration::update_duration); - // cfg.service(mcaptcha::duration::get_duration); -} - -pub fn new_services(cfg: &mut ServiceConfig) { +pub fn services(cfg: &mut ServiceConfig) { meta::services(cfg); auth::services(cfg); account::services(cfg); mcaptcha::services(cfg); - - //define_resource!( - // cfg, - // ROUTES.meta.build_details, - // Methods::Get, - // meta::build_details - //); - //define_resource!(cfg, ROUTES.meta.health, Methods::Get, meta::health); - - // auth - - //define_resource!(cfg, ROUTES.auth.register, Methods::Post, auth::signup); - //define_resource!(cfg, ROUTES.auth.logout, Methods::ProtectGet, auth::signout); - //define_resource!(cfg, ROUTES.auth.login, Methods::Post, auth::signin); - - // account - - // define_resource!( - // cfg, - // ROUTES.account.delete, - // Methods::ProtectPost, - // account::delete::delete_account - // ); - // - // define_resource!( - // cfg, - // ROUTES.account.username_exists, - // Methods::Post, - // account::username::username_exists - // ); - // - // define_resource!( - // cfg, - // ROUTES.account.email_exists, - // Methods::Post, - // account::email::email_exists - // ); - // - // define_resource!( - // cfg, - // ROUTES.account.update_email, - // Methods::Post, - // account::email::set_email - // ); - // - // define_resource!( - // cfg, - // ROUTES.account.get_secret, - // Methods::ProtectGet, - // account::secret::get_secret - // ); - // - // define_resource!( - // cfg, - // ROUTES.account.update_secret, - // Methods::ProtectPost, - // account::secret::update_user_secret - // ); } #[cfg(test)] diff --git a/src/errors.rs b/src/errors.rs index 499f57e5..df9ec9c9 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -26,7 +26,6 @@ use actix_web::{ use argon2_creds::errors::CredsError; //use awc::error::SendRequestError; use derive_more::{Display, Error}; -use log::debug; use m_captcha::errors::CaptchaError; use serde::{Deserialize, Serialize}; use url::ParseError; @@ -140,7 +139,6 @@ impl ResponseError for ServiceError { impl From for ServiceError { #[cfg(not(tarpaulin_include))] fn from(e: CredsError) -> ServiceError { - debug!("{:?}", &e); match e { CredsError::UsernameCaseMappedError => ServiceError::UsernameCaseMappedError, CredsError::ProfainityError => ServiceError::ProfainityError, diff --git a/src/main.rs b/src/main.rs index 48d31657..98e81465 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,7 +90,6 @@ async fn main() -> std::io::Result<()> { )) .configure(v1::pow::services) .configure(v1::services) - .configure(v1::new_services) .configure(docs::services) .configure(static_assets::services) .configure(templates::services) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index d82519a0..2a546ab8 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -57,7 +57,6 @@ macro_rules! get_app { )) .configure(crate::api::v1::pow::services) .configure(crate::api::v1::services) - .configure(crate::api::v1::new_services) .data($data.clone()), ) };