80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
import secrets
|
||
import datetime
|
||
from flask import Flask, render_template, request, jsonify, send_file
|
||
from weasyprint import HTML
|
||
import io
|
||
|
||
app = Flask(__name__)
|
||
|
||
def generate_secure_password():
|
||
# Length: randomly between 12 and 16 characters
|
||
length = secrets.choice(range(12, 17))
|
||
|
||
# Allowed Characters
|
||
upper = "ABCDEFGHJKMNPQRSTUVWXYZ" # without I, L, O
|
||
lower = "abcdefghjkmnpqrstuvwxyz" # without i, l, o
|
||
digits = "23456789" # without 0, 1
|
||
special = "!§$%&/()=?\\+#*-_,;.:@€"
|
||
|
||
while True:
|
||
password = ''.join(secrets.choice(upper + lower + digits + special) for _ in range(length))
|
||
|
||
# Minimum 2 characters from each category
|
||
if (sum(c in upper for c in password) >= 2 and
|
||
sum(c in lower for c in password) >= 2 and
|
||
sum(c in digits for c in password) >= 2 and
|
||
sum(c in special for c in password) >= 2):
|
||
|
||
# Double check that no forbidden characters somehow made it in
|
||
forbidden = "ILOilo01ÄÖÜäöü<>|~´`'^°\""
|
||
if not any(c in forbidden for c in password):
|
||
return password
|
||
|
||
@app.route('/')
|
||
def index():
|
||
return render_template('index.html')
|
||
|
||
@app.route('/api/generate_password', methods=['GET'])
|
||
def api_generate_password():
|
||
password = generate_secure_password()
|
||
return jsonify({"password": password})
|
||
|
||
@app.route('/download_pdf', methods=['POST'])
|
||
def download_pdf():
|
||
vorname = request.form.get('vorname', '')
|
||
nachname = request.form.get('nachname', '')
|
||
benutzername = request.form.get('benutzername', '')
|
||
passwort = request.form.get('passwort', '')
|
||
|
||
datum = datetime.datetime.now().strftime("%d.%m.%Y")
|
||
|
||
# Render template
|
||
html_out = render_template(
|
||
'benutzer_template.html',
|
||
VORNAME=vorname,
|
||
NACHNAME=nachname,
|
||
BENUTZERNAME=benutzername,
|
||
PASSWORT=passwort,
|
||
DATUM=datum
|
||
)
|
||
|
||
# Generate PDF
|
||
pdf_file = io.BytesIO()
|
||
HTML(string=html_out).write_pdf(pdf_file)
|
||
pdf_file.seek(0)
|
||
|
||
# Format filename safely
|
||
safe_vorname = vorname.replace(' ', '_')
|
||
safe_nachname = nachname.replace(' ', '_')
|
||
filename = f"Benutzerantrag_{safe_vorname}_{safe_nachname}.pdf"
|
||
|
||
return send_file(
|
||
pdf_file,
|
||
download_name=filename,
|
||
as_attachment=True,
|
||
mimetype='application/pdf'
|
||
)
|
||
|
||
if __name__ == '__main__':
|
||
app.run(host='0.0.0.0', port=5000, debug=True)
|