2020-08-05 12:30:56 +02:00
|
|
|
from flask import render_template, flash, redirect, url_for, request
|
2019-12-29 15:10:40 +01:00
|
|
|
from flask_login import login_required, current_user
|
|
|
|
|
|
|
|
from app.dashboard.base import dashboard_bp
|
2020-08-05 12:30:56 +02:00
|
|
|
from app.dashboard.views.enter_sudo import sudo_required
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2020-05-17 10:11:38 +02:00
|
|
|
from app.models import RecoveryCode
|
2023-01-12 12:34:47 +01:00
|
|
|
from app.utils import CSRFValidationForm
|
2019-12-29 15:10:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/mfa_cancel", methods=["GET", "POST"])
|
|
|
|
@login_required
|
2020-05-18 11:14:40 +02:00
|
|
|
@sudo_required
|
2019-12-29 15:10:40 +01:00
|
|
|
def mfa_cancel():
|
|
|
|
if not current_user.enable_otp:
|
|
|
|
flash("you don't have MFA enabled", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2023-01-12 12:34:47 +01:00
|
|
|
csrf_form = CSRFValidationForm()
|
|
|
|
|
2020-08-05 12:30:56 +02:00
|
|
|
# user cancels TOTP
|
|
|
|
if request.method == "POST":
|
2023-01-12 12:34:47 +01:00
|
|
|
if not csrf_form.validate():
|
|
|
|
flash("Invalid request", "warning")
|
|
|
|
return redirect(request.url)
|
2020-08-05 12:30:56 +02:00
|
|
|
current_user.enable_otp = False
|
|
|
|
current_user.otp_secret = None
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2019-12-29 15:10:40 +01:00
|
|
|
|
2020-08-05 12:30:56 +02:00
|
|
|
# user does not have any 2FA enabled left, delete all recovery codes
|
|
|
|
if not current_user.two_factor_authentication_enabled():
|
|
|
|
RecoveryCode.empty(current_user)
|
2019-12-29 15:10:40 +01:00
|
|
|
|
2020-08-05 12:30:56 +02:00
|
|
|
flash("TOTP is now disabled", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
2019-12-29 15:10:40 +01:00
|
|
|
|
2023-01-12 12:34:47 +01:00
|
|
|
return render_template("dashboard/mfa_cancel.html", csrf_form=csrf_form)
|