Added log flushing and debug OTP printing
Browse files- webpass/routes/otp.py +14 -16
webpass/routes/otp.py
CHANGED
|
@@ -20,6 +20,7 @@ from webpass import mail, csrf, limiter
|
|
| 20 |
|
| 21 |
otp_bp = Blueprint("otp", __name__)
|
| 22 |
|
|
|
|
| 23 |
# --- HELPER: ASYNC EMAIL SENDER ---
|
| 24 |
def send_async_email(app, msg):
|
| 25 |
"""
|
|
@@ -29,9 +30,11 @@ def send_async_email(app, msg):
|
|
| 29 |
with app.app_context():
|
| 30 |
try:
|
| 31 |
mail.send(msg)
|
| 32 |
-
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
@otp_bp.route("/otp/send")
|
| 37 |
@login_required
|
|
@@ -41,41 +44,36 @@ def send_otp():
|
|
| 41 |
"""
|
| 42 |
Generates OTP, sends email asynchronously, and redirects to verification page.
|
| 43 |
"""
|
| 44 |
-
# 1. Capture where the user was trying to go
|
| 45 |
next_url = request.args.get("next") or url_for("dashboard.dashboard")
|
| 46 |
feature = request.args.get("feature", "Sensitive Action")
|
| 47 |
|
| 48 |
-
#
|
| 49 |
code = f"{random.randint(0, 999999):06d}"
|
| 50 |
|
| 51 |
-
#
|
|
|
|
|
|
|
|
|
|
| 52 |
session["otp_code"] = code
|
| 53 |
-
session["otp_expiry"] = time.time() + 5 * 60
|
| 54 |
session["otp_next"] = next_url
|
| 55 |
session["otp_feature"] = feature
|
| 56 |
|
| 57 |
-
#
|
| 58 |
try:
|
| 59 |
msg = Message(
|
| 60 |
subject=f"Your OTP for {feature}",
|
| 61 |
recipients=[current_user.email],
|
| 62 |
-
body=
|
| 63 |
-
f"Your one-time code for accessing {feature} is:\n\n"
|
| 64 |
-
f" {code}\n\n"
|
| 65 |
-
"It expires in 5 minutes."
|
| 66 |
-
)
|
| 67 |
)
|
| 68 |
|
| 69 |
-
# THREADING: Hand the email off to a background process!
|
| 70 |
-
# current_app._get_current_object() passes the real Flask app to the new thread
|
| 71 |
Thread(target=send_async_email, args=(current_app._get_current_object(), msg)).start()
|
| 72 |
|
| 73 |
flash(f"An OTP has been sent to {current_user.email}", "info")
|
| 74 |
except Exception as e:
|
| 75 |
-
print(f"Error preparing email: {e}")
|
| 76 |
flash("Failed to prepare OTP email. Please check logs.", "danger")
|
| 77 |
|
| 78 |
-
# 5. Redirect to Enter Code IMMEDIATELY (No waiting for Gmail!)
|
| 79 |
return redirect(url_for("otp.verify_otp"))
|
| 80 |
|
| 81 |
|
|
|
|
| 20 |
|
| 21 |
otp_bp = Blueprint("otp", __name__)
|
| 22 |
|
| 23 |
+
# --- HELPER: ASYNC EMAIL SENDER ---
|
| 24 |
# --- HELPER: ASYNC EMAIL SENDER ---
|
| 25 |
def send_async_email(app, msg):
|
| 26 |
"""
|
|
|
|
| 30 |
with app.app_context():
|
| 31 |
try:
|
| 32 |
mail.send(msg)
|
| 33 |
+
# flush=True forces Hugging Face to show this immediately
|
| 34 |
+
print(f" [+] SUCCESS: OTP Email sent asynchronously to {msg.recipients}", flush=True)
|
| 35 |
except Exception as e:
|
| 36 |
+
# This will reveal exactly why Gmail is rejecting the email!
|
| 37 |
+
print(f" [!] EMAIL ERROR: Failed to send async email: {e}", flush=True)
|
| 38 |
|
| 39 |
@otp_bp.route("/otp/send")
|
| 40 |
@login_required
|
|
|
|
| 44 |
"""
|
| 45 |
Generates OTP, sends email asynchronously, and redirects to verification page.
|
| 46 |
"""
|
|
|
|
| 47 |
next_url = request.args.get("next") or url_for("dashboard.dashboard")
|
| 48 |
feature = request.args.get("feature", "Sensitive Action")
|
| 49 |
|
| 50 |
+
# Generate Code
|
| 51 |
code = f"{random.randint(0, 999999):06d}"
|
| 52 |
|
| 53 |
+
# EMERGENCY BACKDOOR FOR TESTING: Print the OTP directly to the Hugging Face logs!
|
| 54 |
+
print(f" [*] GENERATED OTP FOR {current_user.email} IS: {code} (Feature: {feature})", flush=True)
|
| 55 |
+
|
| 56 |
+
# Store in Session
|
| 57 |
session["otp_code"] = code
|
| 58 |
+
session["otp_expiry"] = time.time() + 5 * 60
|
| 59 |
session["otp_next"] = next_url
|
| 60 |
session["otp_feature"] = feature
|
| 61 |
|
| 62 |
+
# Send Email Asynchronously
|
| 63 |
try:
|
| 64 |
msg = Message(
|
| 65 |
subject=f"Your OTP for {feature}",
|
| 66 |
recipients=[current_user.email],
|
| 67 |
+
body=f"Your one-time code for accessing {feature} is:\n\n {code}\n\nIt expires in 5 minutes."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
)
|
| 69 |
|
|
|
|
|
|
|
| 70 |
Thread(target=send_async_email, args=(current_app._get_current_object(), msg)).start()
|
| 71 |
|
| 72 |
flash(f"An OTP has been sent to {current_user.email}", "info")
|
| 73 |
except Exception as e:
|
| 74 |
+
print(f"Error preparing email: {e}", flush=True)
|
| 75 |
flash("Failed to prepare OTP email. Please check logs.", "danger")
|
| 76 |
|
|
|
|
| 77 |
return redirect(url_for("otp.verify_otp"))
|
| 78 |
|
| 79 |
|