|
|
|
@@ -14,6 +14,10 @@ render = web.template.render('templates/') |
|
|
|
# The remember cookie should be valid for a week |
|
|
|
remember_timeout = 3600*24*7 |
|
|
|
|
|
|
|
# The timeout between login attempts, after the 3rd incorrect one |
|
|
|
login_timeout = 60 |
|
|
|
login_attempts_threshold = 2 |
|
|
|
|
|
|
|
|
|
|
|
class Login(): |
|
|
|
|
|
|
|
@@ -47,11 +51,24 @@ class Login(): |
|
|
|
# Validate login credential with database query |
|
|
|
user = models.user.get_user(data.username) |
|
|
|
|
|
|
|
if bcrypt.checkpw(data.password.encode('UTF-8'), user[2].encode('UTF-8')): |
|
|
|
self.login(user[1], user[0], data.remember) |
|
|
|
if user is None: |
|
|
|
return render.login(nav, login_form, "- User authentication failed") |
|
|
|
|
|
|
|
userid, username, password_hash, login_attempts, last_login_attempt = user |
|
|
|
|
|
|
|
if login_attempts > login_attempts_threshold and last_login_attempt + login_timeout > time.time(): |
|
|
|
return render.login(nav, login_form, "- There have been too many incorrect login attempts for your account. You have to wait a minute before you can log in.") |
|
|
|
|
|
|
|
if bcrypt.checkpw(data.password.encode('UTF-8'), password_hash.encode('UTF-8')): |
|
|
|
models.user.set_login_attempts(userid, 0, time.time()) |
|
|
|
self.login(username, userid, data.remember) |
|
|
|
raise web.seeother("/") |
|
|
|
else: |
|
|
|
return render.login(nav, login_form, "- User authentication failed") |
|
|
|
models.user.set_login_attempts(userid, login_attempts+1, time.time()) |
|
|
|
if login_attempts == login_attempts_threshold: |
|
|
|
return render.login(nav, login_form, "- Too many incorrect login attempts. You have to wait a minute before trying again.") |
|
|
|
else: |
|
|
|
return render.login(nav, login_form, "- User authentication failed") |
|
|
|
|
|
|
|
def login(self, username, userid, remember): |
|
|
|
""" |
|
|
|
|