using custom version of actix-codegen and cors for pow routes

This commit is contained in:
realaravinth
2021-05-25 14:34:24 +05:30
parent 0421cb681c
commit 72667bd2e1
26 changed files with 334 additions and 363 deletions

View File

@@ -22,6 +22,7 @@ use super::auth::Password;
use crate::errors::*;
use crate::Data;
#[my_codegen::post(path="crate::V1_API_ROUTES.account.delete", wrap="crate::CheckLogin")]
async fn delete_account(
id: Identity,
payload: web::Json<Password>,
@@ -59,13 +60,14 @@ async fn delete_account(
}
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
);
cfg.service(delete_account);
// use crate::define_resource;
// use crate::V1_API_ROUTES;
//
// define_resource!(
// cfg,
// V1_API_ROUTES.account.delete,
// Methods::ProtectPost,
// delete_account
// );
}

View File

@@ -29,6 +29,7 @@ pub struct Email {
pub email: String,
}
#[my_codegen::post(path="crate::V1_API_ROUTES.account.email_exists")]
pub async fn email_exists(
payload: web::Json<AccountCheckPayload>,
data: web::Data<Data>,
@@ -52,6 +53,7 @@ pub async fn email_exists(
}
/// update email
#[my_codegen::post(path="crate::V1_API_ROUTES.account.update_email", wrap="crate::CheckLogin")]
async fn set_email(
id: Identity,
payload: web::Json<Email>,
@@ -84,20 +86,22 @@ async fn set_email(
}
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
);
cfg.service(email_exists);
cfg.service(set_email);
// 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
// );
}

View File

@@ -29,6 +29,7 @@ pub struct Secret {
pub secret: String,
}
#[my_codegen::get(path="crate::V1_API_ROUTES.account.get_secret", wrap="crate::CheckLogin")]
async fn get_secret(
id: Identity,
data: web::Data<Data>,
@@ -46,6 +47,7 @@ async fn get_secret(
Ok(HttpResponse::Ok().json(secret))
}
#[my_codegen::post(path="crate::V1_API_ROUTES.account.update_secret", wrap="crate::CheckLogin")]
async fn update_user_secret(
id: Identity,
data: web::Data<Data>,
@@ -82,20 +84,6 @@ async fn update_user_secret(
}
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
);
cfg.service(get_secret);
cfg.service(update_user_secret);
}

View File

@@ -20,6 +20,7 @@ use super::{AccountCheckPayload, AccountCheckResp};
use crate::errors::*;
use crate::Data;
#[my_codegen::post(path="crate::V1_API_ROUTES.account.username_exists")]
async fn username_exists(
payload: web::Json<AccountCheckPayload>,
data: web::Data<Data>,
@@ -43,13 +44,5 @@ async fn username_exists(
}
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
);
cfg.service(username_exists);
}

View File

@@ -25,9 +25,6 @@ use serde::{Deserialize, Serialize};
use super::mcaptcha::get_random;
use crate::errors::*;
use crate::Data;
use crate::*;
pub const AUTH: routes::Auth = routes::Auth::new();
pub mod routes {
pub struct Auth {
@@ -50,16 +47,17 @@ pub mod routes {
}
}
//post!(V1_API_ROUTES.auth.register, signup);
pub fn services(cfg: &mut web::ServiceConfig) {
// protect_get!(cfg, V1_API_ROUTES.auth.logout, signout);
//cfg.service(signup);
cfg.service(signup);
cfg.service(signin);
cfg.service(signout);
define_resource!(cfg, V1_API_ROUTES.auth.register, Methods::Post, signup);
define_resource!(cfg, V1_API_ROUTES.auth.logout, Methods::ProtectGet, signout);
define_resource!(cfg, V1_API_ROUTES.auth.login, Methods::Post, signin);
// define_resource!(cfg, V1_API_ROUTES.auth.register, Methods::Post, signup);
// define_resource!(cfg, V1_API_ROUTES.auth.logout, Methods::ProtectGet, signout);
// define_resource!(cfg, V1_API_ROUTES.auth.login, Methods::Post, signin);
//post!(cfg, V1_API_ROUTES.auth.login, signin);
}
@@ -82,6 +80,7 @@ pub struct Password {
pub password: String,
}
#[my_codegen::post(path="crate::V1_API_ROUTES.auth.register")]
async fn signup(
payload: web::Json<Register>,
data: web::Data<Data>,
@@ -127,8 +126,7 @@ async fn signup(
.execute(&data.db)
.await;
}
if res.is_ok() {
break;
if res.is_ok() { break;
} else {
if let Err(sqlx::Error::Database(err)) = res {
if err.code() == Some(Cow::from("23505")) {
@@ -149,6 +147,7 @@ async fn signup(
Ok(HttpResponse::Ok())
}
#[my_codegen::post(path="crate::V1_API_ROUTES.auth.login")]
async fn signin(
id: Identity,
payload: web::Json<Login>,
@@ -180,6 +179,7 @@ async fn signin(
}
}
#[my_codegen::get(path="crate::V1_API_ROUTES.auth.logout", wrap="crate::CheckLogin")]
async fn signout(id: Identity) -> impl Responder {
if let Some(_) = id.identity() {
id.forget();

View File

@@ -44,6 +44,7 @@ pub struct UpdateDuration {
pub duration: i32,
}
#[my_codegen::post(path="crate::V1_API_ROUTES.duration.update", wrap="crate::CheckLogin")]
async fn update_duration(
payload: web::Json<UpdateDuration>,
data: web::Data<Data>,
@@ -81,6 +82,7 @@ pub struct GetDuration {
pub token: String,
}
#[my_codegen::post(path="crate::V1_API_ROUTES.duration.get", wrap="crate::CheckLogin")]
async fn get_duration(
payload: web::Json<MCaptchaDetails>,
data: web::Data<Data>,
@@ -101,21 +103,23 @@ async fn get_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.service(get_duration);
cfg.service(update_duration);
// 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)]

View File

@@ -59,39 +59,15 @@ pub struct AddLevels {
}
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
);
cfg.service(add_levels);
cfg.service(update_levels);
cfg.service(delete_levels);
cfg.service(get_levels);
}
// TODO redo mcaptcha table to include levels as json field
// so that the whole thing can be added/udpaed in a single stroke
#[my_codegen::post(path="crate::V1_API_ROUTES.levels.add", wrap="crate::CheckLogin")]
async fn add_levels(
payload: web::Json<AddLevels>,
data: web::Data<Data>,
@@ -144,6 +120,7 @@ pub struct UpdateLevels {
pub key: String,
}
#[my_codegen::post(path="crate::V1_API_ROUTES.levels.update", wrap="crate::CheckLogin")]
async fn update_levels(
payload: web::Json<UpdateLevels>,
data: web::Data<Data>,
@@ -201,6 +178,7 @@ async fn update_levels(
Ok(HttpResponse::Ok())
}
#[my_codegen::post(path="crate::V1_API_ROUTES.levels.delete", wrap="crate::CheckLogin")]
async fn delete_levels(
payload: web::Json<UpdateLevels>,
data: web::Data<Data>,
@@ -227,6 +205,7 @@ async fn delete_levels(
Ok(HttpResponse::Ok())
}
#[my_codegen::post(path="crate::V1_API_ROUTES.levels.get", wrap="crate::CheckLogin")]
async fn get_levels(
payload: web::Json<MCaptchaDetails>,
data: web::Data<Data>,

View File

@@ -43,29 +43,9 @@ 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.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
);
cfg.service(update_token);
cfg.service(delete_mcaptcha);
cfg.service(get_token);
}
#[derive(Clone, Debug, Deserialize, Serialize)]
@@ -131,6 +111,7 @@ pub async fn add_mcaptcha_util(
Ok(resp)
}
#[my_codegen::post(path="crate::V1_API_ROUTES.mcaptcha.update_key", wrap="crate::CheckLogin")]
async fn update_token(
payload: web::Json<MCaptchaDetails>,
data: web::Data<Data>,
@@ -181,6 +162,7 @@ async fn update_token_helper(
Ok(())
}
#[my_codegen::post(path="crate::V1_API_ROUTES.mcaptcha.get_token", wrap="crate::CheckLogin")]
async fn get_token(
payload: web::Json<MCaptchaDetails>,
data: web::Data<Data>,
@@ -208,6 +190,7 @@ async fn get_token(
Ok(HttpResponse::Ok().json(res))
}
#[my_codegen::post(path="crate::V1_API_ROUTES.mcaptcha.delete", wrap="crate::CheckLogin")]
async fn delete_mcaptcha(
payload: web::Json<MCaptchaDetails>,
data: web::Data<Data>,

View File

@@ -45,6 +45,7 @@ pub mod routes {
}
/// emmits build details of the bninary
#[my_codegen::get(path="crate::V1_API_ROUTES.meta.build_details")]
async fn build_details() -> impl Responder {
let build = BuildDetails {
version: VERSION,
@@ -60,6 +61,7 @@ pub struct Health {
}
/// checks all components of the system
#[my_codegen::get(path="crate::V1_API_ROUTES.meta.health")]
async fn health(data: web::Data<Data>) -> impl Responder {
use sqlx::Connection;
@@ -75,16 +77,8 @@ async fn health(data: web::Data<Data>) -> impl Responder {
}
pub fn services(cfg: &mut web::ServiceConfig) {
use crate::define_resource;
use crate::V1_API_ROUTES;
define_resource!(
cfg,
V1_API_ROUTES.meta.build_details,
Methods::Get,
build_details
);
define_resource!(cfg, V1_API_ROUTES.meta.health, Methods::Get, health);
cfg.service(build_details);
cfg.service(health);
}
#[cfg(test)]

View File

@@ -30,6 +30,7 @@ pub struct AddNotification {
}
/// route handler that adds a notification message
#[my_codegen::post(path="crate::V1_API_ROUTES.notifications.add", wrap="crate::CheckLogin")]
pub async fn add_notification(
payload: web::Json<AddNotification>,
data: web::Data<Data>,

View File

@@ -52,6 +52,7 @@ impl From<Notification> for NotificationResp {
}
}
/// route handler that gets all unread notifications
#[my_codegen::get(path="crate::V1_API_ROUTES.notifications.get", wrap="crate::CheckLogin")]
pub async fn get_notification(
data: web::Data<Data>,
id: Identity,

View File

@@ -38,20 +38,6 @@ pub mod routes {
}
pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
use crate::define_resource;
use crate::V1_API_ROUTES;
define_resource!(
cfg,
V1_API_ROUTES.notifications.add,
Methods::ProtectPost,
add::add_notification
);
define_resource!(
cfg,
V1_API_ROUTES.notifications.get,
Methods::ProtectGet,
get::get_notification
);
cfg.service(add::add_notification);
cfg.service(get::get_notification);
}

View File

@@ -27,6 +27,7 @@ use super::GetDurationResp;
use super::I32Levels;
use crate::errors::*;
use crate::Data;
use crate::V1_API_ROUTES;
//#[derive(Clone, Debug, Deserialize, Serialize)]
//pub struct PoWConfig {
@@ -42,6 +43,7 @@ pub struct GetConfigPayload {
// API keys are mcaptcha actor names
/// get PoW configuration for an mcaptcha key
#[my_codegen::post(path = "V1_API_ROUTES.pow.get_config.strip_prefix(V1_API_ROUTES.pow.scope).unwrap()")]
pub async fn get_config(
payload: web::Json<GetConfigPayload>,
data: web::Data<Data>,
@@ -80,6 +82,7 @@ pub async fn get_config(
Some(false) => Err(ServiceError::TokenNotFound),
None => Err(ServiceError::TokenNotFound),
}
}
/// Call this when [MCaptcha][m_captcha::MCaptcha] is not in master.
///

View File

@@ -15,7 +15,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use actix_cors::Cors;
use actix_web::web;
use actix_web::*;
@@ -28,28 +27,19 @@ pub use super::mcaptcha::levels::I32Levels;
use crate::api::v1::mcaptcha::stats::*;
pub fn services(cfg: &mut web::ServiceConfig) {
use crate::define_resource;
use crate::V1_API_ROUTES;
let cors = actix_cors::Cors::default()
.allow_any_origin()
.allowed_methods(vec!["POST"])
.allow_any_header()
.max_age(3600)
.send_wildcard();
define_resource!(
cfg,
V1_API_ROUTES.pow.verify_pow,
Methods::CorsAllowAllPost,
verify_pow::verify_pow
);
define_resource!(
cfg,
V1_API_ROUTES.pow.get_config,
Methods::CorsAllowAllPost,
get_config::get_config
);
define_resource!(
cfg,
V1_API_ROUTES.pow.validate_captcha_token,
Methods::CorsAllowAllPost,
verify_token::validate_captcha_token
cfg.service(
Scope::new(crate::V1_API_ROUTES.pow.scope)
.wrap(cors)
.service(verify_pow::verify_pow)
.service(get_config::get_config)
.service(verify_token::validate_captcha_token),
);
}
@@ -58,30 +48,48 @@ pub mod routes {
pub get_config: &'static str,
pub verify_pow: &'static str,
pub validate_captcha_token: &'static str,
pub scope: &'static str,
}
impl PoW {
pub const fn new() -> Self {
let scope = "/api/v1/pow/";
PoW {
get_config: "/api/v1/pow/config",
verify_pow: "/api/v1/pow/verify",
validate_captcha_token: "/api/v1/pow/siteverify",
scope,
}
}
}
}
#[allow(non_camel_case_types, missing_docs)]
pub struct post;
impl actix_web::dev::HttpServiceFactory for post {
fn register(self, __config: &mut actix_web::dev::AppService) {
async fn post() -> impl Responder {
HttpResponse::Ok()
}
let __resource = actix_web::Resource::new("/test/post")
.guard(actix_web::guard::Post())
.to(post);
actix_web::dev::HttpServiceFactory::register(__resource, __config)
//#[allow(non_camel_case_types, missing_docs)]
//pub struct post;
//impl actix_web::dev::HttpServiceFactory for post {
// fn register(self, __config: &mut actix_web::dev::AppService) {
// async fn post() -> impl Responder {
// HttpResponse::Ok()
// }
// let __resource = actix_web::Resource::new("/test/post")
// .guard(actix_web::guard::Post())
// .to(post);
// actix_web::dev::HttpServiceFactory::register(__resource, __config)
// }
//}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scope_pow_works() {
let pow = routes::PoW::new();
assert_eq!(pow.get_config.strip_prefix(pow.scope).unwrap(), "config");
assert_eq!(pow.verify_pow.strip_prefix(pow.scope).unwrap(), "verify");
assert_eq!(
pow.validate_captcha_token.strip_prefix(pow.scope).unwrap(),
"siteverify"
);
}
}

View File

@@ -23,6 +23,7 @@ use serde::{Deserialize, Serialize};
use super::record_solve;
use crate::errors::*;
use crate::Data;
use crate::V1_API_ROUTES;
#[derive(Clone, Debug, Deserialize, Serialize)]
/// validation token that clients receive as proof for submiting
@@ -35,6 +36,7 @@ pub struct ValidationToken {
/// route handler that verifies PoW and issues a solution token
/// if verification is successful
#[my_codegen::post(path = "V1_API_ROUTES.pow.verify_pow.strip_prefix(V1_API_ROUTES.pow.scope).unwrap()")]
pub async fn verify_pow(
payload: web::Json<Work>,
data: web::Data<Data>,

View File

@@ -23,6 +23,7 @@ use serde::{Deserialize, Serialize};
use super::record_confirm;
use crate::errors::*;
use crate::Data;
use crate::V1_API_ROUTES;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CaptchaValidateResp {
@@ -32,6 +33,7 @@ pub struct CaptchaValidateResp {
// API keys are mcaptcha actor names
/// route hander that validates a PoW solution token
#[my_codegen::post(path = "V1_API_ROUTES.pow.validate_captcha_token.strip_prefix(V1_API_ROUTES.pow.scope).unwrap()")]
pub async fn validate_captcha_token(
payload: web::Json<VerifyCaptchaResult>,
data: web::Data<Data>,