add site key form

This commit is contained in:
realaravinth
2021-05-03 20:24:03 +05:30
parent 0531a26274
commit 812b0ff2c9
49 changed files with 1253 additions and 597 deletions

View File

@@ -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()
}

View File

@@ -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(),
};

View File

@@ -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))
}

View File

@@ -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);
}

View File

@@ -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);
}
}
}

View File

@@ -18,6 +18,8 @@
use actix_web::{HttpResponse, Responder};
use sailfish::TemplateOnce;
use crate::pages::TITLE;
#[derive(Clone, TemplateOnce)]
#[template(path = "auth/login/index.html")]
struct IndexPage<'a> {
@@ -28,7 +30,7 @@ struct IndexPage<'a> {
impl<'a> Default for IndexPage<'a> {
fn default() -> Self {
IndexPage {
name: "mCaptcha",
name: TITLE,
title: "Login",
}
}

View File

@@ -18,6 +18,8 @@
use actix_web::{HttpResponse, Responder};
use sailfish::TemplateOnce;
use crate::pages::TITLE;
#[derive(TemplateOnce, Clone)]
#[template(path = "auth/register/index.html")]
struct IndexPage<'a> {
@@ -28,7 +30,7 @@ struct IndexPage<'a> {
impl<'a> Default for IndexPage<'a> {
fn default() -> Self {
IndexPage {
name: "mCaptcha",
name: TITLE,
title: "Join",
}
}

View File

@@ -21,6 +21,8 @@ mod auth;
mod panel;
pub mod routes;
pub const TITLE: &str = "mCaptcha";
pub fn services(cfg: &mut ServiceConfig) {
auth::services(cfg);
panel::services(cfg);
@@ -59,7 +61,11 @@ mod tests {
)
.await;
let urls = vec![PAGES.home, PAGES.panel.sitekey.add];
let urls = vec![
PAGES.home,
PAGES.panel.sitekey.add,
PAGES.panel.sitekey.list,
];
for url in urls.iter() {
let resp =

View File

@@ -18,6 +18,8 @@
use actix_web::{HttpResponse, Responder};
use sailfish::TemplateOnce;
use crate::pages::TITLE;
pub mod sitekey;
#[derive(TemplateOnce, Clone)]
@@ -27,13 +29,13 @@ pub struct IndexPage<'a> {
pub title: &'a str,
}
const TITLE: &str = "Dashboard";
const COMPONENT: &str = "Dashboard";
impl<'a> Default for IndexPage<'a> {
fn default() -> Self {
IndexPage {
name: "mCaptcha",
title: TITLE,
name: TITLE,
title: COMPONENT,
}
}
}

View File

@@ -18,6 +18,8 @@
use actix_web::{HttpResponse, Responder};
use sailfish::TemplateOnce;
use crate::pages::TITLE;
#[derive(TemplateOnce, Clone)]
#[template(path = "panel/add-site-key/index.html")]
pub struct IndexPage<'a> {
@@ -28,13 +30,13 @@ pub struct IndexPage<'a> {
pub form_description: &'a str,
}
const TITLE: &str = "Add Site Key";
const COMPONENT: &str = "Add Site Key";
impl<'a> Default for IndexPage<'a> {
fn default() -> Self {
IndexPage {
name: "mCaptcha",
title: TITLE,
name: TITLE,
title: COMPONENT,
levels: 1,
form_description: "",
form_title: "Add Site Key",

View File

@@ -0,0 +1,44 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
use actix_web::{HttpResponse, Responder};
use sailfish::TemplateOnce;
#[derive(TemplateOnce, Clone)]
#[template(path = "panel/site-keys/index.html")]
pub struct IndexPage<'a> {
pub name: &'a str,
pub title: &'a str,
}
const TITLE: &str = "Add Site Key";
impl<'a> Default for IndexPage<'a> {
fn default() -> Self {
IndexPage {
name: "mCaptcha",
title: TITLE,
}
}
}
pub async fn list_sitekeys() -> impl Responder {
let body = IndexPage::default().render_once().unwrap();
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(body)
}

View File

@@ -16,6 +16,7 @@
*/
mod add;
mod list;
pub mod routes {
pub struct Sitekey {
@@ -43,4 +44,11 @@ pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
Methods::ProtectGet,
add::add_sitekey
);
define_resource!(
cfg,
PAGES.panel.sitekey.list,
Methods::ProtectGet,
list::list_sitekeys
);
}

View File

@@ -180,7 +180,7 @@ pub async fn add_levels_util(
name: &str,
password: &str,
) -> (data::Data, Login, ServiceResponse, MCaptchaDetails) {
let (data, creds, signin_resp, token_key) = add_token_util(name, password).await;
let (data, creds, signin_resp) = signin(name, password).await;
let cookies = get_cookie!(signin_resp);
let mut app = get_app!(data).await;
@@ -188,7 +188,6 @@ pub async fn add_levels_util(
let add_level = AddLevels {
levels: levels.clone(),
key: token_key.key.clone(),
};
// 1. add level
@@ -200,6 +199,7 @@ pub async fn add_levels_util(
)
.await;
assert_eq!(add_token_resp.status(), StatusCode::OK);
let token_key: MCaptchaDetails = test::read_body_json(add_token_resp).await;
(data, creds, signin_resp, token_key)
}