From 149c2068305207db557ec1dd42ff4469d4f585a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Sun, 25 Apr 2021 18:40:41 +0200 Subject: [PATCH] fix circular dependency --- server/netwrapper.py | 9 ++++----- server/server.py | 11 +++++++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/server/netwrapper.py b/server/netwrapper.py index 9417a4d..758aaaf 100644 --- a/server/netwrapper.py +++ b/server/netwrapper.py @@ -8,12 +8,12 @@ from Crypto.PublicKey.RSA import RsaKey from Crypto.Random import get_random_bytes from netsim import network_interface -from server import Server +from authentication import Authetication class NetWrapper: - def __init__(self, clientPublicKey: dict, serverPrivateKey: RsaKey, serverInstance: Server): + def __init__(self, clientPublicKey: dict, serverPrivateKey: RsaKey, authenticationInstance: Authetication): self.clientPublicKey = clientPublicKey self.currentClientPublicKey = "".encode('UTF-8') self.serverPrivateKey = serverPrivateKey @@ -21,7 +21,7 @@ class NetWrapper: self.network = network_interface('./', 'A') self.clientAddr = "" self.currentUser = "" - self.serverInstance = serverInstance + self.authenticationInstance = authenticationInstance def serverIdentify(self, msg: bytes) -> None: incommingJson = json.loads(msg.decode('UTF-8')) @@ -77,7 +77,7 @@ class NetWrapper: retcipher = ChaCha20.new(self.cipherkey, nonce=retnonce) plaintext = retcipher.decrypt(retciphertext).decode('UTF-8').split(' ') linsuccess = (not (len(plaintext) != 3 or plaintext[0] != "LIN" or plaintext[ - 1] != self.currentUser)) and self.serverInstance.login(plaintext[1], plaintext[2]) + 1] != self.currentUser)) and self.authenticationInstance.login(plaintext[1], plaintext[2]) if linsuccess: message = "OK".encode('UTF-8') else: @@ -118,7 +118,6 @@ class NetWrapper: return self.recieveEncryptedMessage(msg) def logout(self) -> None: - self.serverInstance.logout() self.clientAddr = "" self.cipherkey = "".encode('UTF-8') self.currentClientPublicKey = "".encode('UTF-8') diff --git a/server/server.py b/server/server.py index a8c8338..16c5c1c 100644 --- a/server/server.py +++ b/server/server.py @@ -18,7 +18,7 @@ class Server: passphrase = input() self.auth = Authetication() self.networkInstance = NetWrapper(self.auth.loadUserPublicKeys(), self.auth.loadServerPrivateKey(passphrase), - self) + self.auth) def login(self, username: str, password: str) -> bool: self.isAuthenticated = True @@ -30,6 +30,7 @@ class Server: return True def logout(self) -> None: + self.networkInstance.logout() self.isAuthenticated = False self.availableServer = False self.homeDirectory = "" @@ -56,7 +57,13 @@ class Server: def execute(self, command: str, firstParam: str = "", secondParam: str = "") -> None: if self.homeDirectory == "" or self.executor.currentDirectory == "" or self.executor.baseDir == "": raise Exception("Directories must not be empty string. Did the user log in?") - if command == "MKD": + if command == "LOUT": + if secondParam != "" or firstParam != "": + self.networkInstance.sendMessage("ERROR".encode('UTF-8')) + else: + self.logout() + self.networkInstance.sendMessage("OK".encode('UTF-8')) + elif command == "MKD": if secondParam != "": self.networkInstance.sendMessage("ERROR".encode('UTF-8')) else: