-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
auth.py
173 lines (138 loc) · 4.72 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from app import app
from flask import request, abort, flash, redirect, render_template
from flask_login import (
LoginManager,
login_user,
current_user,
logout_user,
confirm_login,
)
from flask_wtf import FlaskForm, RecaptchaField
from database import User
from wtforms.fields import StringField, BooleanField, PasswordField
from wtforms.validators import Required, Length, EqualTo, Email
from util import authenticate_user, create_user, lookup_user
login_manager = LoginManager(app)
@login_manager.user_loader
def load_user(user_id):
return User.objects(username__iexact=user_id).first()
login_manager.login_view = "login"
login_manager.refresh_view = "reauth"
class AuthForm(FlaskForm):
username = StringField(
"Username*",
validators=[
Required(message="Please enter your username"),
Length(min=3, max=16),
],
)
password = PasswordField(
"Password*",
validators=[Required(message="Please enter your password"), Length(min=8)],
)
remember = BooleanField("Remember Account?", validators=[], default=True)
def validate(self):
rv = FlaskForm.validate(self)
if rv is None:
return False
user = authenticate_user(self.username.data, self.password.data)
if user is None:
self.username.errors.append("Invalid username or password")
return False
else:
self.user = user
return True
class ReAuthForm(FlaskForm):
password = PasswordField(
"Password*",
validators=[Required(message="Please enter your password"), Length(min=8)],
)
def validate(self):
rv = FlaskForm.validate(self)
if rv is None:
return False
user = authenticate_user(current_user.username, self.password.data)
if user is None:
return False
return True
class RegForm(FlaskForm):
username = StringField(
"Username*",
validators=[
Required(message="Please enter your username"),
Length(min=3, max=16),
],
)
email = StringField("Email", validators=[Email()])
password = PasswordField(
"Password*",
validators=[Required(message="Please enter your password"), Length(min=8)],
)
password2 = PasswordField(
"Repeat Password*",
validators=[EqualTo("password", message="Passwords do not match"), Required()],
)
recaptcha = RecaptchaField()
def validate(self):
rv = FlaskForm.validate(self)
if rv is None:
return False
user = User.objects(username__iexact=self.username.data).first()
if user is None:
return True
else:
self.username.errors.append("Username is already taken - try again")
return False
class ForgotPasswordForm(FlaskForm):
username = RegForm.username
def validate(self):
user = lookup_user(self.username.data)
if user is None:
self.username.errors.append("Username does not exist")
return False
if user.email is None:
self.username.errors.append("No email on file, cannot be recovered")
return False
return True
@app.route("/auth/login", methods=("POST", "GET"))
def login():
form = AuthForm(request.form)
if current_user.is_authenticated:
return redirect("/")
if request.method == "POST" and form.validate():
user = form.user
login_user(user, remember=form.remember.data)
return redirect("/")
return render_template("login.html", form=form, title="Login")
@app.route("/auth/logout")
def logout():
if not current_user.is_authenticated:
abort(403)
logout_user()
flash("You have been logged out", "info")
return redirect("/")
@app.route("/auth/register", methods=("POST", "GET"))
def register():
form = RegForm()
if form.validate_on_submit():
create_user(**form.data)
flash("Thanks for registering!", "info")
return redirect("/")
return render_template("register.html", form=form, title="Register")
@app.route("/auth/reauth", methods=("POST", "GET"))
def reauth():
form = ReAuthForm(request.form)
if form.validate_on_submit():
confirm_login()
return redirect(request.args.get("next", "/"))
else:
return render_template("reauth.html", form=form, title="ReAuthenticate")
# @app.route('/auth/forgotpassword', methods=('POST', 'GET'))
def pwreset():
form = ForgotPasswordForm(request.form)
if form.validate_on_submit():
# Email Link to reset password
pass
return render_template("pwreset.html", form=form, title="Forgot Passowrd")
pass
login_manager.login_view = login