def AuthenticateUser(self, adminSocket, adminAddress):
        # Receive a username from the client
        username = Connection().ReceiveMessage(adminSocket)
        if username == None:
            return 2  # Connection closed by client code
        # Receive a password from the client
        password = Connection().ReceiveMessage(adminSocket)
        # Decode the username and password to utf-8 format
        username = username.decode("utf-8")
        password = password.decode("utf-8")
        # If the username does not exist:
        if username not in self.users:
            print("Username not found")
            return 0  # Username or password incorrect code

        # Load the salt and password hash of the given user
        salt = self.users[username]["salt"]
        knownHash = self.users[username]["password"]
        # Generate a new hash with the client's password
        newHash = self.GetPasswordHash(password, salt)
        # If the hashes are the same:
        if newHash == knownHash:
            # Authorize user
            print("Client at ", adminAddress, " authorized")
            return 1  # Username and password correct code
        else:
            # Do not authorize user
            print("Client at ", adminAddress, " not authorized")
            return 0  # Username or password incorrect code
Exemple #2
0
 def ListenToUser(self):
     # Run until the connection is closed
     while True:
         # Receive message from the admin application
         data = Connection().ReceiveMessage(self.socket)
         print(data)
         # If the connection was closed:
         if data == None:
             print("Closing thread...")
             # Close the socket
             self.socket.close()
             # End the infinite loop (close the thread)
             return
         else:
             # Handle the received request
             self.HandleUserRequest(data.decode("utf-8"))