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

134 lines
4.4 KiB
Python
Raw Normal View History

2020-05-05 10:32:49 +02:00
import json
import secrets
2020-05-07 17:49:29 +02:00
import uuid
2020-05-18 11:14:40 +02:00
from time import time
2020-05-05 10:32:49 +02:00
2020-05-07 17:49:29 +02:00
import webauthn
2020-05-05 10:32:49 +02:00
from flask import render_template, flash, redirect, url_for, session
from flask_login import login_required, current_user
from flask_wtf import FlaskForm
2020-05-18 11:55:41 +02:00
from wtforms import StringField, HiddenField, validators
2020-05-05 10:32:49 +02:00
2020-05-07 17:49:29 +02:00
from app.config import RP_ID, URL
2020-05-05 10:32:49 +02:00
from app.dashboard.base import dashboard_bp
from app.extensions import db
from app.log import LOG
2020-05-18 22:54:05 +02:00
from app.models import Fido, RecoveryCode
2020-05-18 11:14:40 +02:00
from app.dashboard.views.enter_sudo import sudo_required
2020-05-05 10:32:49 +02:00
2020-05-18 11:15:52 +02:00
2020-05-05 10:32:49 +02:00
class FidoTokenForm(FlaskForm):
2020-05-18 11:55:41 +02:00
key_name = StringField("key_name", validators=[validators.DataRequired()])
2020-05-05 10:32:49 +02:00
sk_assertion = HiddenField("sk_assertion", validators=[validators.DataRequired()])
@dashboard_bp.route("/fido_setup", methods=["GET", "POST"])
@login_required
2020-05-18 11:14:40 +02:00
@sudo_required
2020-05-05 10:32:49 +02:00
def fido_setup():
if not current_user.can_use_fido:
flash(
"This feature is currently in invitation-only beta. Please send us an email if you want to try",
"warning",
)
return redirect(url_for("dashboard.index"))
2020-05-18 09:01:27 +02:00
if current_user.fido_uuid is not None:
2020-05-18 22:55:38 +02:00
fidos = Fido.filter_by(uuid=current_user.fido_uuid).all()
2020-05-18 09:01:27 +02:00
else:
2020-05-18 22:55:38 +02:00
fidos = []
2020-05-18 07:05:37 +02:00
2020-05-05 10:32:49 +02:00
fido_token_form = FidoTokenForm()
2020-05-05 10:58:42 +02:00
# Handling POST requests
if fido_token_form.validate_on_submit():
try:
sk_assertion = json.loads(fido_token_form.sk_assertion.data)
except Exception as e:
2020-05-07 11:53:28 +02:00
flash("Key registration failed. Error: Invalid Payload", "warning")
2020-05-05 10:58:42 +02:00
return redirect(url_for("dashboard.index"))
2020-05-07 11:53:28 +02:00
fido_uuid = session["fido_uuid"]
challenge = session["fido_challenge"]
2020-05-05 10:58:42 +02:00
fido_reg_response = webauthn.WebAuthnRegistrationResponse(
2020-05-07 11:33:24 +02:00
RP_ID,
2020-05-07 14:34:17 +02:00
URL,
2020-05-05 10:58:42 +02:00
sk_assertion,
challenge,
2020-05-07 11:53:28 +02:00
trusted_attestation_cert_required=False,
none_attestation_permitted=True,
)
2020-05-05 10:58:42 +02:00
try:
fido_credential = fido_reg_response.verify()
except Exception as e:
2020-05-07 11:53:28 +02:00
LOG.error(f"An error occurred in WebAuthn registration process: {e}")
flash("Key registration failed.", "warning")
2020-05-05 10:58:42 +02:00
return redirect(url_for("dashboard.index"))
2020-05-18 09:06:24 +02:00
2020-05-18 09:01:27 +02:00
if current_user.fido_uuid is None:
current_user.fido_uuid = fido_uuid
2020-05-24 20:35:52 +02:00
db.session.flush()
2020-05-18 09:01:27 +02:00
2020-05-18 22:54:05 +02:00
Fido.create(
2020-05-18 09:06:24 +02:00
credential_id=str(fido_credential.credential_id, "utf-8"),
uuid=fido_uuid,
public_key=str(fido_credential.public_key, "utf-8"),
sign_count=fido_credential.sign_count,
2020-05-18 11:55:41 +02:00
name=fido_token_form.key_name.data,
2020-05-18 09:01:27 +02:00
)
2020-05-05 10:58:42 +02:00
db.session.commit()
2020-05-18 09:06:24 +02:00
LOG.d(
f"credential_id={str(fido_credential.credential_id, 'utf-8')} added for {fido_uuid}"
)
2020-05-18 09:01:27 +02:00
2020-05-05 10:58:42 +02:00
flash("Security key has been activated", "success")
2020-05-18 14:07:06 +02:00
if not RecoveryCode.query.filter_by(user_id=current_user.id).all():
return redirect(url_for("dashboard.recovery_code_route"))
else:
return redirect(url_for("dashboard.fido_manage"))
2020-05-07 11:53:28 +02:00
2020-05-07 11:31:42 +02:00
# Prepare information for key registration process
2020-05-18 09:06:24 +02:00
fido_uuid = (
str(uuid.uuid4()) if current_user.fido_uuid is None else current_user.fido_uuid
)
2020-05-05 10:32:49 +02:00
challenge = secrets.token_urlsafe(32)
credential_create_options = webauthn.WebAuthnMakeCredentialOptions(
2020-05-07 11:53:28 +02:00
challenge,
"SimpleLogin",
RP_ID,
fido_uuid,
current_user.email,
current_user.name if current_user.name else current_user.email,
2020-05-07 11:53:28 +02:00
False,
attestation="none",
user_verification="discouraged",
2020-05-07 11:53:28 +02:00
)
2020-05-05 10:32:49 +02:00
# Don't think this one should be used, but it's not configurable by arguments
# https://www.w3.org/TR/webauthn/#sctn-location-extension
registration_dict = credential_create_options.registration_dict
2020-05-07 11:53:28 +02:00
del registration_dict["extensions"]["webauthn.loc"]
2020-05-05 10:32:49 +02:00
2020-05-18 09:01:27 +02:00
# Prevent user from adding duplicated keys
2020-05-18 22:55:38 +02:00
for fido in fidos:
2020-05-18 09:06:24 +02:00
registration_dict["excludeCredentials"].append(
{
"type": "public-key",
2020-05-18 22:55:38 +02:00
"id": fido.credential_id,
2020-05-18 09:06:24 +02:00
"transports": ["usb", "nfc", "ble", "internal"],
}
)
2020-05-18 07:05:37 +02:00
2020-05-07 11:53:28 +02:00
session["fido_uuid"] = fido_uuid
session["fido_challenge"] = challenge.rstrip("=")
2020-05-05 10:32:49 +02:00
return render_template(
2020-05-07 11:53:28 +02:00
"dashboard/fido_setup.html",
fido_token_form=fido_token_form,
credential_create_options=registration_dict,
2020-05-05 10:32:49 +02:00
)