From 51802bc4f8a18850675e15e67cd459c2102c5aec Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sun, 7 Jan 2024 23:01:12 +0530 Subject: [PATCH] feat: add python example server --- .gitignore | 133 +++++++++++++++++++++++++++++++++ python/.env_sample | 3 + python/.gitignore | 1 + python/README.md | 40 ++++++++++ python/requirements.txt | 13 ++++ python/server.py | 43 +++++++++++ python/templates/index.html.j2 | 110 +++++++++++++++++++++++++++ 7 files changed, 343 insertions(+) create mode 100644 .gitignore create mode 100644 python/.env_sample create mode 100644 python/.gitignore create mode 100644 python/README.md create mode 100644 python/requirements.txt create mode 100644 python/server.py create mode 100644 python/templates/index.html.j2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..773085b --- /dev/null +++ b/.gitignore @@ -0,0 +1,133 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional stylelint cache +.stylelintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# vuepress v2.x temp and cache directory +.temp +.cache + +# Docusaurus cache and generated files +.docusaurus + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v2 +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* +.env +.env.local +tmp/ diff --git a/python/.env_sample b/python/.env_sample new file mode 100644 index 0000000..ad0e1ce --- /dev/null +++ b/python/.env_sample @@ -0,0 +1,3 @@ +SITEKEY= +SECRET= +INSTANCE_URL= diff --git a/python/.gitignore b/python/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/python/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/python/README.md b/python/README.md new file mode 100644 index 0000000..cc5ca69 --- /dev/null +++ b/python/README.md @@ -0,0 +1,40 @@ +# Example Server with mCaptcha protection + +The example server shows a dummy form with the mCaptcha widget. When the +form is submitted, it validates the [authorization +token](https://mcaptcha.org/docs/webmasters/terminology#authorization-token) +presented by the visitor against the mCaptcha instance that the server +is configured with. + +The example server is built with the Flask webframework and uses the +[Python API library for mCaptcha](https://pypi.org/project/mcaptcha-api/0.1.0/) for validation. + +## 1. Configuration + +Before running, please configure the server: + +```bash +cp .env_sample .env +``` + +And fill in the configuration parameters in `.env` file with: + +1. [Sitekey](https://mcaptcha.org/docs/webmasters/terminology#sitekey) +2. Account secret: Available in the settings page on the mCaptcha + dashboard +3. Instance URL + +## 2. Install dependencies + +```bash +virtualenv venv && . venv/bin/activate && pip install -r ./requirements.txt +``` + +## 3. Launch server + +```bash +flask --app server run +``` + +If all configuration parameters are properly filled in, the example server must +work. diff --git a/python/requirements.txt b/python/requirements.txt new file mode 100644 index 0000000..406438a --- /dev/null +++ b/python/requirements.txt @@ -0,0 +1,13 @@ +blinker==1.7.0 +certifi==2023.11.17 +charset-normalizer==3.3.2 +click==8.1.7 +Flask==3.0.0 +idna==3.6 +itsdangerous==2.1.2 +Jinja2==3.1.2 +MarkupSafe==2.1.3 +mcaptcha_api==0.1.0 +requests==2.31.0 +urllib3==2.1.0 +Werkzeug==3.0.1 diff --git a/python/server.py b/python/server.py new file mode 100644 index 0000000..206f7d9 --- /dev/null +++ b/python/server.py @@ -0,0 +1,43 @@ +import os +from flask import Flask, render_template, request +from mcaptcha_api import MCaptcha + +app = Flask(__name__) + +sitekey = os.environ["SITEKEY"] +secret = os.environ["SECRET"] +instance_url = os.environ["INSTANCE_URL"] + +if len(sitekey) == 0: + print("please enter sitekey") + exit(-1) + + +if len(secret) == 0: + print("please enter secret") + exit(-1) + + +if len(instance_url) == 0: + print("please enter instance url") + exit(-1) + + +@app.route("/") +def index(): + return render_template( + "index.html.j2", widget_link=f"{instance_url}/widget?sitekey={sitekey}" + ) + + +mcaptcha = MCaptcha(instance_url=instance_url, sitekey=sitekey, secret=secret) + + +@app.route("/login", methods=["POST"]) +def login_page(): + username = request.form["username"] + token = request.form["mcaptcha__token"] + if mcaptcha.verify(token=token): + return "

Success. Click here to retry

" + else: + return '

Failed. Click here to retry

' diff --git a/python/templates/index.html.j2 b/python/templates/index.html.j2 new file mode 100644 index 0000000..ea05d90 --- /dev/null +++ b/python/templates/index.html.j2 @@ -0,0 +1,110 @@ + + + + + + Home | mCaptcha Example SErver + + + + + + + + +
+

Demo Form

+ + + + +
+ + + +
+ + + +