diff --git a/server/authentication.py b/server/authentication.py index 5cba7fb..f1ceec3 100644 --- a/server/authentication.py +++ b/server/authentication.py @@ -1,4 +1,4 @@ -import os,sys +import os, sys from base64 import b64encode from Crypto.Hash import SHA256 from Crypto.Protocol.KDF import bcrypt, bcrypt_check @@ -9,10 +9,11 @@ import logging auth_logger = logging.getLogger('AUTH APPLICATION ') auth_logger.root.setLevel(logging.INFO) + class Authetication: ABSOLUTE_PATH = os.path.abspath(os.path.dirname(sys.argv[0])) HOME_DIRECTORY_LOCATION = ABSOLUTE_PATH + "\\home" - CONFIG_DIRECTORY_LOCATION = ABSOLUTE_PATH+ "\\config" + CONFIG_DIRECTORY_LOCATION = ABSOLUTE_PATH + "\\config" USER_INDEX = 0 def __init__(self): @@ -22,14 +23,14 @@ class Authetication: if not os.path.isdir(self.CONFIG_DIRECTORY_LOCATION): os.mkdir(self.CONFIG_DIRECTORY_LOCATION) - if not os.path.isfile(self.CONFIG_DIRECTORY_LOCATION + "\\config.txt") or os.stat(self.CONFIG_DIRECTORY_LOCATION + "\\config.txt").st_size == 0: + if not os.path.isfile(self.CONFIG_DIRECTORY_LOCATION + "\\config.txt") or os.stat( + self.CONFIG_DIRECTORY_LOCATION + "\\config.txt").st_size == 0: data = {} data['index'] = 0 data['user'] = [] with open(self.CONFIG_DIRECTORY_LOCATION + "\\config.txt", 'w+') as outfile: json.dump(data, outfile) - def login(self, username, password): with open(Authetication.CONFIG_DIRECTORY_LOCATION + '\\config.txt') as json_file: data = json.load(json_file) @@ -43,10 +44,9 @@ class Authetication: except ValueError: auth_logger.debug("User NOT logged in: " + username) return '' - auth_logger.debug("User logged in: "+username) + auth_logger.debug("User logged in: " + username) return user['homeDir'] - def checkUserExists(self, username): with open(Authetication.CONFIG_DIRECTORY_LOCATION + '\\config.txt') as json_file: data = json.load(json_file) @@ -57,16 +57,13 @@ class Authetication: return False def initConfig(self): - data = {} - data['index'] = 0 - data['user'] = [] + data = {'index': 0, 'user': []} with open(self.CONFIG_DIRECTORY_LOCATION + "\\config.txt", 'w+') as outfile: json.dump(data, outfile) shutil.rmtree(self.HOME_DIRECTORY_LOCATION) os.mkdir(self.HOME_DIRECTORY_LOCATION) - def saveUser(self, username, password): bytePass = password.encode('utf-8') b64pwd = b64encode(SHA256.new(bytePass).digest()) @@ -78,7 +75,7 @@ class Authetication: user = { 'username': username, 'password': bcrypt_hash.decode('utf-8'), - 'homeDir': data['index']+1 + 'homeDir': data['index'] + 1 } data['index'] = data['index'] + 1 @@ -97,7 +94,3 @@ class Authetication: auth_logger.debug("User NOT saved! Home directory already exists") return True - - - - diff --git a/server/authentication_test.py b/server/authentication_test.py index 2027d95..e0ab9ae 100644 --- a/server/authentication_test.py +++ b/server/authentication_test.py @@ -2,10 +2,10 @@ from authentication import Authetication import json import logging - test_logger = logging.getLogger('TEST ') test_logger.root.setLevel(logging.INFO) + def testSaveUser(username, password): logging.info('STARTING SAVE USER TEST') auth = Authetication() @@ -61,7 +61,8 @@ def testUserExists(username, password): else: logging.info('TEST 2 --> User exists with INVALID user :: PASSED') + if __name__ == '__main__': - testSaveUser("Diósbejglia","Diósbejgli") - testAuth("Diósbejglia","Diósbejgli") - testUserExists("Diósbejglia","Diósbejgli") \ No newline at end of file + testSaveUser("Diósbejglia", "Diósbejgli") + testAuth("Diósbejglia", "Diósbejgli") + testUserExists("Diósbejglia", "Diósbejgli")