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

This commit is contained in:
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