2020-05-05 12:03:29 +00:00
|
|
|
import json
|
|
|
|
import secrets
|
2020-10-15 14:45:28 +00:00
|
|
|
|
2020-05-07 15:49:29 +00:00
|
|
|
import webauthn
|
2020-05-23 19:56:42 +00:00
|
|
|
from flask import (
|
|
|
|
request,
|
|
|
|
render_template,
|
|
|
|
redirect,
|
|
|
|
url_for,
|
|
|
|
flash,
|
|
|
|
session,
|
|
|
|
make_response,
|
2020-05-24 14:14:48 +00:00
|
|
|
g,
|
2020-05-23 19:56:42 +00:00
|
|
|
)
|
2020-05-05 12:03:29 +00:00
|
|
|
from flask_login import login_user
|
|
|
|
from flask_wtf import FlaskForm
|
2020-05-23 19:56:42 +00:00
|
|
|
from wtforms import HiddenField, validators, BooleanField
|
2020-05-05 12:03:29 +00:00
|
|
|
|
|
|
|
from app.auth.base import auth_bp
|
|
|
|
from app.config import MFA_USER_ID
|
2020-05-07 15:49:29 +00:00
|
|
|
from app.config import RP_ID, URL
|
2021-10-12 12:36:47 +00:00
|
|
|
from app.db import Session
|
|
|
|
from app.extensions import limiter
|
2020-05-05 12:03:29 +00:00
|
|
|
from app.log import LOG
|
2020-05-23 19:56:42 +00:00
|
|
|
from app.models import User, Fido, MfaBrowser
|
2022-03-29 16:03:18 +00:00
|
|
|
from app.utils import sanitize_next_url
|
2020-05-05 12:03:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FidoTokenForm(FlaskForm):
|
|
|
|
sk_assertion = HiddenField("sk_assertion", validators=[validators.DataRequired()])
|
2020-05-23 19:56:42 +00:00
|
|
|
remember = BooleanField(
|
|
|
|
"attr", default=False, description="Remember this browser for 30 days"
|
|
|
|
)
|
2020-05-05 12:03:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@auth_bp.route("/fido", methods=["GET", "POST"])
|
2020-05-24 14:14:48 +00:00
|
|
|
@limiter.limit(
|
|
|
|
"10/minute", deduct_when=lambda r: hasattr(g, "deduct_limit") and g.deduct_limit
|
|
|
|
)
|
2020-05-05 12:03:29 +00:00
|
|
|
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 15:56:25 +00:00
|
|
|
if not (user and user.fido_enabled()):
|
2020-05-05 12:03:29 +00:00
|
|
|
flash("Only user with security key linked should go to this page", "warning")
|
|
|
|
return redirect(url_for("auth.login"))
|
|
|
|
|
2020-05-12 02:17:51 +00:00
|
|
|
auto_activate = True
|
2020-05-05 12:03:29 +00:00
|
|
|
fido_token_form = FidoTokenForm()
|
|
|
|
|
2022-03-29 16:03:18 +00:00
|
|
|
next_url = sanitize_next_url(request.args.get("next"))
|
2020-05-05 12:03:29 +00:00
|
|
|
|
2020-05-23 19:56:42 +00:00
|
|
|
if request.cookies.get("mfa"):
|
|
|
|
browser = MfaBrowser.get_by(token=request.cookies.get("mfa"))
|
|
|
|
if browser and not browser.is_expired() and browser.user_id == user.id:
|
|
|
|
login_user(user)
|
2021-01-11 09:22:39 +00:00
|
|
|
flash(f"Welcome back!", "success")
|
2020-05-23 19:56:42 +00:00
|
|
|
# Redirect user to correct page
|
|
|
|
return redirect(next_url or url_for("dashboard.index"))
|
2020-05-24 14:14:48 +00:00
|
|
|
else:
|
|
|
|
# Trigger rate limiter
|
|
|
|
g.deduct_limit = True
|
2020-05-23 19:56:42 +00:00
|
|
|
|
2020-05-05 12:03:29 +00:00
|
|
|
# Handling POST requests
|
|
|
|
if fido_token_form.validate_on_submit():
|
|
|
|
try:
|
|
|
|
sk_assertion = json.loads(fido_token_form.sk_assertion.data)
|
2020-12-06 21:08:35 +00:00
|
|
|
except Exception:
|
2020-05-07 09:53:28 +00:00
|
|
|
flash("Key verification failed. Error: Invalid Payload", "warning")
|
2020-05-05 21:13:01 +00:00
|
|
|
return redirect(url_for("auth.login"))
|
2020-05-07 09:53:28 +00:00
|
|
|
|
|
|
|
challenge = session["fido_challenge"]
|
2020-05-05 12:03:29 +00:00
|
|
|
|
|
|
|
try:
|
2020-05-18 20:54:05 +00:00
|
|
|
fido_key = Fido.get_by(
|
2020-05-18 08:02:58 +00:00
|
|
|
uuid=user.fido_uuid, credential_id=sk_assertion["id"]
|
|
|
|
)
|
|
|
|
webauthn_user = webauthn.WebAuthnUser(
|
|
|
|
user.fido_uuid,
|
|
|
|
user.email,
|
|
|
|
user.name if user.name else user.email,
|
|
|
|
False,
|
|
|
|
fido_key.credential_id,
|
|
|
|
fido_key.public_key,
|
|
|
|
fido_key.sign_count,
|
|
|
|
RP_ID,
|
|
|
|
)
|
|
|
|
webauthn_assertion_response = webauthn.WebAuthnAssertionResponse(
|
|
|
|
webauthn_user, sk_assertion, challenge, URL, uv_required=False
|
|
|
|
)
|
2020-05-05 21:26:26 +00:00
|
|
|
new_sign_count = webauthn_assertion_response.verify()
|
2020-05-05 12:03:29 +00:00
|
|
|
except Exception as e:
|
2021-09-08 09:29:55 +00:00
|
|
|
LOG.w(f"An error occurred in WebAuthn verification process: {e}")
|
2020-05-07 09:53:28 +00:00
|
|
|
flash("Key verification failed.", "warning")
|
2020-05-24 14:14:48 +00:00
|
|
|
# Trigger rate limiter
|
|
|
|
g.deduct_limit = True
|
2020-05-12 02:17:51 +00:00
|
|
|
auto_activate = False
|
2020-05-07 12:41:34 +00:00
|
|
|
else:
|
2020-05-05 12:03:29 +00:00
|
|
|
user.fido_sign_count = new_sign_count
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-05-05 12:03:29 +00:00
|
|
|
del session[MFA_USER_ID]
|
|
|
|
|
|
|
|
login_user(user)
|
2021-01-11 09:22:39 +00:00
|
|
|
flash(f"Welcome back!", "success")
|
2020-05-05 12:03:29 +00:00
|
|
|
|
2020-05-23 19:56:42 +00:00
|
|
|
# Redirect user to correct page
|
|
|
|
response = make_response(redirect(next_url or url_for("dashboard.index")))
|
|
|
|
|
|
|
|
if fido_token_form.remember.data:
|
|
|
|
browser = MfaBrowser.create_new(user=user)
|
2021-10-12 12:36:47 +00:00
|
|
|
Session.commit()
|
2020-05-23 19:56:42 +00:00
|
|
|
response.set_cookie(
|
|
|
|
"mfa",
|
|
|
|
value=browser.token,
|
|
|
|
expires=browser.expires.datetime,
|
|
|
|
secure=True if URL.startswith("https") else False,
|
|
|
|
httponly=True,
|
|
|
|
samesite="Lax",
|
|
|
|
)
|
|
|
|
|
|
|
|
return response
|
2020-05-07 15:48:44 +00:00
|
|
|
|
2020-05-07 09:31:42 +00:00
|
|
|
# Prepare information for key registration process
|
2020-05-07 09:53:28 +00:00
|
|
|
session.pop("challenge", None)
|
2020-05-05 12:03:29 +00:00
|
|
|
challenge = secrets.token_urlsafe(32)
|
2020-05-07 09:53:28 +00:00
|
|
|
|
|
|
|
session["fido_challenge"] = challenge.rstrip("=")
|
2020-05-05 12:03:29 +00:00
|
|
|
|
2020-05-18 20:55:38 +00:00
|
|
|
fidos = Fido.filter_by(uuid=user.fido_uuid).all()
|
2020-05-18 08:02:58 +00:00
|
|
|
webauthn_users = []
|
2020-05-18 20:55:38 +00:00
|
|
|
for fido in fidos:
|
2020-05-18 08:02:58 +00:00
|
|
|
webauthn_users.append(
|
|
|
|
webauthn.WebAuthnUser(
|
|
|
|
user.fido_uuid,
|
|
|
|
user.email,
|
|
|
|
user.name if user.name else user.email,
|
|
|
|
False,
|
2020-05-18 20:55:38 +00:00
|
|
|
fido.credential_id,
|
|
|
|
fido.public_key,
|
|
|
|
fido.sign_count,
|
2020-05-18 08:02:58 +00:00
|
|
|
RP_ID,
|
|
|
|
)
|
|
|
|
)
|
2020-05-18 08:04:45 +00:00
|
|
|
|
2020-05-05 12:03:29 +00:00
|
|
|
webauthn_assertion_options = webauthn.WebAuthnAssertionOptions(
|
2020-05-18 07:08:06 +00:00
|
|
|
webauthn_users, challenge
|
2020-05-07 09:53:28 +00:00
|
|
|
)
|
2020-05-05 12:03:29 +00:00
|
|
|
webauthn_assertion_options = webauthn_assertion_options.assertion_dict
|
2022-04-26 16:41:12 +00:00
|
|
|
try:
|
|
|
|
# HACK: We need to upgrade to webauthn > 1 so it can support specifying the transports
|
|
|
|
del webauthn_assertion_options["allowCredentials"][0]["transports"]
|
|
|
|
except KeyError:
|
|
|
|
# Should never happen but...
|
|
|
|
pass
|
2020-05-05 12:03:29 +00:00
|
|
|
|
2020-05-07 09:53:28 +00: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 02:17:51 +00:00
|
|
|
auto_activate=auto_activate,
|
2020-05-17 08:35:11 +00:00
|
|
|
next_url=next_url,
|
2020-05-07 09:53:28 +00:00
|
|
|
)
|