|
| 1 | +""" |
| 2 | + PyMatcha - A Python Dating Website |
| 3 | + Copyright (C) 2018-2019 jlasne/gmorer |
| 4 | + <jlasne@student.42.fr> - <gmorer@student.42.fr> |
| 5 | +
|
| 6 | + This program is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | +""" |
| 19 | +import datetime |
| 20 | +import os |
| 21 | + |
| 22 | +from flask import Blueprint |
| 23 | +from flask import current_app |
| 24 | +from flask import redirect |
| 25 | +from flask import render_template |
| 26 | +from flask import request |
| 27 | +from itsdangerous import BadSignature |
| 28 | +from itsdangerous import SignatureExpired |
| 29 | +from PyMatcha.models.user import get_user |
| 30 | +from PyMatcha.utils.confirm_token import confirm_token |
| 31 | +from PyMatcha.utils.confirm_token import generate_confirmation_token |
| 32 | +from PyMatcha.utils.decorators import validate_params |
| 33 | +from PyMatcha.utils.errors import NotFoundError |
| 34 | +from PyMatcha.utils.mail import send_mail_html |
| 35 | +from PyMatcha.utils.success import Success |
| 36 | + |
| 37 | + |
| 38 | +REQUIRED_KEYS_NEW_EMAIL_CONF = {"email": str} |
| 39 | + |
| 40 | +auth_email_bp = Blueprint("auth_email", __name__) |
| 41 | + |
| 42 | + |
| 43 | +@auth_email_bp.route("/auth/confirm/<token>", methods=["GET"]) |
| 44 | +def confirm_email(token): |
| 45 | + current_app.logger.debug("/auth/confirm/{} -> Call".format(token)) |
| 46 | + try: |
| 47 | + email, token_type = confirm_token(token, expiration=7200) |
| 48 | + except (SignatureExpired, BadSignature) as e: |
| 49 | + if e == SignatureExpired: |
| 50 | + current_app.logger.debug("/auth/confirm -> Signature Expired") |
| 51 | + return redirect("/?type=confirm&success=false&message=Signature expired") |
| 52 | + else: |
| 53 | + current_app.logger.debug("/auth/confirm -> Bad Expired") |
| 54 | + return redirect("/?type=confirm&success=false&message=Bad Signature") |
| 55 | + else: |
| 56 | + if token_type != "confirm": |
| 57 | + current_app.logger.debug("/auth/confirm -> Wrong token type") |
| 58 | + return redirect("/?type=confirm&success=false&message=Wrong token type") |
| 59 | + try: |
| 60 | + u = get_user(email) |
| 61 | + except NotFoundError: |
| 62 | + current_app.logger.debug("/auth/confirm -> User not found") |
| 63 | + return redirect("/?type=confirm&success=false&message=User not found") |
| 64 | + if u.is_confirmed: |
| 65 | + current_app.logger.debug("/auth/confirm -> User already confirmed") |
| 66 | + return redirect("/?type=confirm&success=false&message=User already confirmed") |
| 67 | + else: |
| 68 | + u.is_confirmed = True |
| 69 | + u.confirmed_on = datetime.datetime.utcnow() |
| 70 | + u.save() |
| 71 | + current_app.logger.debug("/auth/confirm -> User {} confirmed.".format(u.id)) |
| 72 | + return redirect("/?type=confirm&success=true&message=User confirmed") |
| 73 | + |
| 74 | + |
| 75 | +@auth_email_bp.route("/auth/confirm/new", methods=["POST"]) |
| 76 | +@validate_params(REQUIRED_KEYS_NEW_EMAIL_CONF) |
| 77 | +def request_new_email_conf(): |
| 78 | + current_app.logger.debug("/auth/confirm/new -> Call") |
| 79 | + data = request.get_json() |
| 80 | + email = data["email"] |
| 81 | + try: |
| 82 | + u = get_user(email) |
| 83 | + except NotFoundError: |
| 84 | + current_app.logger.debug("/auth/confirm/new -> User not found") |
| 85 | + pass |
| 86 | + else: |
| 87 | + if u.is_confirmed: |
| 88 | + current_app.logger.debug("/auth/confirm/new -> User found, Already confirmed.") |
| 89 | + return Success("User already confirmed") |
| 90 | + else: |
| 91 | + current_app.logger.debug("/auth/confirm/new -> User found, sending new confirmation email") |
| 92 | + token = generate_confirmation_token(email=email, token_type="confirm") |
| 93 | + link = os.getenv("APP_URL") + "/auth/confirm/" + token |
| 94 | + rendered_html = render_template("confirm_email.html", link=link) |
| 95 | + send_mail_html.delay(dest=data["email"], subject="Confirm your email on PyMatcha", html=rendered_html) |
| 96 | + current_app.logger.debug("/auth/confirm/new -> New confirmation email sent if user exists in database") |
| 97 | + return Success("New confirmation email sent if user exists in database") |
0 commit comments