From 8f3ef5a7f08af0f13213f4546ed2cb838d176b79 Mon Sep 17 00:00:00 2001 From: Tracker-Friendly Date: Sun, 19 Nov 2023 01:06:08 +0000 Subject: [PATCH] Added CAPTCHA support --- app.py | 29 ++++++++++++++++++++++++++--- templates/captcha_err.html | 19 +++++++++++++++++++ templates/index.html | 19 +++++++++++++++---- tmp/password.tmp | 2 +- 4 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 templates/captcha_err.html diff --git a/app.py b/app.py index 5921063..51cacd4 100644 --- a/app.py +++ b/app.py @@ -1,15 +1,24 @@ -from flask import Flask, render_template, request, redirect, url_for +from flask import Flask, render_template, request, redirect, url_for, session import subprocess import re import os +import random +from captcha.image import ImageCaptcha from waitress import serve +import base64 allowed_pattern = r'^[a-zA-Z0-9.]+$' +def generate_captcha_text(): + characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + captcha_text = ''.join(random.choice(characters) for i in range(6)) + return captcha_text + def is_valid_input(input_string): return re.match(allowed_pattern, input_string) is not None app = Flask(__name__) +app.secret_key = "secret_key_here" def create_email_account(username, password): if password and is_valid_input(username): @@ -45,16 +54,30 @@ def create_email_account(username, password): @app.route('/') def index(): - return render_template('index.html') + captcha_text = generate_captcha_text() + image = ImageCaptcha().generate(captcha_text) + session['captcha_text'] = captcha_text + image_base64 = base64.b64encode(image.getvalue()).decode('utf-8') + print(captcha_text) + return render_template('index.html', captcha_text=captcha_text, captcha_image=image_base64) @app.route('/api', methods=['POST']) def register(): username = request.form.get('username') password = request.form.get('password') + user_captcha = request.form.get('captcha') - if not is_valid_input(username): + print(user_captcha) + + if not is_valid_input(username) or not is_valid_input(user_captcha): return render_template('num.html'), 400 + # Validate the captcha + captcha_text = session.get('captcha_text', '') + print(captcha_text) + if user_captcha.lower() != captcha_text.lower(): + return render_template('captcha_err.html'), 400 + if create_email_account(username, password): return render_template('ok.html') else: diff --git a/templates/captcha_err.html b/templates/captcha_err.html new file mode 100644 index 0000000..f74ea38 --- /dev/null +++ b/templates/captcha_err.html @@ -0,0 +1,19 @@ + + + + Error! + + + +
+ HectaMail + Sign up +
+
+

Error!

+ Incorrect CAPTCHA! +
+ +
+
+ diff --git a/templates/index.html b/templates/index.html index 3170920..9e8192f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,8 +1,8 @@ - Error! - + Sign Up +
@@ -17,14 +17,25 @@

-
+
+
+ +
+ +
+ Captcha +
+
+ +
+

- + diff --git a/tmp/password.tmp b/tmp/password.tmp index 5e5cb99..30d74d2 100644 --- a/tmp/password.tmp +++ b/tmp/password.tmp @@ -1 +1 @@ -aaaaaaaaa44545 \ No newline at end of file +test \ No newline at end of file