Skip to content

Commit 68f30ea

Browse files
committed
Fixed 238
1 parent a5d1fa4 commit 68f30ea

File tree

6 files changed

+72
-38
lines changed

6 files changed

+72
-38
lines changed

backend/PyMatcha/__init__.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,22 @@
6161
if item not in os.environ:
6262
raise EnvironmentError(f"{item} is not set in the server's environment or .env file. It is required.")
6363

64-
if os.getenv("ENABLE_LOGGING") == "True":
64+
from PyMatcha.utils.static import (
65+
ENABLE_LOGGING,
66+
FLASK_SECRET_KEY,
67+
CELERY_RESULT_BACKEND,
68+
CELERY_BROKER_URL,
69+
DB_NAME,
70+
DB_USER,
71+
DB_PASSWORD,
72+
DB_PORT,
73+
DB_HOST,
74+
MAIL_PASSWORD,
75+
REDIS_PORT,
76+
REDIS_HOST,
77+
)
78+
79+
if ENABLE_LOGGING == "True":
6580
setup_logging()
6681

6782
application = Flask(__name__)
@@ -71,13 +86,11 @@
7186
else:
7287
application.debug = False
7388

74-
application.secret_key = os.getenv("FLASK_SECRET_KEY")
75-
application.config.update(FLASK_SECRET_KEY=os.getenv("FLASK_SECRET_KEY"))
76-
application.config["JWT_SECRET_KEY"] = os.environ.get("FLASK_SECRET_KEY")
89+
application.secret_key = FLASK_SECRET_KEY
90+
application.config.update(FLASK_SECRET_KEY=FLASK_SECRET_KEY)
91+
application.config["JWT_SECRET_KEY"] = FLASK_SECRET_KEY
7792

7893
logging.debug("Configuring Celery Redis URLs")
79-
CELERY_BROKER_URL = os.getenv("CELERY_BROKER_URL")
80-
CELERY_RESULT_BACKEND = os.getenv("CELERY_RESULT_BACKEND")
8194
# Celery configuration
8295
application.config["CELERY_BROKER_URL"] = CELERY_BROKER_URL
8396
application.config["CELERY_RESULT_BACKEND"] = CELERY_RESULT_BACKEND
@@ -120,18 +133,13 @@ def expired_token_callback(expired_token):
120133
logging.debug("Configuring CORS")
121134
CORS(application, expose_headers="Authorization", supports_credentials=True)
122135

123-
if os.getenv("CI"):
124-
database_password = ""
125-
else:
126-
database_password = os.getenv("DB_PASSWORD")
127-
128136
logging.debug("Setting database config from environment variables")
129137
database_config = {
130-
"host": os.getenv("DB_HOST"),
131-
"port": int(os.getenv("DB_PORT")),
132-
"user": os.getenv("DB_USER"),
133-
"password": database_password,
134-
"db": os.getenv("DB_NAME"),
138+
"host": DB_HOST,
139+
"port": int(DB_PORT),
140+
"user": DB_USER,
141+
"password": DB_PASSWORD,
142+
"db": DB_NAME,
135143
"charset": "utf8mb4",
136144
"cursorclass": DictCursor,
137145
}
@@ -148,14 +156,14 @@ def expired_token_callback(expired_token):
148156
MAIL_PORT=465,
149157
MAIL_USE_SSL=True,
150158
MAIL_USERNAME="pymatcha@gmail.com",
151-
MAIL_PASSWORD=os.getenv("MAIL_PASSWORD"),
159+
MAIL_PASSWORD=MAIL_PASSWORD,
152160
MAIL_DEBUG=False,
153161
MAIL_DEFAULT_SENDER="pymatcha@gmail.com",
154162
)
155163
logging.debug("Configuring mail")
156164
mail = Mail(application)
157165

158-
redis = StrictRedis(host=os.getenv("REDIS_HOST"), port=os.getenv("REDIS_PORT"), decode_responses=True, db=2)
166+
redis = StrictRedis(host=REDIS_HOST, port=REDIS_PORT, decode_responses=True, db=2)
159167

160168
redis.flushdb()
161169

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
"""
1919
import datetime
20-
import os
2120

2221
from flask import Blueprint
2322
from flask import current_app
24-
from flask import redirect
2523
from flask import render_template
2624
from flask import request
2725
from itsdangerous import BadSignature
@@ -30,8 +28,10 @@
3028
from PyMatcha.utils.confirm_token import confirm_token
3129
from PyMatcha.utils.confirm_token import generate_confirmation_token
3230
from PyMatcha.utils.decorators import validate_params
31+
from PyMatcha.utils.errors import BadRequestError
3332
from PyMatcha.utils.errors import NotFoundError
3433
from PyMatcha.utils.mail import send_mail_html
34+
from PyMatcha.utils.static import FRONTEND_EMAIL_CONFIRMATION_URL
3535
from PyMatcha.utils.success import Success
3636

3737

@@ -40,36 +40,36 @@
4040
auth_email_bp = Blueprint("auth_email", __name__)
4141

4242

43-
@auth_email_bp.route("/auth/confirm/<token>", methods=["GET"])
43+
@auth_email_bp.route("/auth/confirm/<token>", methods=["POST"])
4444
def confirm_email(token):
4545
current_app.logger.debug("/auth/confirm/{} -> Call".format(token))
4646
try:
4747
email, token_type = confirm_token(token, expiration=7200)
4848
except (SignatureExpired, BadSignature) as e:
4949
if e == SignatureExpired:
5050
current_app.logger.debug("/auth/confirm -> Signature Expired")
51-
return redirect("/?type=confirm&success=false&message=Signature expired")
51+
raise BadRequestError("Signature Expired.", "Request another email confirmation and try again.")
5252
else:
5353
current_app.logger.debug("/auth/confirm -> Bad Expired")
54-
return redirect("/?type=confirm&success=false&message=Bad Signature")
54+
raise BadRequestError("Bad Signature.", "Request another password reset and try again.")
5555
else:
5656
if token_type != "confirm":
5757
current_app.logger.debug("/auth/confirm -> Wrong token type")
58-
return redirect("/?type=confirm&success=false&message=Wrong token type")
58+
raise BadRequestError("Wrong token type.")
5959
try:
6060
u = get_user(email)
6161
except NotFoundError:
6262
current_app.logger.debug("/auth/confirm -> User not found")
63-
return redirect("/?type=confirm&success=false&message=User not found")
63+
raise NotFoundError("User not found.")
6464
if u.is_confirmed:
6565
current_app.logger.debug("/auth/confirm -> User already confirmed")
66-
return redirect("/?type=confirm&success=false&message=User already confirmed")
66+
raise BadRequestError("Email already confirmed", "")
6767
else:
6868
u.is_confirmed = True
6969
u.confirmed_on = datetime.datetime.utcnow()
7070
u.save()
7171
current_app.logger.debug("/auth/confirm -> User {} confirmed.".format(u.id))
72-
return redirect("/?type=confirm&success=true&message=User confirmed")
72+
return Success("Confirmation successfull")
7373

7474

7575
@auth_email_bp.route("/auth/confirm/new", methods=["POST"])
@@ -90,7 +90,7 @@ def request_new_email_conf():
9090
else:
9191
current_app.logger.debug("/auth/confirm/new -> User found, sending new confirmation email")
9292
token = generate_confirmation_token(email=email, token_type="confirm")
93-
link = os.getenv("FRONTEND_BASE_URL") + "/auth/confirm/" + token
93+
link = FRONTEND_EMAIL_CONFIRMATION_URL + token
9494
rendered_html = render_template("confirm_email.html", link=link)
9595
send_mail_html.delay(dest=data["email"], subject="Confirm your email on PyMatcha", html=rendered_html)
9696
current_app.logger.debug("/auth/confirm/new -> New confirmation email sent if user exists in database")

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
You should have received a copy of the GNU General Public License
1717
along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
"""
19-
import os
20-
2119
from flask import Blueprint
2220
from flask import current_app
2321
from flask import render_template
@@ -32,9 +30,9 @@
3230
from PyMatcha.utils.errors import BadRequestError
3331
from PyMatcha.utils.errors import NotFoundError
3432
from PyMatcha.utils.mail import send_mail_html
33+
from PyMatcha.utils.static import FRONTEND_PASSWORD_RESET_URL
3534
from PyMatcha.utils.success import Success
3635

37-
3836
REQUIRED_KEYS_PASSWORD_FORGOT = {"email": str}
3937
REQUIRED_KEYS_PASSWORD_RESET = {"token": str, "password": str}
4038

@@ -55,7 +53,7 @@ def forgot_password():
5553
pass
5654
else:
5755
token = generate_confirmation_token(email=data["email"], token_type="reset")
58-
link = f"{os.getenv('FRONTEND_BASE_URL')}/accounts/password/reset?token={token}"
56+
link = FRONTEND_PASSWORD_RESET_URL + token
5957
rendered_html = render_template("password_reset.html", link=link)
6058
current_app.logger.debug("/auth/password/forgot -> Sending worker request to send email")
6159
send_mail_html.delay(dest=data["email"], subject="Reset your password on PyMatcha", html=rendered_html)

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
You should have received a copy of the GNU General Public License
1717
along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
"""
19-
import os
20-
2119
from flask import Blueprint
2220
from flask import current_app
2321
from flask import render_template
@@ -27,9 +25,9 @@
2725
from PyMatcha.utils.decorators import validate_params
2826
from PyMatcha.utils.errors import ConflictError
2927
from PyMatcha.utils.mail import send_mail_html
28+
from PyMatcha.utils.static import FRONTEND_EMAIL_CONFIRMATION_URL
3029
from PyMatcha.utils.success import SuccessOutputMessage
3130

32-
3331
REQUIRED_KEYS_USER_CREATION = {"username": str, "email": str, "password": str, "first_name": str, "last_name": str}
3432

3533
auth_register_bp = Blueprint("auth_register", __name__)
@@ -55,7 +53,7 @@ def api_create_user():
5553
raise e
5654
else:
5755
token = generate_confirmation_token(email=data["email"], token_type="confirm")
58-
link = os.getenv("FRONTEND_BASE_URL") + "/auth/confirm/" + token
56+
link = FRONTEND_EMAIL_CONFIRMATION_URL + token
5957
rendered_html = render_template("confirm_email.html", link=link)
6058
send_mail_html.delay(dest=data["email"], subject="Confirm your email on PyMatcha", html=rendered_html)
6159
return SuccessOutputMessage("email", new_user.email, "New user successfully created.")

backend/PyMatcha/routes/api/profile/edit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
"""
1919
import datetime
20-
import os
2120

2221
import Geohash
2322
from flask import Blueprint
@@ -36,6 +35,7 @@
3635
from PyMatcha.utils.mail import send_mail_html
3736
from PyMatcha.utils.mail import send_mail_text
3837
from PyMatcha.utils.password import check_password
38+
from PyMatcha.utils.static import FRONTEND_EMAIL_CONFIRMATION_URL
3939
from PyMatcha.utils.success import Success
4040

4141
profile_edit_bp = Blueprint("profile_edit", __name__)
@@ -114,7 +114,7 @@ def edit_email():
114114
current_user.is_confirmed = False
115115
current_user.save()
116116
token = generate_confirmation_token(email=new_email, token_type="confirm")
117-
link = os.getenv("FRONTEND_BASE_URL") + "/auth/confirm/" + token
117+
link = FRONTEND_EMAIL_CONFIRMATION_URL + token
118118
rendered_html = render_template("confirm_email.html", link=link)
119119
send_mail_html.delay(dest=data["email"], subject="Confirm your email on PyMatcha", html=rendered_html)
120120
return Success("Email sent for new email")

backend/PyMatcha/utils/static.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
3+
FLASK_PORT = os.getenv("FLASK_PORT")
4+
FLASK_SECRET_KEY = os.getenv("FLASK_DEBUG")
5+
6+
ENABLE_LOGGING = os.getenv("FLASK_HOST")
7+
8+
CELERY_BROKER_URL = os.getenv("FLASK_SECRET_KEY")
9+
CELERY_RESULT_BACKEND = os.getenv("ENABLE_LOGGING")
10+
11+
DB_HOST = os.getenv("CELERY_BROKER_URL")
12+
DB_PORT = os.getenv("CELERY_RESULT_BACKEND")
13+
DB_USER = os.getenv("DB_HOST")
14+
DB_PASSWORD = os.getenv("DB_PORT")
15+
DB_NAME = os.getenv("DB_USER")
16+
17+
MAIL_PASSWORD = os.getenv("DB_PASSWORD")
18+
19+
REDIS_HOST = os.getenv("DB_NAME")
20+
REDIS_PORT = os.getenv("MAIL_PASSWORD")
21+
22+
DEBUG_AUTH_TOKEN = os.getenv("REDIS_HOST")
23+
24+
FRONTEND_BASE_URL = os.getenv("REDIS_PORT")
25+
26+
FRONTEND_EMAIL_CONFIRMATION_URL = FRONTEND_BASE_URL + "/accounts/verify?token="
27+
FRONTEND_PASSWORD_RESET_URL = FRONTEND_BASE_URL + "/accounts/password/reset?token="
28+
29+
PYMATCHA_ROOT = os.path.join(os.path.dirname(__file__), "../..")
30+
BACKEND_ROOT = os.path.join(os.path.dirname(__file__), "../")

0 commit comments

Comments
 (0)