Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin-Kwan committed Jul 3, 2019
1 parent 097295e commit 8d8ef62
Show file tree
Hide file tree
Showing 20 changed files with 385 additions and 190 deletions.
19 changes: 11 additions & 8 deletions server/src/DatabaseAccessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,30 @@

class DatabaseAccessor():

def selectUsername(self, username):
def selectUsername(self, user):
username = user.getUsername()
cursor.execute("SELECT Username from Users WHERE Username = %s", (username,))
selectedUsername = cursor.fetchone()
return self.handleQueryReturn(selectedUsername)

# selects user's hashed password based on username
def selectHashedPassword(self, username):
def selectHashedPassword(self, user):
username = user.getUsername()
cursor.execute("SELECT HashedPass from Users WHERE Username = %s", (username,))
selectedHashedPassword = cursor.fetchone()
return self.handleQueryReturn(selectedHashedPassword)

def selectUserId(self, userId):
cursor.execute("SELECT UserId from Users WHERE UserId = %s", (userId,))
def selectUserId(self, user):
username = user.getUsername()
cursor.execute("SELECT UserId from Users WHERE Username = %s", (username,))
selectedUserId = cursor.fetchone()
return self.handleQueryReturn(selectedUserId)

def insertUserInfo(self, username, hashedPassword, userId):
def insertUserInfo(self, user):
username = user.getUsername()
hashedPassword = user.getHashedPassword()
userId = user.getUserId()
cursor.execute("INSERT INTO Users(Username, HashedPass, UserId) VALUES(%s, %s, %s)", (username, hashedPassword, userId))
connection.commit()
username = ''
hashedPassword = ''

def clearDatabase(self):
cursor.execute('DELETE FROM Users')
Expand Down
66 changes: 38 additions & 28 deletions server/src/InputHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@

import bcrypt
from DatabaseAccessor import DatabaseAccessor
from ResultCodes import ResultCodes
from ResultCodes import ResultCodes

DBA = DatabaseAccessor()
resultCodes = ResultCodes()

class InputHandler():

# def handleEmptyFields(self, username, password):
# isUsernameEmpty = self.checkTextEmpty(username)
# isPasswordEmpty = self.checkTextEmpty(password)
#
# if(isUsernameEmpty and isPasswordEmpty):
# return resultCodes.ERROR_EMPTY_FIELDS
# elif(isUsernameEmpty):
# return resultCodes.ERROR_EMPTY_USERNAME
# elif(isPasswordEmpty):
# return resultCodes.ERROR_EMPTY_PASSWORD
# else:
# return resultCodes.SUCCESS_FIELDS_FILLED

def handleEmptyFields(self, username, password):
def checkInputNull(self, username, password):
if(username == None and password == None):
return resultCodes.ERROR_EMPTY_FIELDS
elif(username == None and password != None):
return resultCodes.ERROR_EMPTY_USERNAME
elif(password == None and username != None):
return resultCodes.ERROR_EMPTY_PASSWORD
return resultCodes.SUCCESS_FIELDS_FILLED

def handleEmptyFields(self, user):

username = user.getUsername()
password = user.getTextPassword()

isUsernameEmpty = self.checkTextEmpty(username)
isPasswordEmpty = self.checkTextEmpty(password)

Expand All @@ -38,7 +38,16 @@ def handleEmptyFields(self, username, password):
else:
return resultCodes.SUCCESS_FIELDS_FILLED

def handleInputLengthChecks(self, username, password):
def checkTextEmpty(self, text):
if(len(text) == 0):
return True
return False

def handleInputLengthChecks(self, user):

username = user.getUsername()
password = user.getTextPassword()

isUsernameLengthOk = self.checkInputLength('USERNAME', username)
isPasswordLengthOk = self.checkInputLength('PASSWORD', password)

Expand All @@ -51,27 +60,28 @@ def handleInputLengthChecks(self, username, password):
else:
return resultCodes.ERROR_USERNAME_LENGTH_INVALID

def checkTextEmpty(self, text):
return text == ''

def checkInputLength(self, inputType, input):
if(inputType == 'USERNAME'):
return len(input) >= 6 and len(input) <= 35
elif(inputType == 'PASSWORD'):
return len(input) >= 8 and len(input) <= 65

def checkForInvalidUsernameChars(self, username):
def checkForInvalidUsernameChars(self, user):
username = user.getUsername()

for currentChar in username:
if(currentChar.isalpha() == False and currentChar.isdigit() == False):
return False
return True

def checkForExistingUsername(self, username):
selectedUsername = DBA.selectUsername(username)
return selectedUsername == username
def checkForExistingUsername(self, user):
selectedUsername = DBA.selectUsername(user)
return selectedUsername == user.getUsername()

def verifyPassword(self, user):
password = user.getTextPassword().encode('utf-8')
selectedHashedPassword = DBA.selectHashedPassword(user).encode('utf-8')

'''test!'''
def verifyPassword(self, username, password):
selectedHashedPassword = DBA.selectHashedPassword(username)
isPasswordCorrect = bcrypt.check_password_hash(selectedHashedPassword, password)
return isPasswordCorrect
if(bcrypt.checkpw(password, selectedHashedPassword) == False):
return False
return True
19 changes: 10 additions & 9 deletions server/src/User.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class User():
def __init__(self, username, textPassword):
self.username = username
self.textPassword = textPassword
self.hashedPassword = ''
self.userId = str(uuid.uuid4())
self.securityToken = ''
self.hashedPassword = None
self.userId = None
self.securityToken = None

def getUsername(self):
return self.username
Expand All @@ -27,15 +27,16 @@ def getUserId(self):
def getSecurityToken(self):
return self.securityToken

# update User Id!


def updateUserId(self, userId):
self.userId = userId

def encryptAndUpdatePassword(self, password):
hashedPassword = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt())
self.hashedPassword = str(hashedPassword)
hashedPassword = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
# generated password is encoded so must decode before storing in db
self.hashedPassword = hashedPassword.decode()

# generate and update user id!
def generateAndUpdateUserId(self):
self.userId = str(uuid.uuid4())

def generateAndUpdateSecurityToken(self):
username = self.getUsername()
Expand Down
Binary file modified server/src/__pycache__/DatabaseAccessor.cpython-36.pyc
Binary file not shown.
Binary file modified server/src/__pycache__/InputHandler.cpython-36.pyc
Binary file not shown.
Binary file modified server/src/__pycache__/User.cpython-36.pyc
Binary file not shown.
Binary file modified server/src/__pycache__/index.cpython-36.pyc
Binary file not shown.
27 changes: 16 additions & 11 deletions server/src/controllers/LoginController.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,38 @@
from ResultCodes import ResultCodes
from User import User

inputHandler = InputHandler()
resultCodes = ResultCodes()
DBA = DatabaseAccessor()

class LoginController():

def handleUserLogin(self, username, password):

# check if inputs are null
fieldNullCheckResult = inputHandler.checkInputNull(username, password)
if(fieldNullCheckResult != resultCodes.SUCCESS_FIELDS_FILLED):
return fieldNullCheckResult

user = self.getUser(str(username.lower()), str(password))

# check if fields are empty
fieldEmptyCheckResult = inputHandler.handleEmptyFields(user.getUsername(), user.getTextPassword())
# check if fields are empty strings
fieldEmptyCheckResult = inputHandler.handleEmptyFields(user)
if(fieldEmptyCheckResult != resultCodes.SUCCESS_FIELDS_FILLED):
return fieldEmptyCheckResult

# check if user exists & check if input password matches user's password
doesUsernameExist = inputHandler.checkForExistingUsername(user.getUsername())
isPasswordCorrect = inputHandler.verifyPassword(user.getUsername(), user.getTextPassword())
doesUsernameExist = inputHandler.checkForExistingUsername(user)
isPasswordCorrect = inputHandler.verifyPassword(user)
if(doesUsernameExist == False or isPasswordCorrect == False):
return resultCodes.ERROR_INVALID_USERNAME_OR_PASSWORD

# at this point, generate security token and return it
# - going to have user id problems



userId = DBA.selectUserId(user)
user.updateUserId(userId)
user.generateAndUpdateSecurityToken()
print(user.securityToken)

securityToken = user.getSecurityToken()

return securityToken

def getUser(self, username, password):
user = User(username, password)
Expand Down
28 changes: 14 additions & 14 deletions server/src/controllers/SignUpController.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,47 @@
from User import User
import uuid

inputHandler = InputHandler()
DBA = DatabaseAccessor()
resultCodes = ResultCodes()
inputHandler = InputHandler()
resultCodes = ResultCodes()
DBA = DatabaseAccessor()

class SignUpController():

def handleUserSignUp(self, username, password):

user = self.getUser(str(username.lower()), str(password))
# check if inputs are null
fieldNullCheckResult = inputHandler.checkInputNull(username, password)
if(fieldNullCheckResult != resultCodes.SUCCESS_FIELDS_FILLED):
return fieldNullCheckResult

# # check if fields are empty
# fieldEmptyCheckResult = inputHandler.handleEmptyFields(user.getUsername(), user.getTextPassword())
# if(fieldEmptyCheckResult != resultCodes.SUCCESS_FIELDS_FILLED):
# return fieldEmptyCheckResult
user = self.getUser(str(username.lower()), str(password))

# pass user
# check if inputs are empty strings
fieldEmptyCheckResult = inputHandler.handleEmptyFields(user)
if(fieldEmptyCheckResult != resultCodes.SUCCESS_FIELDS_FILLED):
return fieldEmptyCheckResult


# check for proper string input lengths
inputLengthResult = inputHandler.handleInputLengthChecks(user.getUsername(), user.getTextPassword())
inputLengthResult = inputHandler.handleInputLengthChecks(user)
if(inputLengthResult != resultCodes.SUCCESS_USERNAME_PASSWORD_LENGTH):
return inputLengthResult

# check for invalid characters in inputs
isUsernameCharsValid = inputHandler.checkForInvalidUsernameChars(user.getUsername())
isUsernameCharsValid = inputHandler.checkForInvalidUsernameChars(user)
if(isUsernameCharsValid == False):
return resultCodes.ERROR_INVALID_USERNAME_CHARS

# check if username already exists
doesUsernameExist = inputHandler.checkForExistingUsername(user.getUsername())
doesUsernameExist = inputHandler.checkForExistingUsername(user)
if(doesUsernameExist):
return resultCodes.ERROR_DUPLICATE_USERNAME

# insert user info into db
DBA.insertUserInfo(user.getUsername(), user.getHashedPassword(), user.getUserId())
DBA.insertUserInfo(user)
return resultCodes.SUCCESS

def getUser(self, username, password):
user = User(username, password)
user.encryptAndUpdatePassword(password)
user.generateAndUpdateUserId()
return user
Binary file modified server/src/controllers/__pycache__/LoginController.cpython-36.pyc
Binary file not shown.
Binary file not shown.
1 change: 0 additions & 1 deletion server/src/todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#Server Refactoring

- double password confirmation?
- start working on login controller (eventually use json tokens)

- change button color?
- Need Google Captcha
Binary file not shown.
Binary file modified server/test/__pycache__/test_InputHandler.cpython-36-PYTEST.pyc
Binary file not shown.
Binary file not shown.
Binary file modified server/test/__pycache__/test_User.cpython-36-PYTEST.pyc
Binary file not shown.
Loading

0 comments on commit 8d8ef62

Please sign in to comment.