|
|
@@ -6,24 +6,28 @@ from werkzeug.security import generate_password_hash, check_password_hash
|
|
|
from .models import User
|
|
|
from . import db
|
|
|
|
|
|
-from .forms import LoginForm, RegForm
|
|
|
+from .forms import LoginForm, RegForm, MFAForm
|
|
|
+
|
|
|
+# MFA
|
|
|
+import pyotp
|
|
|
|
|
|
accounts = Blueprint('accounts', __name__)
|
|
|
|
|
|
@accounts.route('/login', methods=['GET', 'POST'])
|
|
|
def login():
|
|
|
form = LoginForm()
|
|
|
-
|
|
|
+
|
|
|
if form.validate_on_submit():
|
|
|
email = form.email.data
|
|
|
challenge_passwd = form.passwd.data
|
|
|
|
|
|
+ # Check fo user in User table
|
|
|
user = User.query.filter_by(email=email).first()
|
|
|
+
|
|
|
+ # If there's a user
|
|
|
if user:
|
|
|
if check_password_hash(user.password, challenge_passwd):
|
|
|
- flash('Successful Login!', category='success')
|
|
|
- login_user(user, remember=True)
|
|
|
- return redirect(url_for('dashboards.market'))
|
|
|
+ return redirect(url_for('accounts.mfa', user_chal = user.id)) # passes user to mfa
|
|
|
else:
|
|
|
flash('Unsucessful Login!', category='error')
|
|
|
else:
|
|
|
@@ -80,7 +84,39 @@ def register():
|
|
|
return redirect(url_for('dashboards.market'))
|
|
|
else:
|
|
|
flash('Registration Failed', category='error')
|
|
|
- return render_template("register.html", user=current_user, form = form)
|
|
|
+ return render_template("register.html", user = current_user, form = form)
|
|
|
+
|
|
|
|
|
|
+@accounts.route('/mfa', methods=['GET', 'POST'])
|
|
|
+def mfa():
|
|
|
+ form = MFAForm()
|
|
|
+ user_chal = request.args['user_chal']
|
|
|
+ user = User.query.filter_by(id = user_chal).first()
|
|
|
+
|
|
|
+ # check for existing totphash
|
|
|
+ if not user.totphash:
|
|
|
+ # generate random secret key for auth
|
|
|
+ secret = pyotp.random_base32()
|
|
|
+ # add to User table and show this secret next time.
|
|
|
+ dbcall = User.query.filter_by(id = user.id).first()
|
|
|
+ dbcall.totphash = secret
|
|
|
+ db.session.commit()
|
|
|
+ flash('Generated new TOTP Secret', category='success')
|
|
|
+ else: # create a new totphash
|
|
|
+ secret = user.totphash
|
|
|
+
|
|
|
+ challenge_answer = int(pyotp.TOTP(secret).now())
|
|
|
+
|
|
|
+ if form.validate_on_submit():
|
|
|
+ otp = int(form.otp.data)
|
|
|
+ # checks MFA
|
|
|
+ if challenge_answer == otp:
|
|
|
+ flash('Login Successful!', category='sucess')
|
|
|
+ login_user(user, remember=True)
|
|
|
+ return redirect(url_for('dashboards.market'))
|
|
|
+ else:
|
|
|
+ flash('Login Unsuccessful!', category='error')
|
|
|
+ return redirect(url_for('accounts.mfa'))
|
|
|
|
|
|
|
|
|
+ return render_template('mfa.html', secret = secret, form = form, user = user)
|