Skip to content

Commit d4dead2

Browse files
committed
Moved auth routes to folder in separate files
1 parent 3ca6e1b commit d4dead2

File tree

7 files changed

+367
-260
lines changed

7 files changed

+367
-260
lines changed

backend/PyMatcha/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,20 @@ def check_if_token_is_revoked(decrypted_token):
187187

188188
from PyMatcha.routes.api.ping_pong import ping_pong_bp
189189
from PyMatcha.routes.api.user import user_bp
190-
from PyMatcha.routes.api.auth import auth_bp
190+
from PyMatcha.routes.api.auth.email import auth_email_bp
191+
from PyMatcha.routes.api.auth.password import auth_password_bp
192+
from PyMatcha.routes.api.auth.register import auth_register_bp
193+
from PyMatcha.routes.api.auth.login import auth_login_bp
191194
from PyMatcha.routes.api.profile import profile_bp
192195
from PyMatcha.routes.api.like import like_bp
193196

194197
logging.debug("Registering Flask blueprints")
195198
application.register_blueprint(ping_pong_bp)
196199
application.register_blueprint(user_bp)
197-
application.register_blueprint(auth_bp)
200+
application.register_blueprint(auth_email_bp)
201+
application.register_blueprint(auth_password_bp)
202+
application.register_blueprint(auth_register_bp)
203+
application.register_blueprint(auth_login_bp)
198204
application.register_blueprint(profile_bp)
199205
application.register_blueprint(like_bp)
200206

backend/PyMatcha/routes/api/auth.py

Lines changed: 0 additions & 258 deletions
This file was deleted.

backend/PyMatcha/routes/api/auth/__init__.py

Whitespace-only changes.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)