2021-04-18 13:36:04 +02:00
|
|
|
import os, sys
|
2021-04-17 19:52:58 +02:00
|
|
|
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)
|
|
|
|
|
2021-04-18 13:36:04 +02:00
|
|
|
|
2021-04-17 19:52:58 +02:00
|
|
|
class Authetication:
|
|
|
|
ABSOLUTE_PATH = os.path.abspath(os.path.dirname(sys.argv[0]))
|
|
|
|
HOME_DIRECTORY_LOCATION = ABSOLUTE_PATH + "\\home"
|
2021-04-18 13:36:04 +02:00
|
|
|
CONFIG_DIRECTORY_LOCATION = ABSOLUTE_PATH + "\\config"
|
2021-04-17 19:52:58 +02:00
|
|
|
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)
|
|
|
|
|
2021-04-18 13:36:04 +02:00
|
|
|
if not os.path.isfile(self.CONFIG_DIRECTORY_LOCATION + "\\config.txt") or os.stat(
|
|
|
|
self.CONFIG_DIRECTORY_LOCATION + "\\config.txt").st_size == 0:
|
2021-04-17 19:52:58 +02:00
|
|
|
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 ''
|
2021-04-18 13:36:04 +02:00
|
|
|
auth_logger.debug("User logged in: " + username)
|
2021-04-17 19:52:58 +02:00
|
|
|
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):
|
2021-04-18 13:36:04 +02:00
|
|
|
data = {'index': 0, 'user': []}
|
2021-04-17 19:52:58 +02:00
|
|
|
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'),
|
2021-04-18 13:36:04 +02:00
|
|
|
'homeDir': data['index'] + 1
|
2021-04-17 19:52:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|