Exemplo n.º 1
0
def mfa_cancel():
    if not current_user.enable_otp:
        flash("you don't have MFA enabled", "warning")
        return redirect(url_for("dashboard.index"))

    otp_token_form = OtpTokenForm()
    totp = pyotp.TOTP(current_user.otp_secret)

    if otp_token_form.validate_on_submit():
        token = otp_token_form.token.data

        if totp.verify(token):
            current_user.enable_otp = False
            current_user.otp_secret = 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)

            flash("MFA is now disabled", "warning")
            return redirect(url_for("dashboard.index"))
        else:
            flash("Incorrect token", "warning")

    return render_template("dashboard/mfa_cancel.html",
                           otp_token_form=otp_token_form)
Exemplo n.º 2
0
def mfa_cancel():
    if not current_user.enable_otp:
        flash("you don't have MFA enabled", "warning")
        return redirect(url_for("dashboard.index"))

    # user cancels TOTP
    if request.method == "POST":
        current_user.enable_otp = False
        current_user.otp_secret = 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)

        flash("TOTP is now disabled", "warning")
        return redirect(url_for("dashboard.index"))

    return render_template("dashboard/mfa_cancel.html")
Exemplo n.º 3
0
def fido_manage():
    if not current_user.fido_enabled():
        flash("You haven't registered a security key", "warning")
        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

        fido_key = Fido.get_by(uuid=current_user.fido_uuid,
                               credential_id=credential_id)

        if not fido_key:
            flash("Unknown error, redirect back to manage page", "warning")
            return redirect(url_for("dashboard.fido_manage"))

        Fido.delete(fido_key.id)
        Session.commit()

        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
        if not Fido.filter_by(uuid=current_user.fido_uuid).all():
            current_user.fido_uuid = None
            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)

            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,
        keys=Fido.filter_by(uuid=current_user.fido_uuid),
    )
Exemplo n.º 4
0
def recovery_code_route():
    if not current_user.two_factor_authentication_enabled():
        flash("you need to enable either TOTP or WebAuthn", "warning")
        return redirect(url_for("dashboard.index"))

    recovery_codes = RecoveryCode.query.filter_by(
        user_id=current_user.id).all()
    if request.method == "GET" and not recovery_codes:
        # user arrives at this page for the first time
        LOG.d("%s has no recovery keys, generate", current_user)
        RecoveryCode.generate(current_user)
        recovery_codes = RecoveryCode.query.filter_by(
            user_id=current_user.id).all()

    if request.method == "POST":
        RecoveryCode.generate(current_user)
        flash("New recovery codes generated", "success")
        return redirect(url_for("dashboard.recovery_code_route"))

    return render_template("dashboard/recovery_code.html",
                           recovery_codes=recovery_codes)
Exemplo n.º 5
0
def recovery_route():
    # 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)

    if not user.two_factor_authentication_enabled():
        flash("Only user with MFA enabled should go to this page", "warning")
        return redirect(url_for("auth.login"))

    recovery_form = RecoveryForm()
    next_url = request.args.get("next")

    if recovery_form.validate_on_submit():
        code = recovery_form.code.data
        recovery_code = RecoveryCode.get_by(user_id=user.id, code=code)

        if recovery_code:
            if recovery_code.used:
                # Trigger rate limiter
                g.deduct_limit = True
                flash("Code already used", "error")
            else:
                del session[MFA_USER_ID]

                login_user(user)
                flash(f"Welcome back!", "success")

                recovery_code.used = True
                recovery_code.used_at = arrow.now()
                Session.commit()

                # User comes to login page from another page
                if next_url:
                    LOG.d("redirect user to %s", next_url)
                    return redirect(next_url)
                else:
                    LOG.d("redirect user to dashboard")
                    return redirect(url_for("dashboard.index"))
        else:
            # Trigger rate limiter
            g.deduct_limit = True
            flash("Incorrect code", "error")
            send_invalid_totp_login_email(user, "recovery")

    return render_template("auth/recovery.html", recovery_form=recovery_form)
Exemplo n.º 6
0
def fido_setup():
    if current_user.fido_uuid is not None:
        fidos = Fido.filter_by(uuid=current_user.fido_uuid).all()
    else:
        fidos = []

    fido_token_form = FidoTokenForm()

    # Handling POST requests
    if fido_token_form.validate_on_submit():
        try:
            sk_assertion = json.loads(fido_token_form.sk_assertion.data)
        except Exception:
            flash("Key registration failed. Error: Invalid Payload", "warning")
            return redirect(url_for("dashboard.index"))

        fido_uuid = session["fido_uuid"]
        challenge = session["fido_challenge"]

        fido_reg_response = webauthn.WebAuthnRegistrationResponse(
            RP_ID,
            URL,
            sk_assertion,
            challenge,
            trusted_attestation_cert_required=False,
            none_attestation_permitted=True,
        )

        try:
            fido_credential = fido_reg_response.verify()
        except Exception as e:
            LOG.w(f"An error occurred in WebAuthn registration process: {e}")
            flash("Key registration failed.", "warning")
            return redirect(url_for("dashboard.index"))

        if current_user.fido_uuid is None:
            current_user.fido_uuid = fido_uuid
            Session.flush()

        Fido.create(
            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,
            name=fido_token_form.key_name.data,
            user_id=current_user.id,
        )
        Session.commit()

        LOG.d(
            f"credential_id={str(fido_credential.credential_id, 'utf-8')} added for {fido_uuid}"
        )

        flash("Security key has been activated", "success")
        if not RecoveryCode.filter_by(user_id=current_user.id).all():
            return redirect(url_for("dashboard.recovery_code_route"))
        else:
            return redirect(url_for("dashboard.fido_manage"))

    # Prepare information for key registration process
    fido_uuid = (str(uuid.uuid4())
                 if current_user.fido_uuid is None else current_user.fido_uuid)
    challenge = secrets.token_urlsafe(32)

    credential_create_options = webauthn.WebAuthnMakeCredentialOptions(
        challenge,
        "SimpleLogin",
        RP_ID,
        fido_uuid,
        current_user.email,
        current_user.name if current_user.name else current_user.email,
        False,
        attestation="none",
        user_verification="discouraged",
    )

    # 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
    del registration_dict["extensions"]["webauthn.loc"]

    # Prevent user from adding duplicated keys
    for fido in fidos:
        registration_dict["excludeCredentials"].append({
            "type":
            "public-key",
            "id":
            fido.credential_id,
            "transports": ["usb", "nfc", "ble", "internal"],
        })

    session["fido_uuid"] = fido_uuid
    session["fido_challenge"] = challenge.rstrip("=")

    return render_template(
        "dashboard/fido_setup.html",
        fido_token_form=fido_token_form,
        credential_create_options=registration_dict,
    )