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 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:

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>
<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') }}">
<title>Sign Up</title>
<link rel="stylesheet" href="/static/css/main.css">
</head>
<body>
<div class="headerbar">
@ -17,14 +17,25 @@
<input type="text" name="username" required="">
</div>
<br>
<div class="pswdbox">
<div class="Password">
<label for="password">Password</label>
<div class="spacer">
<input type="password" name="password" required="">
</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>
<input type="submit" value="Register">
</form>
</div>
</body><style type="text/css"></style></html>
<style type="text/css"></style></body></html>

View File

@ -1 +1 @@
aaaaaaaaa44545
test