mcaptcah uses const routes

This commit is contained in:
realaravinth 2021-05-02 16:35:15 +05:30
parent 76ae2b03e9
commit 5361e9b43a
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
9 changed files with 141 additions and 46 deletions

View file

@ -68,7 +68,7 @@ pub struct AccountCheckResp {
pub exists: bool,
}
pub fn service(cfg: &mut actix_web::web::ServiceConfig) {
pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
use crate::define_resource;
use crate::V1_API_ROUTES;

View file

@ -53,6 +53,17 @@ async fn uname_email_exists_works() {
.await;
assert_eq!(resp.status(), StatusCode::OK);
// chech if get user secret works
let resp = test::call_service(
&mut app,
test::TestRequest::post()
.cookie(cookies.clone())
.uri(ROUTES.account.update_secret)
.to_request(),
)
.await;
assert_eq!(resp.status(), StatusCode::OK);
let mut payload = AccountCheckPayload { val: NAME.into() };
let user_exists_resp = test::call_service(

View file

@ -47,7 +47,7 @@ pub mod routes {
}
}
pub fn service(cfg: &mut web::ServiceConfig) {
pub fn services(cfg: &mut web::ServiceConfig) {
use crate::define_resource;
use crate::V1_API_ROUTES;

View file

@ -16,12 +16,11 @@
*/
use actix_identity::Identity;
use actix_web::{post, web, HttpResponse, Responder};
use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use crate::api::v1::mcaptcha::mcaptcha::MCaptchaDetails;
use crate::errors::*;
use crate::CheckLogin;
use crate::Data;
pub mod routes {
@ -45,8 +44,8 @@ pub struct UpdateDuration {
pub duration: i32,
}
#[post("/api/v1/mcaptcha/domain/token/duration/update", wrap = "CheckLogin")]
pub async fn update_duration(
//#[post("/api/v1/mcaptcha/domain/token/duration/update", wrap = "CheckLogin")]
async fn update_duration(
payload: web::Json<UpdateDuration>,
data: web::Data<Data>,
id: Identity,
@ -83,8 +82,8 @@ pub struct GetDuration {
pub token: String,
}
#[post("/api/v1/mcaptcha/domain/token/duration/get", wrap = "CheckLogin")]
pub async fn get_duration(
//#[post("/api/v1/mcaptcha/domain/token/duration/get", wrap = "CheckLogin")]
async fn get_duration(
payload: web::Json<MCaptchaDetails>,
data: web::Data<Data>,
id: Identity,
@ -103,6 +102,24 @@ pub async fn get_duration(
Ok(HttpResponse::Ok().json(duration))
}
pub fn services(cfg: &mut web::ServiceConfig) {
use crate::define_resource;
use crate::V1_API_ROUTES;
define_resource!(
cfg,
V1_API_ROUTES.duration.get,
Methods::ProtectPost,
get_duration
);
define_resource!(
cfg,
V1_API_ROUTES.duration.update,
Methods::ProtectPost,
update_duration
);
}
#[cfg(test)]
mod tests {
use actix_web::http::{header, StatusCode};

View file

@ -16,13 +16,12 @@
*/
use actix_identity::Identity;
use actix_web::{post, web, HttpResponse, Responder};
use actix_web::{web, HttpResponse, Responder};
use m_captcha::{defense::Level, DefenseBuilder};
use serde::{Deserialize, Serialize};
use crate::api::v1::mcaptcha::mcaptcha::MCaptchaDetails;
use crate::errors::*;
use crate::CheckLogin;
use crate::Data;
pub mod routes {
@ -57,10 +56,42 @@ pub struct AddLevels {
pub key: String,
}
pub fn services(cfg: &mut web::ServiceConfig) {
use crate::define_resource;
use crate::V1_API_ROUTES;
define_resource!(
cfg,
V1_API_ROUTES.levels.add,
Methods::ProtectPost,
add_levels
);
define_resource!(
cfg,
V1_API_ROUTES.levels.update,
Methods::ProtectPost,
update_levels
);
define_resource!(
cfg,
V1_API_ROUTES.levels.delete,
Methods::ProtectPost,
delete_levels
);
define_resource!(
cfg,
V1_API_ROUTES.levels.get,
Methods::ProtectPost,
get_levels
);
}
// TODO try for non-existent token names
#[post("/api/v1/mcaptcha/levels/add", wrap = "CheckLogin")]
pub async fn add_levels(
//#[post("/api/v1/mcaptcha/levels/add", wrap = "CheckLogin")]
async fn add_levels(
payload: web::Json<AddLevels>,
data: web::Data<Data>,
id: Identity,
@ -99,8 +130,8 @@ pub async fn add_levels(
Ok(HttpResponse::Ok())
}
#[post("/api/v1/mcaptcha/levels/update", wrap = "CheckLogin")]
pub async fn update_levels(
//#[post("/api/v1/mcaptcha/levels/update", wrap = "CheckLogin")]
async fn update_levels(
payload: web::Json<AddLevels>,
data: web::Data<Data>,
id: Identity,
@ -157,8 +188,8 @@ pub async fn update_levels(
Ok(HttpResponse::Ok())
}
#[post("/api/v1/mcaptcha/levels/delete", wrap = "CheckLogin")]
pub async fn delete_levels(
//#[post("/api/v1/mcaptcha/levels/delete", wrap = "CheckLogin")]
async fn delete_levels(
payload: web::Json<AddLevels>,
data: web::Data<Data>,
id: Identity,
@ -184,8 +215,8 @@ pub async fn delete_levels(
Ok(HttpResponse::Ok())
}
#[post("/api/v1/mcaptcha/levels/get", wrap = "CheckLogin")]
pub async fn get_levels(
//#[post("/api/v1/mcaptcha/levels/get", wrap = "CheckLogin")]
async fn get_levels(
payload: web::Json<MCaptchaDetails>,
data: web::Data<Data>,
id: Identity,

View file

@ -17,12 +17,11 @@
use std::borrow::Cow;
use actix_identity::Identity;
use actix_web::{post, web, HttpResponse, Responder};
use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use super::get_random;
use crate::errors::*;
use crate::CheckLogin;
use crate::Data;
pub mod routes {
@ -45,6 +44,38 @@ pub mod routes {
}
}
pub fn services(cfg: &mut web::ServiceConfig) {
use crate::define_resource;
use crate::V1_API_ROUTES;
define_resource!(
cfg,
V1_API_ROUTES.mcaptcha.add,
Methods::ProtectPost,
add_mcaptcha
);
define_resource!(
cfg,
V1_API_ROUTES.mcaptcha.update_key,
Methods::ProtectPost,
update_token
);
define_resource!(
cfg,
V1_API_ROUTES.mcaptcha.delete,
Methods::ProtectPost,
delete_mcaptcha
);
define_resource!(
cfg,
V1_API_ROUTES.mcaptcha.get_token,
Methods::ProtectPost,
get_token
);
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MCaptchaID {
pub name: Option<String>,
@ -57,8 +88,8 @@ pub struct MCaptchaDetails {
}
// this should be called from within add levels
#[post("/api/v1/mcaptcha/add", wrap = "CheckLogin")]
pub async fn add_mcaptcha(data: web::Data<Data>, id: Identity) -> ServiceResult<impl Responder> {
//#[post("/api/v1/mcaptcha/add", wrap = "CheckLogin")]
async fn add_mcaptcha(data: web::Data<Data>, id: Identity) -> ServiceResult<impl Responder> {
let username = id.identity().unwrap();
let mut key;
@ -99,14 +130,12 @@ pub async fn add_mcaptcha(data: web::Data<Data>, id: Identity) -> ServiceResult<
Ok(HttpResponse::Ok().json(resp))
}
#[post("/api/v1/mcaptcha/update/key", wrap = "CheckLogin")]
pub async fn update_token(
//#[post("/api/v1/mcaptcha/update/key", wrap = "CheckLogin")]
async fn update_token(
payload: web::Json<MCaptchaDetails>,
data: web::Data<Data>,
id: Identity,
) -> ServiceResult<impl Responder> {
use std::borrow::Cow;
let username = id.identity().unwrap();
let mut key;
@ -152,8 +181,8 @@ async fn update_token_helper(
Ok(())
}
#[post("/api/v1/mcaptcha/get", wrap = "CheckLogin")]
pub async fn get_token(
//#[post("/api/v1/mcaptcha/get", wrap = "CheckLogin")]
async fn get_token(
payload: web::Json<MCaptchaDetails>,
data: web::Data<Data>,
id: Identity,
@ -180,8 +209,8 @@ pub async fn get_token(
Ok(HttpResponse::Ok().json(res))
}
#[post("/api/v1/mcaptcha/delete", wrap = "CheckLogin")]
pub async fn delete_mcaptcha(
//#[post("/api/v1/mcaptcha/delete", wrap = "CheckLogin")]
async fn delete_mcaptcha(
payload: web::Json<MCaptchaDetails>,
data: web::Data<Data>,
id: Identity,

View file

@ -33,3 +33,9 @@ pub fn get_random(len: usize) -> String {
.take(len)
.collect::<String>()
}
pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
duration::services(cfg);
levels::services(cfg);
mcaptcha::services(cfg);
}

View file

@ -74,7 +74,7 @@ async fn health(data: web::Data<Data>) -> impl Responder {
HttpResponse::Ok().json(resp_builder.build().unwrap())
}
pub fn service(cfg: &mut web::ServiceConfig) {
pub fn services(cfg: &mut web::ServiceConfig) {
use crate::define_resource;
use crate::V1_API_ROUTES;

View file

@ -26,28 +26,29 @@ mod routes;
pub use routes::ROUTES;
pub fn services(cfg: &mut ServiceConfig) {
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);
//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);
//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);
// // duration
// cfg.service(mcaptcha::duration::update_duration);
// cfg.service(mcaptcha::duration::get_duration);
}
pub fn new_services(cfg: &mut ServiceConfig) {
meta::service(cfg);
auth::service(cfg);
account::service(cfg);
meta::services(cfg);
auth::services(cfg);
account::services(cfg);
mcaptcha::services(cfg);
//define_resource!(
// cfg,