authentication service + test
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
DESKTOP-DPA61F8\Benedek 2021-04-17 19:52:58 +02:00
parent dc74164d3a
commit c5533e1c5e
2 changed files with 170 additions and 0 deletions

103
server/authentication.py Normal file
View File

@ -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

View File

@ -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")