widget: verification works

This commit is contained in:
realaravinth 2021-05-29 21:19:45 +05:30
parent d9cb38ac13
commit 98cf4a476d
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
13 changed files with 188 additions and 39 deletions

View file

@ -159,7 +159,7 @@ export default {
// testRunner: "jasmine2",
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
testURL: 'http://localhost:7000',
testURL: 'http://localhost:7000/?sitekey=imbatman',
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
// timers: "real",

View file

@ -12,6 +12,7 @@
"@types/jsdom": "^16.2.10",
"@types/node": "^15.0.2",
"@types/sinon": "^10.0.0",
"@wasm-tool/wasm-pack-plugin": "^1.4.0",
"css-loader": "^5.2.4",
"css-minimizer-webpack-plugin": "^2.0.0",
"dart-sass": "^1.25.0",

View file

@ -43,15 +43,17 @@ struct Level {
struct IndexPage {
duration: u32,
name: String,
key: String,
levels: Vec<Level>,
}
impl IndexPage {
fn new(config: McaptchaConfig, levels: Vec<Level>) -> Self {
fn new(config: McaptchaConfig, levels: Vec<Level>, key: String) -> Self {
IndexPage {
duration: config.duration as u32,
name: config.name,
levels,
key,
}
}
}
@ -91,7 +93,7 @@ pub async fn view_sitekey(
let (stats, levels) = try_join!(Stats::new(&key, &data.db), levels_fut)?;
let body = IndexPage::new(config, levels).render_once().unwrap();
let body = IndexPage::new(config, levels, key).render_once().unwrap();
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(body))

View file

@ -46,6 +46,20 @@ macro_rules! post_request {
};
}
#[macro_export]
macro_rules! get_works {
($app:expr,$route:expr ) => {
let list_sitekey_resp = test::call_service(
&mut $app,
test::TestRequest::get()
.uri($route)
.to_request(),
)
.await;
assert_eq!(list_sitekey_resp.status(), StatusCode::OK);
};
}
#[macro_export]
macro_rules! get_app {
() => {

View file

@ -14,27 +14,41 @@
* 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 std::borrow::Cow;
use actix_web::body::Body;
use actix_web::{get, http::header, web, HttpResponse, Responder};
use mime_guess::from_path;
use rust_embed::RustEmbed;
use lazy_static::lazy_static;
use sailfish::TemplateOnce;
use crate::errors::*;
pub const WIDGET_ROUTES: routes::Widget = routes::Widget::new();
pub mod routes {
pub struct Widget {
pub verification_widget: &'static str,
pub js: &'static str,
pub wasm: &'static str,
}
impl Widget {
pub const fn new() -> Self {
Widget { verification_widget: "/widget" }
Widget {
verification_widget: "/widget",
js: "/widget/bundle.js",
wasm: "/widget/1476099975f2b060264c.module.wasm",
}
}
}
}
use actix_web::{web, HttpResponse, Responder};
use lazy_static::lazy_static;
use sailfish::TemplateOnce;
use crate::errors::*;
#[derive(TemplateOnce, Clone)]
#[template(path = "widget/index.html")]
@ -60,14 +74,42 @@ async fn show_widget() -> PageResult<impl Responder> {
.body(&*INDEX_PAGE))
}
#[derive(RustEmbed)]
#[folder = "static/widget/"]
struct WidgetAssets;
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(show_widget);
fn handle_widget_assets(path: &str) -> HttpResponse {
match WidgetAssets::get(path) {
Some(content) => {
let body: Body = match content {
Cow::Borrowed(bytes) => bytes.into(),
Cow::Owned(bytes) => bytes.into(),
};
HttpResponse::Ok()
.set(header::CacheControl(vec![header::CacheDirective::MaxAge(
crate::CACHE_AGE,
)]))
.content_type(from_path(path).first_or_octet_stream().as_ref())
.body(body)
}
None => HttpResponse::NotFound().body("404 Not Found"),
}
}
#[get("/widget/{_:.*}")]
pub async fn widget_assets(path: web::Path<String>) -> impl Responder {
handle_widget_assets(&path.0)
}
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(show_widget);
cfg.service(widget_assets);
}
#[cfg(test)]
mod test {
use actix_web::http::StatusCode;
@ -77,18 +119,20 @@ mod test {
#[actix_rt::test]
async fn captcha_widget_route_works() {
let mut app = get_app!().await;
// let list_sitekey_resp = test::call_service(
// &mut app,
// test::TestRequest::get()
// .uri(crate::WIDGET_ROUTES.verification_widget)
// .to_request(),
// )
// .await;
// assert_eq!(list_sitekey_resp.status(), StatusCode::OK);
let list_sitekey_resp = test::call_service(
&mut app,
test::TestRequest::get()
.uri(crate::WIDGET_ROUTES.verification_widget)
.to_request(),
)
.await;
assert_eq!(list_sitekey_resp.status(), StatusCode::OK);
get_works!(app, crate::WIDGET_ROUTES.verification_widget);
get_works!(app, crate::WIDGET_ROUTES.js);
get_works!(app, crate::WIDGET_ROUTES.wasm);
}
}

Binary file not shown.

1
static/widget/bundle.js Normal file

File diff suppressed because one or more lines are too long

View file

@ -1,4 +1,5 @@
<. include!("../../../components/headers/index.html"); .>
<. include!("../../../components/headers/widget-headers.html"); .>
<body class="layout">
<. include!("../../navbar/index.html"); .>
<div class="tmp-layout">
<. include!("../../header/index.html"); .>
@ -10,7 +11,11 @@
<form class="sitekey-form" action="<.= crate::V1_API_ROUTES.levels.add .>" method="post">
<h1 class="form__title">
Sitekey: <.= name .>
Sitekey: <.= name .>
<a href="<.= crate::WIDGET_ROUTES.verification_widget .>/?sitekey=<.= key.>"
>Click here to see CAPTCHA widget in action</a>
</h1>
<label class="sitekey-form__label" for="description">
Description
@ -47,6 +52,8 @@
<. } .>
</form>
</div>
<!-- end of container -->
<. include!("../../../components/footers.html"); .>

View file

@ -5,5 +5,7 @@
href="<.= &*crate::VERIFICATIN_WIDGET_CSS .>"
/>
<script src="<.= &*crate::VERIFICATIN_WIDGET_JS .>"></script>
<script src="<.= &*crate::WIDGET_ROUTES.js .>"></script>
<script src="<.= &*crate::WIDGET_ROUTES.wasm .>"></script>
</body>
</html>

View file

@ -15,11 +15,41 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import './main.scss';
const PARENT = window.parent;
const verificationContainer = <HTMLElement>(
document.querySelector('.widget__verification-container')
);
verificationContainer.style.display = 'flex';
//PARENT.postMessage
//import prove from './runner/prove';
//import fetchPoWConfig from './runner/fetchPoWConfig';
//import sendWork from './runner/sendWork';
//import sendToParent from './runner/sendToParent';
//import * as CONST from './runner/const';
//
///** add mcaptcha widget element to DOM */
//export const register = () => {
// const verificationContainer = <HTMLElement>(
// document.querySelector('.widget__verification-container')
// );
// verificationContainer.style.display = 'flex';
//
// CONST.btn().addEventListener('click', e => solveCaptchaRunner(e));
//};
//
//const solveCaptchaRunner = async (e: Event) => {
// e.preventDefault();
// // steps:
//
// // 1. hide --before message
// CONST.messageText().before().style.display = 'none';
//
// // 1. show --during
// CONST.messageText().during().style.display = 'block';
// // 1. get config
// const config = await fetchPoWConfig();
// // 2. prove work
// const proof = await prove(config);
// // 3. submit work
// const token = await sendWork(proof);
// // 4. send token
// sendToParent(token);
// // 5. mark checkbox checked
// CONST.btn().checked = true;
//};
//
//register();

View file

@ -83,7 +83,7 @@
}
.widget__verification-checkbox:checked ~ .widget__verification-text--during {
display: non;
display: none;
}
.widget__verification-checkbox:checked ~ .widget__verification-text--error {

View file

@ -2,13 +2,14 @@
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
//const WasmPackPlugin = require('@wasm-tool/wasm-pack-plugin');
module.exports = {
devtool: 'inline-source-map',
mode: 'development',
//mode: 'production',
entry: {
bundle: './templates/index.ts',
bundle: './templates/index.ts',
mobile: './templates/mobile.ts',
verificationWidget: './templates/widget/index.ts',
},
@ -36,14 +37,19 @@ module.exports = {
},
],
},
],
},
resolve: {
extensions: ['.ts', '.tsx', '.js'],
},
plugins: [new MiniCssExtractPlugin()],
plugins: [
new MiniCssExtractPlugin(),
// new WasmPackPlugin({
// crateDirectory: __dirname,
// outName: "pow.wasm",
// }),
],
optimization: {
minimizer: [
// For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
@ -51,4 +57,13 @@ module.exports = {
new CssMinimizerPlugin(),
],
},
// experiments: {
// // executeModule: true,
// // outputModule: true,
// //syncWebAssembly: true,
// // topLevelAwait: true,
// asyncWebAssembly: true,
// // layers: true,
// // lazyCompilation: true,
// },
};

View file

@ -696,6 +696,16 @@
dependencies:
"@types/yargs-parser" "*"
"@wasm-tool/wasm-pack-plugin@^1.4.0":
version "1.4.0"
resolved "https://registry.yarnpkg.com/@wasm-tool/wasm-pack-plugin/-/wasm-pack-plugin-1.4.0.tgz#752e4a6d8fe35477a3c6cafd2ac6b0351f692848"
integrity sha512-zQh0gA7E73dgwhUM9sXX2rsaXsdWUIdK1kMlEhds3oi6ASn+ePxhb/quZweoeo0SjxuETVb0iu+/nxUZ5HxsUQ==
dependencies:
chalk "^2.4.1"
command-exists "^1.2.7"
watchpack "^1.6.0"
which "^2.0.2"
"@webassemblyjs/ast@1.11.0":
version "1.11.0"
resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.11.0.tgz#a5aa679efdc9e51707a4207139da57920555961f"
@ -1372,7 +1382,7 @@ caseless@~0.12.0:
resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc"
integrity sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=
chalk@^2.0.0:
chalk@^2.0.0, chalk@^2.4.1:
version "2.4.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==
@ -1394,7 +1404,7 @@ char-regex@^1.0.2:
resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-1.0.2.tgz#d744358226217f981ed58f479b1d6bcc29545dcf"
integrity sha512-kWWXztvZ5SBQV+eRgKFeh8q5sLuZY2+8WUIzlxWVTg+oGwY14qylx1KbKzHd8P6ZYkAg0xyIDU9JMHhyJMZ1jw==
"chokidar@>=2.0.0 <4.0.0":
"chokidar@>=2.0.0 <4.0.0", chokidar@^3.4.1:
version "3.5.1"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.1.tgz#ee9ce7bbebd2b79f49f304799d5468e31e14e68a"
integrity sha512-9+s+Od+W0VJJzawDma/gvBNQqkTiqYTWLuZoyAsivsI4AaWTCzHG06/TMjsf1cYe9Cb97UCEhjz7HvnPk2p/tw==
@ -1550,6 +1560,11 @@ combined-stream@^1.0.6, combined-stream@~1.0.6:
dependencies:
delayed-stream "~1.0.0"
command-exists@^1.2.7:
version "1.2.9"
resolved "https://registry.yarnpkg.com/command-exists/-/command-exists-1.2.9.tgz#c50725af3808c8ab0260fd60b01fbfa25b954f69"
integrity sha512-LTQ/SGc+s0Xc0Fu5WaKnR0YiygZkm9eKFvyS+fRsU7/ZWFF8ykFM6Pc9aCVf1+xasOOZpO3BAVgVrKvsqKHV7w==
commander@^2.20.0:
version "2.20.3"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
@ -4140,7 +4155,7 @@ negotiator@0.6.2:
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.2.tgz#feacf7ccf525a77ae9634436a64883ffeca346fb"
integrity sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==
neo-async@^2.6.2:
neo-async@^2.5.0, neo-async@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.2.tgz#b4aafb93e3aeb2d8174ca53cf163ab7d7308305f"
integrity sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==
@ -6145,6 +6160,24 @@ walker@^1.0.7, walker@~1.0.5:
dependencies:
makeerror "1.0.x"
watchpack-chokidar2@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/watchpack-chokidar2/-/watchpack-chokidar2-2.0.1.tgz#38500072ee6ece66f3769936950ea1771be1c957"
integrity sha512-nCFfBIPKr5Sh61s4LPpy1Wtfi0HE8isJ3d2Yb5/Ppw2P2B/3eVSEBjKfN0fmHJSK14+31KwMKmcrzs2GM4P0Ww==
dependencies:
chokidar "^2.1.8"
watchpack@^1.6.0:
version "1.7.5"
resolved "https://registry.yarnpkg.com/watchpack/-/watchpack-1.7.5.tgz#1267e6c55e0b9b5be44c2023aed5437a2c26c453"
integrity sha512-9P3MWk6SrKjHsGkLT2KHXdQ/9SNkyoJbabxnKOoJepsvJjJG8uYTR3yTPxPQvNDI3w4Nz1xnE0TLHK4RIVe/MQ==
dependencies:
graceful-fs "^4.1.2"
neo-async "^2.5.0"
optionalDependencies:
chokidar "^3.4.1"
watchpack-chokidar2 "^2.0.1"
watchpack@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/watchpack/-/watchpack-2.1.1.tgz#e99630550fca07df9f90a06056987baa40a689c7"