app-MAIL-temp/app/auth/views/mfa.py

59 lines
1.7 KiB
Python
Raw Normal View History

import pyotp
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 StringField, validators
from app.auth.base import auth_bp
from app.config import MFA_USER_ID
from app.log import LOG
from app.models import User
class OtpTokenForm(FlaskForm):
token = StringField("Token", validators=[validators.DataRequired()])
@auth_bp.route("/mfa", methods=["GET", "POST"])
def mfa():
# passed from login page
2020-01-03 23:42:35 +01:00
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")
2020-01-03 23:50:34 +01:00
return redirect(url_for("auth.login"))
2020-01-03 23:42:35 +01:00
user = User.get(user_id)
2020-01-03 23:42:35 +01:00
if not (user and user.enable_otp):
flash("Only user with MFA enabled should go to this page", "warning")
2020-01-03 23:50:34 +01:00
return redirect(url_for("auth.login"))
otp_token_form = OtpTokenForm()
next_url = request.args.get("next")
if otp_token_form.validate_on_submit():
totp = pyotp.TOTP(user.otp_secret)
2019-12-27 17:36:35 +01:00
token = otp_token_form.token.data
2019-12-27 17:36:35 +01:00
if totp.verify(token):
del session[MFA_USER_ID]
2019-12-27 17:36:35 +01:00
login_user(user)
flash(f"Welcome back {user.name}!")
2019-12-27 17:36:35 +01:00
# User comes to login page from another page
if next_url:
LOG.debug("redirect user to %s", next_url)
return redirect(next_url)
else:
2019-12-27 17:36:35 +01:00
LOG.debug("redirect user to dashboard")
return redirect(url_for("dashboard.index"))
else:
flash("Incorrect token", "warning")
return render_template("auth/mfa.html", otp_token_form=otp_token_form)