From c0a13afe13cc990b96fade728a55c2cff2e881da Mon Sep 17 00:00:00 2001
From: Allyssa Poulin <40319407+allyssap@users.noreply.github.com>
Date: Sun, 12 Mar 2023 17:35:40 -0400
Subject: [PATCH] otpform and otp generation
-issue: how to save generated OTP to user, to check if user inputs the correct OTP
---
.idea/inspectionProfiles/Project_Default.xml | 59 ++++++++++++++++++++
.idea/workspace.xml | 35 ++++++++++++
app/api/auth.py | 1 -
app/auth/email.py | 23 ++++++++
app/auth/forms.py | 4 +-
app/auth/routes.py | 14 +++--
app/main/forms.py | 1 -
app/models.py | 7 +++
app/templates/auth/opt_login.html | 14 +++++
app/templates/email/otp.html | 9 +++
app/templates/email/otp.txt | 11 ++++
11 files changed, 171 insertions(+), 7 deletions(-)
create mode 100644 .idea/inspectionProfiles/Project_Default.xml
create mode 100644 .idea/workspace.xml
create mode 100644 app/templates/auth/opt_login.html
create mode 100644 app/templates/email/otp.html
create mode 100644 app/templates/email/otp.txt
diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
new file mode 100644
index 0000000..a1f9af5
--- /dev/null
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -0,0 +1,59 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
new file mode 100644
index 0000000..49100b4
--- /dev/null
+++ b/.idea/workspace.xml
@@ -0,0 +1,35 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/api/auth.py b/app/api/auth.py
index 230f98c..e78e282 100644
--- a/app/api/auth.py
+++ b/app/api/auth.py
@@ -12,7 +12,6 @@ def verify_password(username, password):
if user and user.check_password(password):
return user
-
@basic_auth.error_handler
def basic_auth_error(status):
return error_response(status)
diff --git a/app/auth/email.py b/app/auth/email.py
index 98755ac..bdb1d7d 100644
--- a/app/auth/email.py
+++ b/app/auth/email.py
@@ -1,8 +1,31 @@
from flask import render_template, current_app
from flask_babel import _
from app.email import send_email
+import math, random
+def generate_otp(user):
+ # Declare a digits variable
+ # which stores all digits
+ digits = "0123456789"
+ otp = ""
+
+ # length of password can be changed
+ # by changing value in range
+ for i in range(4):
+ otp += digits[math.floor(random.random() * 10)]
+ return otp
+
+def send_otp_email(user):
+ otp = generate_otp(user)
+ send_email(_('[Microblog] Your one time passcode'),
+ sender=current_app.config['ADMINS'][0],
+ recipients=[user.email],
+ text_body=render_template('email/otp.txt',
+ user=user, otp=otp),
+ html_body=render_template('email/otp.html',
+ user=user, otp=otp))
+
def send_password_reset_email(user):
token = user.get_reset_password_token()
send_email(_('[Microblog] Reset Your Password'),
diff --git a/app/auth/forms.py b/app/auth/forms.py
index c1dd3eb..f851b79 100644
--- a/app/auth/forms.py
+++ b/app/auth/forms.py
@@ -4,13 +4,15 @@ from wtforms.validators import ValidationError, DataRequired, Email, EqualTo
from flask_babel import _, lazy_gettext as _l
from app.models import User
-
class LoginForm(FlaskForm):
username = StringField(_l('Username'), validators=[DataRequired()])
password = PasswordField(_l('Password'), validators=[DataRequired()])
remember_me = BooleanField(_l('Remember Me'))
submit = SubmitField(_l('Sign In'))
+class OTPForm(FlaskForm):
+ OTP = StringField(_l('One Time Passcode'), validators=[DataRequired()]) ###EqualTo(otp)
+ submit = SubmitField(_l('Log in') )
class RegistrationForm(FlaskForm):
username = StringField(_l('Username'), validators=[DataRequired()])
diff --git a/app/auth/routes.py b/app/auth/routes.py
index b3f1d72..77702dc 100644
--- a/app/auth/routes.py
+++ b/app/auth/routes.py
@@ -9,11 +9,8 @@ from app.auth.forms import LoginForm, RegistrationForm, \
from app.models import User
from app.auth.email import send_password_reset_email
-
@bp.route('/login', methods=['GET', 'POST'])
def login():
- if current_user.is_authenticated:
- return redirect(url_for('main.index'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
@@ -23,10 +20,19 @@ def login():
login_user(user, remember=form.remember_me.data)
next_page = request.args.get('next')
if not next_page or url_parse(next_page).netloc != '':
- next_page = url_for('main.index')
+ next_page = url_for('auth/otp_login.html')
return redirect(next_page)
return render_template('auth/login.html', title=_('Sign In'), form=form)
+def auth_opt():
+ form = OTPForm()
+ if OTP != user.curr_otp :
+ flash(_('Invalid OTP'))
+ if form.validate_on_submit():
+ return redirect(url_for('main.index'))
+
+ return render_template('auth/otp_login.html', title=_('Enter OTP'),
+ form=form)
@bp.route('/logout')
def logout():
diff --git a/app/main/forms.py b/app/main/forms.py
index a7c8d96..f06209c 100644
--- a/app/main/forms.py
+++ b/app/main/forms.py
@@ -26,7 +26,6 @@ class EditProfileForm(FlaskForm):
class EmptyForm(FlaskForm):
submit = SubmitField('Submit')
-
class PostForm(FlaskForm):
post = TextAreaField(_l('Say something'), validators=[DataRequired()])
submit = SubmitField(_l('Submit'))
diff --git a/app/models.py b/app/models.py
index 5bdab20..c7741e7 100644
--- a/app/models.py
+++ b/app/models.py
@@ -96,6 +96,7 @@ class User(UserMixin, PaginatedAPIMixin, db.Model):
password_hash = db.Column(db.String(128))
posts = db.relationship('Post', backref='author', lazy='dynamic')
about_me = db.Column(db.String(140))
+ otp = db.Column(db.String(4), index=True, unique=True)
last_seen = db.Column(db.DateTime, default=datetime.utcnow)
token = db.Column(db.String(32), index=True, unique=True)
token_expiration = db.Column(db.DateTime)
@@ -118,6 +119,12 @@ class User(UserMixin, PaginatedAPIMixin, db.Model):
def __repr__(self):
return ''.format(self.username)
+ def set_otp(self, otp):
+ self.otp = otp
+
+ def get_otp(self):
+ return self.otp
+
def set_password(self, password):
self.password_hash = generate_password_hash(password)
diff --git a/app/templates/auth/opt_login.html b/app/templates/auth/opt_login.html
new file mode 100644
index 0000000..9ab59b3
--- /dev/null
+++ b/app/templates/auth/opt_login.html
@@ -0,0 +1,14 @@
+{% extends 'base.html' %}
+{% import 'bootstrap/wtf.html' as wtf %}
+
+{% block app_content %}
+ {{ _('Validation') }}
+
+
+ {{ wtf.quick_form(form) }}
+
+
+
+ {{ _('New User?') }} {{ _('Click to Register!') }}
+ {{ _('Returning User?') }} {{ _('Click to Login!') }}
+{% endblock %}
diff --git a/app/templates/email/otp.html b/app/templates/email/otp.html
new file mode 100644
index 0000000..a2460e2
--- /dev/null
+++ b/app/templates/email/otp.html
@@ -0,0 +1,9 @@
+Dear {{ user.username }},
+
+ Here is your one time passcode
+
{{otp}}
+
+
+If you have not requested an otp, simply ignore this message.
+Sincerely,
+The Microblog Team
diff --git a/app/templates/email/otp.txt b/app/templates/email/otp.txt
new file mode 100644
index 0000000..afc9182
--- /dev/null
+++ b/app/templates/email/otp.txt
@@ -0,0 +1,11 @@
+Dear {{ user.username }},
+
+Here is your one time passcode:
+
+{{otp}}
+
+If you have not requested a otp, simply ignore this message.
+
+Sincerely,
+
+The Microblog Team