diff --git a/server/authentication.py b/server/authentication.py index 3610e9c..1f6c2b0 100644 --- a/server/authentication.py +++ b/server/authentication.py @@ -3,6 +3,7 @@ import logging import os import shutil import sys +import binascii from base64 import b64encode from Crypto.PublicKey import RSA from Crypto.Hash import SHA256 @@ -19,6 +20,7 @@ class Authetication: PRIVATE_KEY_DIRECTORY_LOCATION = CONFIG_DIRECTORY_LOCATION + os.path.sep + "private_keys" USER_INDEX = 0 + def __init__(self): if not os.path.isdir(self.HOME_DIRECTORY_LOCATION): os.mkdir(self.HOME_DIRECTORY_LOCATION) @@ -35,6 +37,7 @@ class Authetication: with open(self.CONFIG_FILE_LOCATION, 'w+') as outfile: json.dump(data, outfile) + def login(self, username: str, password: str) -> str: with open(self.CONFIG_FILE_LOCATION) as json_file: data = json.load(json_file) @@ -51,6 +54,7 @@ class Authetication: auth_logger.debug("User logged in: " + username) return user['homeDir'] + def checkUserExists(self, username: str) -> bool: with open(self.CONFIG_FILE_LOCATION) as json_file: data = json.load(json_file) @@ -60,6 +64,7 @@ class Authetication: return True return False + def initConfig(self): data = {'index': 0, 'user': []} with open(self.CONFIG_FILE_LOCATION, 'w+') as outfile: @@ -78,13 +83,15 @@ class Authetication: private_key = RSA.generate(2048) public_key = private_key.publickey() - private_key_value = str(private_key.export_key('DER', passphrase=user_passphrase, pkcs=8)) - public_key_value = str(public_key.export_key('DER', pkcs=8)) + private_key_value = bytes.hex(private_key.exportKey('DER', passphrase=user_passphrase, pkcs=8)) + public_key_value = bytes.hex(public_key.exportKey('DER', pkcs=8)) + ##Save private key in separate file user_privatekey = {'passphrase': user_passphrase, 'privateClientKey': private_key_value, 'publicServerKey': public_server_key} with open(self.PRIVATE_KEY_DIRECTORY_LOCATION + os.path.sep + str(data['index']) + '.txt', 'w+') as outfile: json.dump(user_privatekey, outfile) + outfile.close() ##Save public key in users for user in data['user']: @@ -93,11 +100,15 @@ class Authetication: with open(self.CONFIG_FILE_LOCATION, 'w') as outfile: json.dump(data, outfile) break + outfile.close() return True else: + return False + + def saveUser(self, username: str, password: str) -> bool: bytePass = password.encode('utf-8') b64pwd = b64encode(SHA256.new(bytePass).digest())