mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2026-02-12 02:25:41 +00:00
add site key form
This commit is contained in:
@@ -177,7 +177,8 @@ async fn signout(id: Identity) -> impl Responder {
|
||||
if let Some(_) = id.identity() {
|
||||
id.forget();
|
||||
}
|
||||
HttpResponse::Ok()
|
||||
.set_header(header::LOCATION, "/login")
|
||||
.body("")
|
||||
HttpResponse::Found()
|
||||
.header(header::LOCATION, "/login")
|
||||
.finish()
|
||||
.into_body()
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ use actix_web::{web, HttpResponse, Responder};
|
||||
use m_captcha::{defense::Level, DefenseBuilder};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::mcaptcha::add_mcaptcha_util;
|
||||
use crate::api::v1::mcaptcha::mcaptcha::MCaptchaDetails;
|
||||
use crate::errors::*;
|
||||
use crate::Data;
|
||||
@@ -52,8 +53,6 @@ pub mod routes {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct AddLevels {
|
||||
pub levels: Vec<Level>,
|
||||
/// name is config_name
|
||||
pub key: String,
|
||||
}
|
||||
|
||||
pub fn services(cfg: &mut web::ServiceConfig) {
|
||||
@@ -104,6 +103,8 @@ async fn add_levels(
|
||||
|
||||
defense.build()?;
|
||||
|
||||
let mcaptcha_config = add_mcaptcha_util(&data, &id).await?;
|
||||
|
||||
for level in payload.levels.iter() {
|
||||
let difficulty_factor = level.difficulty_factor as i32;
|
||||
let visitor_threshold = level.visitor_threshold as i32;
|
||||
@@ -119,18 +120,25 @@ async fn add_levels(
|
||||
)));",
|
||||
difficulty_factor,
|
||||
visitor_threshold,
|
||||
&payload.key,
|
||||
&mcaptcha_config.key,
|
||||
&username,
|
||||
)
|
||||
.execute(&data.db)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok())
|
||||
Ok(HttpResponse::Ok().json(mcaptcha_config))
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct UpdateLevels {
|
||||
pub levels: Vec<Level>,
|
||||
/// name is config_name
|
||||
pub key: String,
|
||||
}
|
||||
|
||||
async fn update_levels(
|
||||
payload: web::Json<AddLevels>,
|
||||
payload: web::Json<UpdateLevels>,
|
||||
data: web::Data<Data>,
|
||||
id: Identity,
|
||||
) -> ServiceResult<impl Responder> {
|
||||
@@ -187,7 +195,7 @@ async fn update_levels(
|
||||
}
|
||||
|
||||
async fn delete_levels(
|
||||
payload: web::Json<AddLevels>,
|
||||
payload: web::Json<UpdateLevels>,
|
||||
data: web::Data<Data>,
|
||||
id: Identity,
|
||||
) -> ServiceResult<impl Responder> {
|
||||
@@ -304,7 +312,7 @@ mod tests {
|
||||
visitor_threshold: 5000,
|
||||
};
|
||||
let levels = vec![l1, l2];
|
||||
let add_level = AddLevels {
|
||||
let add_level = UpdateLevels {
|
||||
levels: levels.clone(),
|
||||
key: key.key.clone(),
|
||||
};
|
||||
@@ -337,7 +345,7 @@ mod tests {
|
||||
visitor_threshold: 5000,
|
||||
};
|
||||
let levels = vec![l1, l2];
|
||||
let add_level = AddLevels {
|
||||
let add_level = UpdateLevels {
|
||||
levels: levels.clone(),
|
||||
key: key.key.clone(),
|
||||
};
|
||||
|
||||
@@ -88,7 +88,7 @@ pub struct MCaptchaDetails {
|
||||
}
|
||||
|
||||
// this should be called from within add levels
|
||||
async fn add_mcaptcha(data: web::Data<Data>, id: Identity) -> ServiceResult<impl Responder> {
|
||||
pub async fn add_mcaptcha_util(data: &Data, id: &Identity) -> ServiceResult<MCaptchaDetails> {
|
||||
let username = id.identity().unwrap();
|
||||
let mut key;
|
||||
|
||||
@@ -125,7 +125,12 @@ async fn add_mcaptcha(data: web::Data<Data>, id: Identity) -> ServiceResult<impl
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(resp)
|
||||
}
|
||||
|
||||
// this should be called from within add levels
|
||||
async fn add_mcaptcha(data: web::Data<Data>, id: Identity) -> ServiceResult<impl Responder> {
|
||||
let resp = add_mcaptcha_util(&data, &id).await?;
|
||||
Ok(HttpResponse::Ok().json(resp))
|
||||
}
|
||||
|
||||
|
||||
@@ -123,7 +123,7 @@ async fn auth_works() {
|
||||
.to_request(),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(signout_resp.status(), StatusCode::OK);
|
||||
assert_eq!(signout_resp.status(), StatusCode::FOUND);
|
||||
let headers = signout_resp.headers();
|
||||
assert_eq!(headers.get(header::LOCATION).unwrap(), PAGES.auth.login);
|
||||
}
|
||||
|
||||
@@ -70,6 +70,10 @@ async fn protected_routes_work() {
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(authenticated_resp.status(), StatusCode::OK);
|
||||
if url == &V1_API_ROUTES.auth.logout {
|
||||
assert_eq!(authenticated_resp.status(), StatusCode::FOUND);
|
||||
} else {
|
||||
assert_eq!(authenticated_resp.status(), StatusCode::OK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user