2020-05-05 14:03:29 +02:00
|
|
|
import json
|
|
|
|
import secrets
|
|
|
|
|
2020-05-07 17:49:29 +02:00
|
|
|
import webauthn
|
2020-05-05 14:03:29 +02:00
|
|
|
from flask import request, render_template, redirect, url_for, flash, session
|
|
|
|
from flask_login import login_user
|
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import HiddenField, validators
|
|
|
|
|
|
|
|
from app.auth.base import auth_bp
|
|
|
|
from app.config import MFA_USER_ID
|
2020-05-07 17:49:29 +02:00
|
|
|
from app.config import RP_ID, URL
|
|
|
|
from app.extensions import db
|
2020-05-05 14:03:29 +02:00
|
|
|
from app.log import LOG
|
|
|
|
from app.models import User
|
|
|
|
|
|
|
|
|
|
|
|
class FidoTokenForm(FlaskForm):
|
|
|
|
sk_assertion = HiddenField("sk_assertion", validators=[validators.DataRequired()])
|
|
|
|
|
|
|
|
|
|
|
|
@auth_bp.route("/fido", methods=["GET", "POST"])
|
|
|
|
def fido():
|
|
|
|
# passed from login page
|
|
|
|
user_id = session.get(MFA_USER_ID)
|
|
|
|
|
|
|
|
# user access this page directly without passing by login page
|
|
|
|
if not user_id:
|
|
|
|
flash("Unknown error, redirect back to main page", "warning")
|
|
|
|
return redirect(url_for("auth.login"))
|
|
|
|
|
|
|
|
user = User.get(user_id)
|
|
|
|
|
2020-05-07 17:56:25 +02:00
|
|
|
if not (user and user.fido_enabled()):
|
2020-05-05 14:03:29 +02:00
|
|
|
flash("Only user with security key linked should go to this page", "warning")
|
|
|
|
return redirect(url_for("auth.login"))
|
|
|
|
|
2020-05-12 04:17:51 +02:00
|
|
|
auto_activate = True
|
2020-05-05 14:03:29 +02:00
|
|
|
fido_token_form = FidoTokenForm()
|
|
|
|
|
|
|
|
next_url = request.args.get("next")
|
|
|
|
|
|
|
|
webauthn_user = webauthn.WebAuthnUser(
|
2020-05-07 11:53:28 +02:00
|
|
|
user.fido_uuid,
|
|
|
|
user.email,
|
2020-05-08 23:21:38 +02:00
|
|
|
user.name if user.name else user.email,
|
2020-05-07 11:53:28 +02:00
|
|
|
False,
|
|
|
|
user.fido_credential_id,
|
|
|
|
user.fido_pk,
|
|
|
|
user.fido_sign_count,
|
|
|
|
RP_ID,
|
|
|
|
)
|
2020-05-05 14:03:29 +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 verification failed. Error: Invalid Payload", "warning")
|
2020-05-05 23:13:01 +02:00
|
|
|
return redirect(url_for("auth.login"))
|
2020-05-07 11:53:28 +02:00
|
|
|
|
|
|
|
challenge = session["fido_challenge"]
|
2020-05-05 14:03:29 +02:00
|
|
|
|
|
|
|
webauthn_assertion_response = webauthn.WebAuthnAssertionResponse(
|
2020-05-07 14:34:17 +02:00
|
|
|
webauthn_user, sk_assertion, challenge, URL, uv_required=False
|
2020-05-05 14:03:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
2020-05-05 23:26:26 +02:00
|
|
|
new_sign_count = webauthn_assertion_response.verify()
|
2020-05-05 14:03:29 +02:00
|
|
|
except Exception as e:
|
2020-05-07 11:53:28 +02:00
|
|
|
LOG.error(f"An error occurred in WebAuthn verification process: {e}")
|
|
|
|
flash("Key verification failed.", "warning")
|
2020-05-12 04:17:51 +02:00
|
|
|
auto_activate = False
|
2020-05-07 14:41:34 +02:00
|
|
|
else:
|
2020-05-05 14:03:29 +02:00
|
|
|
user.fido_sign_count = new_sign_count
|
|
|
|
db.session.commit()
|
|
|
|
del session[MFA_USER_ID]
|
|
|
|
|
|
|
|
login_user(user)
|
|
|
|
flash(f"Welcome back {user.name}!", "success")
|
|
|
|
|
|
|
|
# User comes to login page from another page
|
|
|
|
if next_url:
|
|
|
|
LOG.debug("redirect user to %s", next_url)
|
|
|
|
return redirect(next_url)
|
|
|
|
else:
|
|
|
|
LOG.debug("redirect user to dashboard")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
2020-05-07 17:48:44 +02:00
|
|
|
|
2020-05-07 11:31:42 +02:00
|
|
|
# Prepare information for key registration process
|
2020-05-07 11:53:28 +02:00
|
|
|
session.pop("challenge", None)
|
2020-05-05 14:03:29 +02:00
|
|
|
challenge = secrets.token_urlsafe(32)
|
2020-05-07 11:53:28 +02:00
|
|
|
|
|
|
|
session["fido_challenge"] = challenge.rstrip("=")
|
2020-05-05 14:03:29 +02:00
|
|
|
|
|
|
|
webauthn_assertion_options = webauthn.WebAuthnAssertionOptions(
|
2020-05-07 11:53:28 +02:00
|
|
|
webauthn_user, challenge
|
|
|
|
)
|
2020-05-05 14:03:29 +02:00
|
|
|
webauthn_assertion_options = webauthn_assertion_options.assertion_dict
|
|
|
|
|
2020-05-07 11:53:28 +02:00
|
|
|
return render_template(
|
|
|
|
"auth/fido.html",
|
|
|
|
fido_token_form=fido_token_form,
|
|
|
|
webauthn_assertion_options=webauthn_assertion_options,
|
|
|
|
enable_otp=user.enable_otp,
|
2020-05-12 04:17:51 +02:00
|
|
|
auto_activate=auto_activate,
|
2020-05-17 10:35:11 +02:00
|
|
|
next_url=next_url,
|
2020-05-07 11:53:28 +02:00
|
|
|
)
|