diff --git a/__pycache__/database.cpython-310.pyc b/__pycache__/database.cpython-310.pyc index 7b9825e..bea0111 100644 Binary files a/__pycache__/database.cpython-310.pyc and b/__pycache__/database.cpython-310.pyc differ diff --git a/__pycache__/mail.cpython-310.pyc b/__pycache__/mail.cpython-310.pyc new file mode 100644 index 0000000..7b7772b Binary files /dev/null and b/__pycache__/mail.cpython-310.pyc differ diff --git a/database.json b/database.json index 24754f9..33f2594 100644 --- a/database.json +++ b/database.json @@ -1 +1,14 @@ -{"users": {"1": {"camille@chauvet.pro": "b'$2b$12$AMuNu9CU/lUyaQjDmyWypeVg8beyRA795lrldMGAmHMXaeyfNnke.'"}}} \ No newline at end of file +{ + "users": { + "1": { + "email": "camille@chauvet.pro", + "password": "b'$2b$12$FVOKVF9p/SlSv9ANVNDW.eOmv1f3qtB0WU86g4ED4B8J2fMBPZu7.'" + }, + "2": { + "email": "spam@camille.chauvet.pro", + "password": "b'$2b$12$D2DexgunhMTbYWkXmEQKw.Aa13W9QZE4CjsNUyaU5bSaZ8ZpdJ2hO'" + } + }, + "resets": {}, + "_default": {} +} \ No newline at end of file diff --git a/database.py b/database.py index 71bb00c..4455776 100644 --- a/database.py +++ b/database.py @@ -1,27 +1,59 @@ -from tinydb import TinyDB, Query +from tinydb import TinyDB, where, Query +import uuid import hasher -db = TinyDB("./database.json") +db = TinyDB("./database.json", indent=4) +query = Query() + users = db.table("users"); def get_users(): return (users.all()) def get_user_by_email(email: str): - for user in get_users(): - if (list(user.keys())[0] == email): - return (user); + user_lst = users.search(query.email == email) + if (user_lst == []): + return (None) + return (user_lst[0]) + +def email_exist(email: str) -> bool: + return (get_user_by_email(email) != None) def user_exist(email: str): return (get_user_by_email(email) != None) def add_user(email: str, password: str): password_hashed = hasher.hash_text(password) - users.insert({email: str(password_hashed)}); + users.insert({"email": email, "password": str(password_hashed)}); def check_password(email: str, password: str): - password_hashed = get_user_by_email(email).get(email) + password_hashed = get_user_by_email(email).get("password") password_hashed = bytes(password_hashed[2:-1], "utf-8") return (hasher.is_same(password, password_hashed)) +def change_user_password(email: str, password: str): + password_hashed = hasher.hash_text(password) + db.update({"password": password_hashed}, query.email == email) + resets = db.table("resets") + +def get_email_by_reset_code(code: str): + user_lst = resets.search(query.code == code) + if (user_lst == []): + return (None) + return (user_lst[0]) + +def reset_code_exist(code: str) -> bool: + return (get_email_by_reset_code(code) != None) + +def remove_reset_code_by_email(email: str): + resets.remove(query.email == email) + +def remove_reset_code_by_code(code: str): + resets.remove(query.code == code) + +def create_reset_code_by_email(email: str): + code = str(uuid.uuid4()); + remove_reset_code_by_email(email); + resets.insert({"email": email, "code": code}) + return (code) diff --git a/flask_session/2029240f6d1128be89ddc32729463129 b/flask_session/2029240f6d1128be89ddc32729463129 index 8b04914..60b84f8 100644 Binary files a/flask_session/2029240f6d1128be89ddc32729463129 and b/flask_session/2029240f6d1128be89ddc32729463129 differ diff --git a/flask_session/320444fe8859cda10187fa078aaa3674 b/flask_session/320444fe8859cda10187fa078aaa3674 deleted file mode 100644 index e53970b..0000000 Binary files a/flask_session/320444fe8859cda10187fa078aaa3674 and /dev/null differ diff --git a/mail.py b/mail.py index 018f9ac..8e21ef2 100644 --- a/mail.py +++ b/mail.py @@ -1,6 +1,7 @@ import ssl import smtplib -from email.message import EmailMessage +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText config = { "server": "ssl0.ovh.net", @@ -11,11 +12,11 @@ config = { } def send_mail(mail_add:str, subject:str, mail_content:str): - email = EmailMessage() + email = MIMEMultipart() email['From'] = config["display_name"] email["To"] = mail_add; email["subject"] = subject; - email.set_content(mail_content); + email.attach(MIMEText(mail_content, "html")) context = ssl.create_default_context(); @@ -23,4 +24,3 @@ def send_mail(mail_add:str, subject:str, mail_content:str): smtp.login(config["email"], config["password"]); smtp.sendmail(config["email"], mail_add, email.as_string()); -send_mail("camille@chauvet.pro", "test", "text") diff --git a/main.py b/main.py index b1ceb49..040d22d 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ from flask import Flask, render_template, request, redirect, session from flask_session import Session import utils import database +import mail app = Flask(__name__); app.config["SESSION_PERMANENT"] = False @@ -12,7 +13,6 @@ Session(app) def home(): return (render_template("home.html")); - @app.route("/connected") def connected(): if (not session.get("email")): @@ -21,12 +21,12 @@ def connected(): @app.route("/login") def login(): + if (session.get("email")): + return (redirect("/connected")) return (render_template("login.html")); @app.route('/login', methods=['POST']) def login_post(): - if (not session.get("email")): - return (redirect("/connected")) email = request.form.get('email') password = request.form.get('password') if (not database.user_exist(email)): @@ -43,12 +43,12 @@ def logout(): @app.route("/signin") def signin(): + if (session.get("email")): + return (redirect("/connected")) return (render_template("signin.html")); @app.route('/signin', methods=['POST']) -def signup_post(): - if (not session.get("email")): - return (redirect("/connected")) +def signin_post(): email = request.form.get('email') password = request.form.get('password') repassword = request.form.get('repassword') @@ -65,9 +65,35 @@ def signup_post(): def forgot(): return (render_template("forgot.html")); +@app.route("/forgot", methods=["POST"]) +def forgot_post(): + email = request.form.get('email') + if (not database.email_exist(email)): + return (render_template("forgot.html", error="Ce compte n'existe pas'")) + uuid = database.create_reset_code_by_email(email); + mail.send_mail(email, "Password recovery", render_template("mails/password_recovery.html", code=uuid)) + return (redirect(f"/reset/{uuid}")) + @app.route("/reset/") def reset(uuid): - return ("bozo") + if (not database.reset_code_exist(uuid)): + return (render_template("error.html", error="code inconnu")) + email = database.get_email_by_reset_code(uuid) + email = email["email"] + return (render_template("reset.html", email=email)) + +@app.route("/reset/", methods=['POST']) +def reset_post(uuid): + if (not database.reset_code_exist(uuid)): + return (render_template("error.html", error="code inconnu")) + email = database.get_email_by_reset_code(uuid) + password = request.form.get('password') + repassword = request.form.get('repassword') + if (password != repassword): + return (render_template("reset.html", error="Les deux mots de passe sont differents")) + database.change_user_password(email, password); + database.remove_reset_code_by_code(uuid); + return (redirect("/login")) @app.route("/join/") def join(uuid): diff --git a/templates/connected.html b/templates/connected.html new file mode 100644 index 0000000..26e0556 --- /dev/null +++ b/templates/connected.html @@ -0,0 +1,14 @@ + + + + + PyMenu + + +

connected

+

Welcome to FlaskApp!

+ + + + + diff --git a/templates/error.html b/templates/error.html new file mode 100644 index 0000000..3f1c7c7 --- /dev/null +++ b/templates/error.html @@ -0,0 +1,16 @@ + + + + + PyMenu + + +

Error

+ {% if error %} +

{{error}}

+ {% endif %} + + + + + diff --git a/templates/forgot.html b/templates/forgot.html new file mode 100644 index 0000000..2a49a8b --- /dev/null +++ b/templates/forgot.html @@ -0,0 +1,83 @@ + + + + + + + + Beyond School + + +
+
+

Mot de passe oublié ?

+ + +
+ {% if error %} +

{{error}}

+ {% endif %} + + + + +
+
+ + + diff --git a/templates/mails/password_recovery.html b/templates/mails/password_recovery.html new file mode 100644 index 0000000..ee73186 --- /dev/null +++ b/templates/mails/password_recovery.html @@ -0,0 +1,76 @@ + + + + + + + + Beyond School + + +
+
+

Mot de passe oublié ?

+ + + +
+
+ + + diff --git a/templates/reset.html b/templates/reset.html new file mode 100644 index 0000000..a7c4fef --- /dev/null +++ b/templates/reset.html @@ -0,0 +1,83 @@ + + + + + + + + Beyond School + + +
+
+

Connection

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