mcaptcha/widget/
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
6//! User facing CAPTCHA widget
7use actix_web::{web, HttpResponse, Responder};
8use lazy_static::lazy_static;
9use sailfish::TemplateOnce;
10
11use crate::errors::*;
12
13pub const WIDGET_ROUTES: routes::Widget = routes::Widget::new();
14
15pub mod routes {
16    pub struct Widget {
17        pub verification_widget: &'static str,
18    }
19
20    impl Widget {
21        pub const fn new() -> Self {
22            Widget {
23                verification_widget: "/widget",
24            }
25        }
26    }
27}
28
29#[derive(TemplateOnce, Clone)]
30#[template(path = "widget/index.html")]
31pub struct IndexPage;
32
33const PAGE: &str = "mCaptcha CAPTCHA verification";
34
35impl IndexPage {
36    fn new() -> Self {
37        IndexPage {}
38    }
39}
40
41lazy_static! {
42    static ref INDEX_PAGE: String = IndexPage::new().render_once().unwrap();
43}
44
45/// render a client side widget for CAPTCHA verification
46#[my_codegen::get(path = "crate::WIDGET_ROUTES.verification_widget")] //, wrap = "crate::CheckLogin")]
47async fn show_widget() -> PageResult<impl Responder> {
48    Ok(HttpResponse::Ok()
49        .content_type("text/html; charset=utf-8")
50        .body(&**INDEX_PAGE))
51}
52
53/// widget services
54pub fn services(cfg: &mut web::ServiceConfig) {
55    cfg.service(show_widget);
56}
57
58#[cfg(test)]
59mod test {
60    use actix_web::http::StatusCode;
61    use actix_web::test;
62
63    use crate::*;
64
65    #[actix_rt::test]
66    async fn captcha_widget_route_works() {
67        let app = get_app!().await;
68        get_works!(app, crate::WIDGET_ROUTES.verification_widget);
69    }
70}