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

60 lines
2.0 KiB
Python
Raw Normal View History

2020-05-18 14:07:06 +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 HiddenField, validators
from app.dashboard.base import dashboard_bp
2020-10-15 16:45:28 +02:00
from app.dashboard.views.enter_sudo import sudo_required
from app.db import Session
2020-05-18 14:07:06 +02:00
from app.log import LOG
2020-05-18 22:54:05 +02:00
from app.models import RecoveryCode, Fido
2020-05-18 14:07:06 +02:00
class FidoManageForm(FlaskForm):
credential_id = HiddenField("credential_id", validators=[validators.DataRequired()])
@dashboard_bp.route("/fido_manage", methods=["GET", "POST"])
@login_required
@sudo_required
def fido_manage():
if not current_user.fido_enabled():
2020-05-18 22:47:42 +02:00
flash("You haven't registered a security key", "warning")
2020-05-18 14:07:06 +02:00
return redirect(url_for("dashboard.index"))
fido_manage_form = FidoManageForm()
if fido_manage_form.validate_on_submit():
credential_id = fido_manage_form.credential_id.data
2020-05-18 22:54:05 +02:00
fido_key = Fido.get_by(uuid=current_user.fido_uuid, credential_id=credential_id)
2020-05-18 14:07:06 +02:00
if not fido_key:
flash("Unknown error, redirect back to manage page", "warning")
return redirect(url_for("dashboard.fido_manage"))
2020-05-18 22:54:05 +02:00
Fido.delete(fido_key.id)
Session.commit()
2020-05-18 14:07:06 +02:00
LOG.d(f"FIDO Key ID={fido_key.id} Removed")
flash(f"Key {fido_key.name} successfully unlinked", "success")
# Disable FIDO for the user if all keys have been deleted
2020-05-18 22:54:05 +02:00
if not Fido.filter_by(uuid=current_user.fido_uuid).all():
2020-05-18 14:07:06 +02:00
current_user.fido_uuid = None
Session.commit()
2020-05-18 14:07:06 +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)
return redirect(url_for("dashboard.index"))
return redirect(url_for("dashboard.fido_manage"))
return render_template(
"dashboard/fido_manage.html",
fido_manage_form=fido_manage_form,
2020-05-18 22:54:05 +02:00
keys=Fido.filter_by(uuid=current_user.fido_uuid),
2020-05-18 14:07:06 +02:00
)