def publish(self, token: str, room_data: str, current=None): """ Publishes a room. The client sends a room to the server and verifies the token against the authentication server. :param token Authentication token :param room_data JSON string representing the room """ if not self._auth.isValid(token): logging.warning("invalid token: %s", token) raise IceGauntlet.Unauthorized() room_actual_data = json.loads(room_data) if set(room_actual_data.keys()) != set(("data", "room")): logging.warning("invalid format for room") raise IceGauntlet.InvalidRoomFormat() room_name = room_actual_data["room"] if not isinstance(room_name, str): logging.warning("no room name present on room data") raise IceGauntlet.InvalidRoomFormat() room_file_path = self._get_room_file(room_name) if os.path.isfile(room_file_path): logging.warning("room %s already exists", room_name) raise IceGauntlet.RoomAlreadyExists() logging.info("registering room %s", room_name) with open(room_file_path, "w") as room_file: room_file.write(room_data)
def getNewToken(self, user, passwordHash, current=None): '''Create new auth token''' logging.debug(f'New token requested by {user}') if user not in self._users_: raise IceGauntlet.Unauthorized() current_hash = self._users_[user].get(PASSWORD_HASH, None) if not current_hash: # User auth is empty raise IceGauntlet.Unauthorized() if current_hash != passwordHash: raise IceGauntlet.Unauthorized() current_token = self._users_[user].get(CURRENT_TOKEN, None) if current_token: # pylint: disable=W0702 try: self._active_tokens_.remove(current_token) except: # Token is already inactive! pass # pylint: enable=W0702 new_token = _build_token_() self._users_[user][CURRENT_TOKEN] = new_token self.__commit__() self._active_tokens_.add(new_token) return new_token
def changePassword(self, user, currentPassHash, newPassHash, current=None): '''Set/Change user password''' logging.debug(f'Change password requested by {user}') if user not in self._users_: raise IceGauntlet.Unauthorized() current_hash = self._users_[user].get(PASSWORD_HASH, None) if current_hash is None: # User auth is empty self._users_[user][CURRENT_TOKEN] = _build_token_() else: if current_hash != currentPassHash: raise IceGauntlet.Unauthorized() self._users_[user][PASSWORD_HASH] = newPassHash self.__commit__()
def publish(self, token, room_data, current=None): '''This metod publish a new map if it doesn't exists''' file_exists = False user = self.authserver.getOwner(token) room_data = ast.literal_eval(room_data) try: data = room_data["data"] room_data["room"] except Exception: print("Error: {}".format("WrongRoomFormat Exception")) raise IceGauntlet.WrongRoomFormat() else: map_list = glob.glob(os.path.join('maps', '*.json')) for i in map_list: with open(str(i)) as maps_file: data_map = maps_file.read() data_map = json.loads(data_map) if data_map["data"] == data: if data_map["user"] != str(user): print("Error: {}".format( "Room Already Exists Exception")) raise IceGauntlet.RoomAlreadyExists() else: file_exists = True break if not file_exists: i = len(map_list) + random.randrange(1, 1000000) map_name = "maps/level" + str(i) + ".json" with open((map_name), "w") as maps_file: json.dump(room_data, maps_file) with open(map_name) as maps_file2: data_map = maps_file2.read() maps = json.loads(data_map) maps["user"] = user with open((map_name), "w") as maps_file3: json.dump(maps, maps_file3) server_sync_prx = IceGauntlet.RoomManagerSyncPrx.uncheckedCast( self.room_manager_sync_channel_prx.getPublisher()) server_sync_prx.newRoom(room_data["room"], self.id)
def remove(self, token: str, room_name: str, current=None): """ Removes a room from the server :param token Authentication token :param room_name Name of the room to be removed """ if not self._auth.isValid(token): logging.warning("invalid token: %s", token) raise IceGauntlet.Unauthorized() room_file_path = self._get_room_file(room_name) if not os.path.isfile(room_file_path): logging.warning("room %s does not exist", room_name) raise IceGauntlet.RoomNotExists() logging.info("deleting room %s", room_name) os.unlink(room_file_path)
def getOwner(self, token, current=None): '''Return user name of the token owner''' for i in self._users_: if token == self._users_[i][CURRENT_TOKEN]: user = self._users_[i] return i if user not in self._users_: raise IceGauntlet.Unauthorized()
def getRoom(self, current=None) -> str: """ Obtains the data for a random room uploaded to the server :return The JSON string representing the room """ matching_paths = glob.glob( os.path.join(self._get_data_dir(), "room_*.json")) if not matching_paths: logging.warning("no rooms were found in the data directory") raise IceGauntlet.RoomNotExists() with open(random.choice(matching_paths), "r") as room_file: return room_file.read()
def getRoom(self, current=None): '''This method obtain the map''' try: map_list = glob.glob(os.path.join('maps', '*.json')) random_map = random.randrange(0, len(map_list)) path = (map_list.pop(random_map)) with open(str(path)) as maps_file: user_data = maps_file.read() user_data = json.loads(user_data) user_data.pop("user") except Exception: print("Error: {}".format("Room Not Exists Exception")) raise IceGauntlet.RoomNotExists() return str(user_data)
def remove(self, token, roomName, current=None): '''This method remove a room from an authorized user''' room_found = False user = self.authserver.getOwner(token) map_list = glob.glob(os.path.join('maps', '*.json')) for i in map_list: map = str(i) with open(map) as maps_file: data_map = maps_file.read() data_map = json.loads(data_map) if (data_map["room"] == str(roomName) and str(user) == data_map["user"]): os.remove(map) room_found = True break if not room_found: print("Error: {}".format("Room Not Exists Exception ")) raise IceGauntlet.RoomNotExists() server_sync_prx = IceGauntlet.RoomManagerSyncPrx.uncheckedCast( self.room_manager_sync_channel_prx.getPublisher()) server_sync_prx.removedRoom(roomName)