From c5533e1c5ecdacaa27f9e74f955bdd55e9f1a8f3 Mon Sep 17 00:00:00 2001 From: "DESKTOP-DPA61F8\\Benedek" Date: Sat, 17 Apr 2021 19:52:58 +0200 Subject: [PATCH] authentication service + test --- server/authentication.py | 103 ++++++++++++++++++++++++++++++++++ server/authentication_test.py | 67 ++++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 server/authentication.py create mode 100644 server/authentication_test.py diff --git a/server/authentication.py b/server/authentication.py new file mode 100644 index 0000000..5cba7fb --- /dev/null +++ b/server/authentication.py @@ -0,0 +1,103 @@ +import os,sys +from base64 import b64encode +from Crypto.Hash import SHA256 +from Crypto.Protocol.KDF import bcrypt, bcrypt_check +import json +import shutil +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" + USER_INDEX = 0 + + def __init__(self): + if not os.path.isdir(self.HOME_DIRECTORY_LOCATION): + os.mkdir(self.HOME_DIRECTORY_LOCATION) + + 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: + 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) + + for user in data['user']: + if username == user['username']: + b64pwd = b64encode(SHA256.new(password.encode('utf-8')).digest()) + try: + b64pwd = b64encode(SHA256.new(password.encode('utf-8')).digest()) + bcrypt_check(b64pwd, user['password'].encode('utf-8')) + except ValueError: + auth_logger.debug("User NOT logged in: " + username) + return '' + 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) + + for user in data['user']: + if username == user['username']: + return True + return False + + def initConfig(self): + data = {} + data['index'] = 0 + data['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()) + bcrypt_hash = bcrypt(b64pwd, 12) + + with open(Authetication.CONFIG_DIRECTORY_LOCATION + '\\config.txt') as json_file: + data = json.load(json_file) + + user = { + 'username': username, + 'password': bcrypt_hash.decode('utf-8'), + 'homeDir': data['index']+1 + } + + data['index'] = data['index'] + 1 + + if self.checkUserExists(username): + auth_logger.debug("User NOT saved! This username already exists!") + return False + else: + if not os.path.isdir(self.HOME_DIRECTORY_LOCATION + "\\" + str(user['homeDir'])): + os.mkdir(self.HOME_DIRECTORY_LOCATION + "\\" + str(user['homeDir'])) + data['user'].append(user) + with open(self.CONFIG_DIRECTORY_LOCATION + '\\config.txt', 'w') as outfile: + json.dump(data, outfile) + auth_logger.debug("User saved!") + else: + auth_logger.debug("User NOT saved! Home directory already exists") + + return True + + + + diff --git a/server/authentication_test.py b/server/authentication_test.py new file mode 100644 index 0000000..2027d95 --- /dev/null +++ b/server/authentication_test.py @@ -0,0 +1,67 @@ +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() + auth.initConfig() + result = auth.saveUser(username, password) + check = False + + with open(auth.CONFIG_DIRECTORY_LOCATION + '\\config.txt') as json_file: + data = json.load(json_file) + + for user in data['user']: + if username == user['username']: + check = True + + if result and check: + test_logger.info('TEST 1 ---> Save VALID user :: PASSED') + else: + test_logger.info('TEST 1 ---> Save VALID user :: FAILED') + + +def testAuth(username, password): + logging.info('STARTING AUTHENTICATION TEST') + auth = Authetication() + auth.initConfig() + auth.saveUser(username, password) + homeDir = auth.login(username, password) + + if homeDir == 1: + test_logger.info('TEST 1 --> Authentication test with VALID :: PASSED') + else: + test_logger.info('TEST 1 --> Authentication test with VALID :: FAILED') + + homeDir = auth.login(username, "") + if homeDir == "": + test_logger.info('TEST 2 --> Authentication test with INVALID :: PASSED') + else: + test_logger.info('TEST 2 --> Authentication test with INVALID ::FAILED') + + +def testUserExists(username, password): + logging.info('STARTING USER EXISTS TEST') + auth = Authetication() + auth.initConfig() + auth.saveUser(username, password) + + if auth.checkUserExists(username): + logging.info('TEST 1 --> User exists with VALID user :: PASSED') + else: + logging.info('TEST 1 --> User exists with VALID user :: FAILED') + + if auth.checkUserExists(""): + logging.info('TEST 2 --> User exists with INVALID user :: FAILED') + 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