|
|
@@ -1,8 +1,10 @@ |
|
|
import web |
|
|
import web |
|
|
from views.forms import login_form |
|
|
from views.forms import login_form |
|
|
|
|
|
import models.session |
|
|
import models.user |
|
|
import models.user |
|
|
from views.utils import get_nav_bar |
|
|
from views.utils import get_nav_bar |
|
|
import os, hmac, base64, pickle |
|
|
|
|
|
|
|
|
import random |
|
|
|
|
|
import string |
|
|
import hashlib |
|
|
import hashlib |
|
|
|
|
|
|
|
|
# Get html templates |
|
|
# Get html templates |
|
|
@@ -17,7 +19,7 @@ class Login(): |
|
|
def GET(self): |
|
|
def GET(self): |
|
|
""" |
|
|
""" |
|
|
Show the login page |
|
|
Show the login page |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:return: The login page showing other users if logged in |
|
|
:return: The login page showing other users if logged in |
|
|
""" |
|
|
""" |
|
|
session = web.ctx.session |
|
|
session = web.ctx.session |
|
|
@@ -40,7 +42,7 @@ class Login(): |
|
|
# Validate login credential with database query |
|
|
# Validate login credential with database query |
|
|
password_hash = hashlib.md5(b'TDT4237' + data.password.encode('utf-8')).hexdigest() |
|
|
password_hash = hashlib.md5(b'TDT4237' + data.password.encode('utf-8')).hexdigest() |
|
|
user = models.user.match_user(data.username, password_hash) |
|
|
user = models.user.match_user(data.username, password_hash) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# If there is a matching user/password in the database the user is logged in |
|
|
# If there is a matching user/password in the database the user is logged in |
|
|
if user: |
|
|
if user: |
|
|
self.login(user[1], user[0], data.remember) |
|
|
self.login(user[1], user[0], data.remember) |
|
|
@@ -63,44 +65,34 @@ class Login(): |
|
|
""" |
|
|
""" |
|
|
Validate the rememberme cookie and log in |
|
|
Validate the rememberme cookie and log in |
|
|
""" |
|
|
""" |
|
|
username = "" |
|
|
|
|
|
sign = "" |
|
|
|
|
|
|
|
|
userid = None |
|
|
# If the user selected 'remember me' they log in automatically |
|
|
# If the user selected 'remember me' they log in automatically |
|
|
try: |
|
|
try: |
|
|
# Fetch the users cookies if it exists |
|
|
# Fetch the users cookies if it exists |
|
|
cookies = web.cookies() |
|
|
cookies = web.cookies() |
|
|
# Fetch the remember cookie and convert from string to bytes |
|
|
# Fetch the remember cookie and convert from string to bytes |
|
|
remember_hash = bytes(cookies.remember[2:][:-1], 'ascii') |
|
|
|
|
|
# Decode the hash |
|
|
|
|
|
decode = base64.b64decode(remember_hash) |
|
|
|
|
|
# Load the decoded hash to receive the host signature and the username |
|
|
|
|
|
username, sign = pickle.loads(decode) |
|
|
|
|
|
|
|
|
remember_token = cookies.remember |
|
|
|
|
|
userid = models.session.get_cookie(remember_token) |
|
|
except AttributeError as e: |
|
|
except AttributeError as e: |
|
|
# The user did not have the stored remember me cookie |
|
|
# The user did not have the stored remember me cookie |
|
|
pass |
|
|
pass |
|
|
|
|
|
|
|
|
# If the users signed cookie matches the host signature then log in |
|
|
# If the users signed cookie matches the host signature then log in |
|
|
if self.sign_username(username) == sign: |
|
|
|
|
|
userid = models.user.get_user_id_by_name(username) |
|
|
|
|
|
|
|
|
if userid is not None: |
|
|
|
|
|
username = models.user.get_user_name_by_id(userid) |
|
|
self.login(username, userid, False) |
|
|
self.login(username, userid, False) |
|
|
|
|
|
|
|
|
def rememberme(self): |
|
|
def rememberme(self): |
|
|
""" |
|
|
""" |
|
|
Encode a base64 object consisting of the username signed with the |
|
|
|
|
|
host secret key and the username. Can be reassembled with the |
|
|
|
|
|
hosts secret key to validate user. |
|
|
|
|
|
:return: base64 object consisting of signed username and username |
|
|
|
|
|
|
|
|
Generate a random token for the user, and store it in the database. |
|
|
""" |
|
|
""" |
|
|
session = web.ctx.session |
|
|
session = web.ctx.session |
|
|
creds = [ session.username, self.sign_username(session.username) ] |
|
|
|
|
|
return base64.b64encode(pickle.dumps(creds)) |
|
|
|
|
|
|
|
|
alphabet = string.ascii_uppercase + string.digits |
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
|
|
def sign_username(self, username): |
|
|
|
|
|
""" |
|
|
|
|
|
Sign the current users name with the hosts secret key |
|
|
|
|
|
:return: The users signed name |
|
|
|
|
|
""" |
|
|
|
|
|
secret = base64.b64decode(self.secret) |
|
|
|
|
|
return hmac.HMAC(secret, username.encode('ascii')).hexdigest() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while True: |
|
|
|
|
|
token = ''.join(random.SystemRandom().choice(alphabet) for _ in range(20)) |
|
|
|
|
|
if models.session.get_cookie(token) is None: |
|
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
|
|
models.session.set_cookie(session.userid, token) |
|
|
|
|
|
return token |