Added CAPTCHA support

This commit is contained in:
Tracker-Friendly 2023-11-19 01:06:08 +00:00
parent 936ac564d7
commit 8f3ef5a7f0
4 changed files with 61 additions and 8 deletions

29
app.py
View File

@ -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 subprocess
import re import re
import os import os
import random
from captcha.image import ImageCaptcha
from waitress import serve from waitress import serve
import base64
allowed_pattern = r'^[a-zA-Z0-9.]+$' 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): def is_valid_input(input_string):
return re.match(allowed_pattern, input_string) is not None return re.match(allowed_pattern, input_string) is not None
app = Flask(__name__) app = Flask(__name__)
app.secret_key = "secret_key_here"
def create_email_account(username, password): def create_email_account(username, password):
if password and is_valid_input(username): if password and is_valid_input(username):
@ -45,16 +54,30 @@ def create_email_account(username, password):
@app.route('/') @app.route('/')
def index(): 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']) @app.route('/api', methods=['POST'])
def register(): def register():
username = request.form.get('username') username = request.form.get('username')
password = request.form.get('password') 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 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): if create_email_account(username, password):
return render_template('ok.html') return render_template('ok.html')
else: else:

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Error!</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
</head>
<body>
<div class="headerbar">
<a href="/">HectaMail</a>
<a class="main" href="/register">Sign up</a>
</div>
<div class="content">
<h1>Error!</h1>
<p1>Incorrect CAPTCHA!</p1>
<div class=spacer2>
<button onclick="history.back()">Go Back</button>
</div>
</div>
</body><style type="text/css"></style></html>

View File

@ -1,8 +1,8 @@
<!DOCTYPE html> <!DOCTYPE html>
<html><head> <html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Error!</title> <title>Sign Up</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}"> <link rel="stylesheet" href="/static/css/main.css">
</head> </head>
<body> <body>
<div class="headerbar"> <div class="headerbar">
@ -17,14 +17,25 @@
<input type="text" name="username" required=""> <input type="text" name="username" required="">
</div> </div>
<br> <br>
<div class="pswdbox"> <div class="Password">
<label for="password">Password</label> <label for="password">Password</label>
<div class="spacer"> <div class="spacer">
<input type="password" name="password" required=""> <input type="password" name="password" required="">
</div> </div>
</div> </div>
<br>
<div class="spacer">
<label for="password">CAPTCHA</label>
<div class="spacer">
<img src="data:image/png;base64,{{ captcha_image }}" alt="Captcha">
</div>
<div class="spacer">
<input required="" name="captcha" type="text">
</div>
</div>
<br> <br>
<input type="submit" value="Register"> <input type="submit" value="Register">
</form> </form>
</div> </div>
</body><style type="text/css"></style></html> <style type="text/css"></style></body></html>

View File

@ -1 +1 @@
aaaaaaaaa44545 test