|
42 | 42 | @auth_password_bp.route("/auth/password/forgot", methods=["POST"]) |
43 | 43 | @validate_params(REQUIRED_KEYS_PASSWORD_FORGOT) |
44 | 44 | def forgot_password(): |
45 | | - current_app.logger.debug("/auth/password/forgot -> Call") |
46 | 45 | data = request.get_json() |
47 | 46 | try: |
48 | 47 | get_user(data["email"]) |
49 | 48 | except NotFoundError: |
50 | | - current_app.logger.debug("/auth/password/forgot -> User {} not found, no email sent".format(data["email"])) |
| 49 | + current_app.logger.debug("User not found, no email sent") |
51 | 50 | pass |
52 | 51 | else: |
53 | 52 | token = generate_confirmation_token(email=data["email"], token_type="reset") |
54 | 53 | link = FRONTEND_PASSWORD_RESET_URL + token |
55 | 54 | rendered_html = render_template("password_reset.html", link=link) |
56 | | - current_app.logger.debug("/auth/password/forgot -> Sending worker request to send email") |
| 55 | + current_app.logger.debug("Sending worker request to send email") |
57 | 56 | send_mail_html.delay(dest=data["email"], subject="Reset your password on PyMatcha", html=rendered_html) |
58 | | - current_app.logger.debug( |
59 | | - "/auth/password/forgot -> Password reset mail sent successfully for user {}".format(data["email"]) |
60 | | - ) |
| 57 | + current_app.logger.debug("Password reset mail sent successfully for user.") |
61 | 58 | return Success("Password reset mail sent successfully if user exists in DB.") |
62 | 59 |
|
63 | 60 |
|
64 | 61 | @auth_password_bp.route("/auth/password/reset", methods=["POST"]) |
65 | 62 | @validate_params(REQUIRED_KEYS_PASSWORD_RESET) |
66 | 63 | def reset_password(): |
67 | | - current_app.logger.debug("/auth/password/reset -> Call") |
68 | 64 | data = request.get_json() |
| 65 | + token = data["token"] |
69 | 66 | try: |
70 | | - email, token_type = confirm_token(data["token"], expiration=7200) |
| 67 | + email, token_type = confirm_token(token, expiration=7200) |
71 | 68 | except (SignatureExpired, BadSignature) as e: |
72 | 69 | if e == SignatureExpired: |
73 | | - current_app.logger.debug("/auth/password/reset -> Signature Expired") |
| 70 | + current_app.logger.debug(f"Signature Expired for {token}") |
74 | 71 | raise BadRequestError("Signature Expired.", "Request another password reset and try again.") |
75 | 72 | else: |
76 | | - current_app.logger.debug("/auth/password/reset -> Bad Signature") |
| 73 | + current_app.logger.debug(f"Bad Signature for {token}") |
77 | 74 | raise BadRequestError("Bad Signature.", "Request another password reset and try again.") |
78 | 75 | else: |
79 | 76 | if token_type != "reset": |
80 | | - current_app.logger.debug("/auth/password/reset -> Wrong token type") |
| 77 | + current_app.logger.debug(f"Wrong token type for {token}") |
81 | 78 | raise BadRequestError("Wrong token type.") |
82 | 79 | try: |
83 | 80 | u = get_user(email) |
84 | 81 | except NotFoundError: |
85 | | - current_app.logger.debug("/auth/password/reset -> User not found") |
86 | 82 | raise NotFoundError("User not found.") |
87 | | - if u.previous_reset_token == data["token"]: |
88 | | - current_app.logger.debug("/auth/password/reset -> Token already used") |
| 83 | + if u.previous_reset_token == token: |
| 84 | + current_app.logger.debug("Token already used") |
89 | 85 | raise BadRequestError("Token already used", "Please request a new one.") |
90 | 86 | u.password = hash_password(data["password"]) |
91 | | - u.previous_reset_token = data["token"] |
| 87 | + u.previous_reset_token = token |
92 | 88 | u.save() |
93 | | - current_app.logger.debug("/auth/password/reset -> Password reset successfully") |
| 89 | + current_app.logger.debug("Password reset successfully") |
94 | 90 | return Success("Password reset successful.") |
95 | 91 |
|
96 | 92 |
|
97 | 93 | @auth_password_bp.route("/auth/password/check_token", methods=["POST"]) |
98 | 94 | @validate_params({"token": str}) |
99 | 95 | def check_token_validity(): |
100 | 96 | data = request.get_json() |
| 97 | + token = data["token"] |
101 | 98 | try: |
102 | | - email, token_type = confirm_token(data["token"], expiration=7200) |
| 99 | + email, token_type = confirm_token(token, expiration=7200) |
103 | 100 | except (SignatureExpired, BadSignature) as e: |
104 | 101 | if e == SignatureExpired: |
105 | | - current_app.logger.debug("/auth/password/reset -> Signature Expired") |
| 102 | + current_app.logger.debug(f"Signature Expired for {token}") |
106 | 103 | raise BadRequestError("Signature Expired.", "Request another password reset and try again.") |
107 | 104 | else: |
108 | | - current_app.logger.debug("/auth/password/reset -> Bad Signature") |
| 105 | + current_app.logger.debug(f"Bad Signature for {token}") |
109 | 106 | raise BadRequestError("Bad Signature.", "Request another password reset and try again.") |
110 | 107 | else: |
111 | 108 | try: |
112 | 109 | u = get_user(email) |
113 | 110 | except NotFoundError: |
114 | | - current_app.logger.debug("/auth/password/reset -> User not found") |
115 | 111 | raise NotFoundError("User not found.") |
116 | | - if u.previous_reset_token == data["token"]: |
117 | | - current_app.logger.debug("/auth/password/reset -> Token already used") |
| 112 | + if u.previous_reset_token == token: |
| 113 | + current_app.logger.debug("Token already used") |
118 | 114 | raise BadRequestError("Token already used", "Please request a new one.") |
119 | 115 | return Success("Reset token is correct.") |
0 commit comments