def _decrypt(self, text, _id, exception=None): text = to_binary(text) plain_text = self.cipher.decrypt(base64.b64decode(text)) padding = byte2int(plain_text[-1]) content = plain_text[16:-padding] xml_length = socket.ntohl(struct.unpack(b'I', content[:4])[0]) xml_content = to_text(content[4:xml_length + 4]) from_id = to_text(content[xml_length + 4:]) if from_id != _id: exception = exception or Exception raise exception() return xml_content
def __init__(self, btn_orientation, btn_json_list=list, **kwargs): btn_orientation = to_text(btn_orientation) assert btn_orientation in ('0', '1') super(BtnActionCardBody, self).__init__(btn_orientation=btn_orientation, btn_json_list=btn_json_list, **kwargs)
def parse_message(self, msg, signature, timestamp, nonce): message = self.crypto.decrypt_message(msg, signature, timestamp, nonce) try: message = json.loads(to_text(message)) self.proc_message(message) except Exception as e: logger.error("proc_message error %s %s", message, e) return message
def __repr__(self): _repr = '{klass}({code}, {msg})'.format(klass=self.__class__.__name__, code=self.errcode, msg=self.errmsg) if six.PY2: return to_binary(_repr) else: return to_text(_repr)
def __str__(self): _repr = 'Error code: {code}, message: {msg}'.format(code=self.errcode, msg=self.errmsg) if six.PY2: return to_binary(_repr) else: return to_text(_repr)
def _decrypt_message(self, msg, signature, timestamp, nonce, crypto_class=None): if not isinstance(msg, dict): msg = json.loads(to_text(msg)) encrypt = msg['encrypt'] return self._decrypt_encrypt_str(signature, timestamp, nonce, encrypt, crypto_class)
def _encrypt_message(self, msg, nonce=None, timestamp=None, crypto_class=None): timestamp = timestamp or int(time.time() * 1000) timestamp = to_text(timestamp) if nonce is None: nonce = random_string() assert self.key is not None pc = crypto_class(self.key) encrypt = to_text(pc.encrypt(msg, self._id)) signature = _get_signature(self.token, timestamp, nonce, encrypt) result = dict() result['msg_signature'] = signature result['encrypt'] = encrypt result['timeStamp'] = timestamp result['nonce'] = nonce return result
def test_decrypt_encrypt_str(self): from dingtalk.core.utils import to_text signature = '5a65ceeef9aab2d149439f82dc191dd6c5cbe2c0' timestamp = '1445827045067' nonce = 'nEXhMP4r' encrypt_str = '1a3NBxmCFwkCJvfoQ7WhJHB+iX3qHPsc9JbaDznE1i03peOk1LaOQoRz3+nlyGNhwmwJ3vDMG+OzrHMeiZI7gTRWVdUBm' \ 'fxjZ8Ej23JVYa9VrYeJ5as7XM/ZpulX8NEQis44w53h1qAgnC3PRzM7Zc/D6Ibr0rgUathB6zRHP8PYrfgnNOS9PhSBdH' \ 'legK+AGGanfwjXuQ9+0pZcy0w9lQ==' crypto = DingTalkCrypto(self.token, self.encoding_aes_key, self.suite_key) msg = crypto.decrypt_encrypt_str(signature, timestamp, nonce, encrypt_str) msg_dict = json.loads(to_text(msg)) self.assertEqual('check_create_suite_url', msg_dict['EventType']) self.assertEqual('LPIdSnlF', msg_dict['Random']) self.assertEqual(self.suite_key, msg_dict['TestSuiteKey'])
def __init__(self, title, markdown, btn_orientation, btn_list=(), **kwargs): """ 独立跳转ActionCard消息 :param title: 透出到会话列表和通知的文案 :param markdown: 消息内容,支持markdown :param btn_orientation: 按钮排列方式,竖直排列(0),横向排列(1) :param btn_json_list: 按钮列表 :param kwargs: """ btn_orientation = to_text(btn_orientation) assert btn_orientation in ('0', '1') super(BtnActionCardBody, self).__init__(title=title, markdown=markdown, btn_orientation=btn_orientation, btn_json_list=list(btn_list), **kwargs)
def get(self, key, default=None): key = self.key_name(key) value = self.kvdb.get(key) if value is None: return default return json.loads(to_text(value))