Esempio n. 1
0
 def wrapper(self, *args,**kwargs):
     headers =  self.request.headers
     access_token = headers.get('Authorization',None)
     reg_tcp_client = ServiceMgrCacher().get_connection(US_REGISTER,PT_TCP)
     validate = verify_access_token(reg_tcp_client,access_token)
     if not validate:
         self.set_status(401, "Unauthorized(access token invalid)")
         return
     kwargs.setdefault('access_token', access_token)
     func(self, *args, **kwargs)
Esempio n. 2
0
def bridge_message_process(xmpp_client, send_jid, body):
    """
    xmpp消息代理发送
    :param xmpp_client: 发送者xmpp客户端
    :param send_jid: 发送者jid
    :param body: 发送的body
    :return: None
    """
    log_params = "from_jid:%s, body:%s" % (send_jid, body)

    # check params
    if not 'access_token' in body\
            or not 'to_jid' in body\
            or not JidMgr().is_jid(send_jid)\
            or not JidMgr().is_jid(body['to_jid']):
        res_body = {'result': error_code.ERROR_PARAMS_ERROR}
        xmpp_client.send_async(JID(send_jid), xmpp.S_C_RES_BRIDGE_MESSGE, res_body)
        logger.info("RpcHandler::bridge_message ERROR_WRONG_PARAMS, %s" % log_params)
        return True

    access_token = body['access_token']
    to_jid = body['to_jid']
    bridge_body = body['body']

    rgst_rpc = ServiceMgrCacher().get_connection(US_REGISTER)

    if not verify_access_token(rgst_rpc, access_token):
        res_body = {'result': error_code.ERROR_ACCESS_TOKEN_ERROR}
        xmpp_client.send_async(JID(send_jid), xmpp.S_C_RES_BRIDGE_MESSGE, res_body)
        logger.info("RpcHandler::bridge_message ERROR_ACCESS_TOKEN_ERROR, %s" % log_params)
        return True

    from_user_name = JidMgr().gen_user_name(send_jid)
    to_user_name = JidMgr().gen_user_name(to_jid)
    log_params += ",from_user_name:%s, to_user_name:%s" % (from_user_name, to_user_name)

    # check auth
    if not UserService().is_friend(from_user_name, to_user_name):
        res_body = {'result': error_code.ERROR_OPER_NOT_PERMIT}
        xmpp_client.send_async(JID(send_jid), xmpp.S_C_RES_BRIDGE_MESSGE, res_body)
        logger.info("RpcHandler::bridge_message ERROR_OPER_NOT_PERMIT, %s" % log_params)
        return True

    # bridge message
    recv_body = {'from_jid': send_jid, "body": bridge_body}
    xmpp_client.send_async(JID(to_jid), xmpp.S_C_RECV_BRIDGE_MESSGAE, recv_body)

    # response
    res_body = {'result': error_code.ERROR_SUCCESS}
    xmpp_client.send_async(JID(send_jid), xmpp.S_C_RES_BRIDGE_MESSGE, res_body)
    logger.info("RpcHandler::bridge_message SUCCESS!!, %s" % log_params)
Esempio n. 3
0
    def valid_mxid(mxid):
        """
        获取mxid的有效信息
        :param mxid: mxid
        :return: jid, access_token
        """
        jid, access_token = MxidMgr.parser_mxid(mxid)
        if not JidMgr().is_jid(jid):
            logger.info("MxidMgr::check_mxid Failed, not jid, mxid:%s" % mxid)
            return None, access_token

        if not verify_access_token(ServiceMgrCacher().get_connection(US_REGISTER), access_token):
            logger.info("MxidMgr::check_mxid Failed, access_token expire, mxid:%s" % mxid)
            return jid, None

        return jid, access_token
Esempio n. 4
0
def xmpp_login_req(mqttc, userdata, topic, payload):
    """
    xmpp 登陆请求
    :param mqttc:请求客户端
    :param userdata:
    :param topic:主题
    :param payload:内容
    :return:
    """
    jid = topic.split("/")[-1]
    access_token = payload.get('access_token', None)
    jid_pass = payload.get('jid_pass', None)
    log_params = "jid:%s, jid_pass:%s, access_token:%s" % (jid, jid_pass, access_token)

    # check params
    if not access_token\
            or not jid\
            or not JidMgr().is_jid(jid)\
            or not jid_pass:
        s2c_message.xmpp_login_res(jid, error_code.ERROR_PARAMS_ERROR, "")
        logger.info("xmpp_login_req ERROR_WRONG_PARAMS, %s" % log_params)
        return

    if not verify_access_token(ServiceMgrCacher().get_connection(US_REGISTER), access_token):
        s2c_message.xmpp_login_res(jid, error_code.ERROR_ACCESS_TOKEN_ERROR, "")
        logger.info("xmpp_login_req ERROR_ACCESS_TOKEN_ERROR, %s" % log_params)
        return

    # 不管重登与否,access_token都有可能发生变化
    XmppAdapterMgr().add_access_token(jid, access_token)

    xmpp_adapter = XmppAdapterMgr().get_xmpp_adapter(jid)
    if xmpp_adapter:
        xmpp_adapter.on_login(relogin=True)
        return

    XmppAdapterMgr().login(jid, jid_pass)
 def test_tcp_rpc_user_token_check2(self):
     user2_token = LoginGetToken(RgstHttpRpc, UserName2, Password2)
     self.assertTrue(verify_access_token(RgstTcpRpc, user2_token))