mcaptcha/api/v1/pow/
mod.rs

1// Copyright (C) 2022  Aravinth Manivannan <realaravinth@batsense.net>
2// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
3//
4// SPDX-License-Identifier: AGPL-3.0-or-later
5
6use actix_web::web;
7
8pub mod get_config;
9pub mod verify_pow;
10pub mod verify_token;
11
12pub use super::mcaptcha::get::I32Levels;
13
14pub fn services(cfg: &mut web::ServiceConfig) {
15    let cors = actix_cors::Cors::default()
16        .allow_any_origin()
17        .allowed_methods(vec!["POST", "GET"])
18        .allow_any_header()
19        .max_age(3600)
20        .send_wildcard();
21
22    let routes = crate::V1_API_ROUTES.pow;
23    cfg.service(
24        web::scope(routes.scope)
25            .wrap(cors)
26            .service(verify_pow::verify_pow)
27            .service(get_config::get_config)
28            .service(verify_token::validate_captcha_token),
29    );
30}
31
32pub mod routes {
33    pub struct PoW {
34        pub get_config: &'static str,
35        pub verify_pow: &'static str,
36        pub validate_captcha_token: &'static str,
37        pub scope: &'static str,
38    }
39
40    macro_rules! rm_scope {
41        ($name:ident) => {
42            /// remove scope for $name route
43            pub fn $name(&self) -> &str {
44                self.$name
45                    //.strip_prefix(&self.scope[..self.scope.len() - 1])
46                    .strip_prefix(self.scope)
47                    .unwrap()
48            }
49        };
50    }
51
52    impl PoW {
53        pub const fn new() -> Self {
54            // date: 2021-11-29 16:31
55            // commit: 6eb75d7
56            // route 404s when scope contained trailing slash
57            //let scope = "/api/v1/pow/";
58            let scope = "/api/v1/pow";
59            PoW {
60                get_config: "/api/v1/pow/config",
61                verify_pow: "/api/v1/pow/verify",
62                validate_captcha_token: "/api/v1/pow/siteverify",
63                scope,
64            }
65        }
66
67        rm_scope!(get_config);
68        rm_scope!(verify_pow);
69        rm_scope!(validate_captcha_token);
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::routes::PoW;
76
77    #[test]
78    fn scope_pow_works() {
79        let pow = PoW::new();
80        assert_eq!(pow.get_config(), "/config");
81        assert_eq!(pow.verify_pow(), "/verify");
82        assert_eq!(pow.validate_captcha_token(), "/siteverify");
83    }
84}