app-MAIL-temp/app/dashboard/views/fido_cancel.py

46 lines
1.5 KiB
Python
Raw Normal View History

2020-05-05 11:20:52 +02:00
from flask import render_template, flash, redirect, url_for
from flask_login import login_required, current_user
from flask_wtf import FlaskForm
from wtforms import PasswordField, validators
from app.dashboard.base import dashboard_bp
from app.extensions import db
from app.models import RecoveryCode
2020-05-05 11:20:52 +02:00
class LoginForm(FlaskForm):
password = PasswordField("Password", validators=[validators.DataRequired()])
@dashboard_bp.route("/fido_cancel", methods=["GET", "POST"])
@login_required
def fido_cancel():
2020-05-07 14:32:52 +02:00
if not current_user.fido_enabled():
2020-05-05 11:20:52 +02:00
flash("You haven't registed a security key", "warning")
return redirect(url_for("dashboard.index"))
password_check_form = LoginForm()
if password_check_form.validate_on_submit():
password = password_check_form.password.data
if current_user.check_password(password):
current_user.fido_pk = None
current_user.fido_uuid = None
2020-05-05 12:16:52 +02:00
current_user.fido_sign_count = None
2020-05-05 11:20:52 +02:00
current_user.fido_credential_id = None
db.session.commit()
# user does not have any 2FA enabled left, delete all recovery codes
if not current_user.two_factor_authentication_enabled():
RecoveryCode.empty(current_user)
2020-05-05 11:20:52 +02:00
flash("We've unlinked your security key.", "success")
return redirect(url_for("dashboard.index"))
else:
flash("Incorrect password", "warning")
2020-05-07 11:53:28 +02:00
return render_template(
"dashboard/fido_cancel.html", password_check_form=password_check_form
)