def __construct_signed_message(self, header, message, key=None, pretty_print=False): """ Creates a signed message Parameters: :param header: top level element for the message data :param message: data to be included :param key: the key with which to sign the message, is none is provided the response key is obtained form the associated application (optional) Returns :return: a dictionary with the message and the authentication code """ signed_message = {header: message} if key is None: application = self.__get_application() if application is not None: key = application.server_response_key if key is not None: message = encode_json(message, pretty_print) signed_message['MAC'] = create_mac(str(key), message) return signed_message return None
def __write_message(self, status_code, header='data', message=None, sign=True, key=None): """ Writes a JSON formatted message to the response body that will be sent to the client Parameters: :param status_code: status code for the HTTP response :param header: top level element for the message data (default: 'data') :param message: data to be sent (optional) :param sign: whether the message should be signed or not (default: True) :param key: the key with which to sign the message, only relevant if the message should be signed, is none is provided the response key is obtained form the associated application (optional) """ self.response.status_int = status_code if status_code not in [204, 205] or message is not None: pretty_print = self.request.get('pretty').lower() == 'true' self.response.content_type = 'application/json' message_json = {header: message} if sign: message_json = self.__construct_signed_message( header, message, key, pretty_print) encoded_message = encode_json(message_json, pretty_print) self.response.write(encoded_message)
def write_signed_error(self, status_code=400, message=None, key=None): """ Sends an error with a message authentication code (if the message is not empty) to the client and aborts the response Parameters: :param status_code: the HTTP status code for the response :param message: the message to add to the response (optional) """ if message is not None: pretty_print = self.request.get('pretty').lower() == 'true' self.response.content_type = 'application/json' message_json = self.__construct_signed_message('error', message, key, pretty_print) encoded_message = encode_json(message_json, pretty_print) self.abort(status_code, detail=encoded_message) else: self.abort(status_code)
def write_signed_error(self, status_code=400, message=None, key=None): """ Sends an error with a message authentication code (if the message is not empty) to the client and aborts the response Parameters: :param status_code: the HTTP status code for the response :param message: the message to add to the response (optional) """ if message is not None: pretty_print = self.request.get('pretty').lower() == 'true' self.response.content_type = 'application/json' message_json = self.__construct_signed_message( 'error', message, key, pretty_print) encoded_message = encode_json(message_json, pretty_print) self.abort(status_code, detail=encoded_message) else: self.abort(status_code)
def __write_message(self, status_code, header='data', message=None, sign=True, key=None): """ Writes a JSON formatted message to the response body that will be sent to the client Parameters: :param status_code: status code for the HTTP response :param header: top level element for the message data (default: 'data') :param message: data to be sent (optional) :param sign: whether the message should be signed or not (default: True) :param key: the key with which to sign the message, only relevant if the message should be signed, is none is provided the response key is obtained form the associated application (optional) """ self.response.status_int = status_code if status_code not in [204, 205] or message is not None: pretty_print = self.request.get('pretty').lower() == 'true' self.response.content_type = 'application/json' message_json = {header: message} if sign: message_json = self.__construct_signed_message(header, message, key, pretty_print) encoded_message = encode_json(message_json, pretty_print) self.response.write(encoded_message)