Ejemplo n.º 1
0
    def onJoin(self, details):
        self.index += 1

        if details.authmethod == u"anonymous":
            print "Connected anonymously."
            try:
                result = yield self.call(u"rpc.info.get_exchange_info")
                print "Exchange info: %s" % str(result)

                def on_event(event):
                    pass

                result = yield self.subscribe(on_event,
                                              u'feeds.market.ohlcv.nets2014')
                print "Subscribed to a ohlcv feed."
            except Exception as e:
                print e
            returnValue(self.leave())

        if details.authmethod == u"wampcra":
            print "Logged in using WAMP-CRA."
            try:
                self.cookie = (yield self.call(u"rpc.token.get_cookie"))[1]
                print "Got a cookie: %s" % self.cookie

                def on_event(event):
                    pass
                result = yield self.subscribe(on_event,
                        u'feeds.user.orders.%s' % \
                                util.encode_username(u'marketmaker'))
                print "Subscribed to a feed."
            except Exception as e:
                print e
            returnValue(self.leave())

        if details.authmethod == u"cookie":
            print "Logged in using a cookie."
            try:
                result = yield self.call(u"rpc.token.logout")
                print "Logged out."
            except Exception as e:
                print e
            returnValue(self.leave())
Ejemplo n.º 2
0
    def onJoin(self, details):
        self.index += 1

        if details.authmethod == u"anonymous":
            print "Connected anonymously."
            try:
                result = yield self.call(u"rpc.info.get_exchange_info")
                print "Exchange info: %s" % str(result)
                def on_event(event):
                    pass
                result = yield self.subscribe(on_event,
                        u'feeds.market.ohlcv.nets2014')
                print "Subscribed to a ohlcv feed."
            except Exception as e:
                print e
            returnValue(self.leave())

        if details.authmethod == u"wampcra":
            print "Logged in using WAMP-CRA."
            try:
                self.cookie = (yield self.call(u"rpc.token.get_cookie"))[1]
                print "Got a cookie: %s" % self.cookie
                def on_event(event):
                    pass
                result = yield self.subscribe(on_event,
                        u'feeds.user.orders.%s' % \
                                util.encode_username(u'marketmaker'))
                print "Subscribed to a feed."
            except Exception as e:
                print e
            returnValue(self.leave())

        if details.authmethod == u"cookie":
            print "Logged in using a cookie."
            try:
                result = yield self.call(u"rpc.token.logout")
                print "Logged out."
            except Exception as e:
                print e
            returnValue(self.leave())
Ejemplo n.º 3
0
 def on_order(self, username, order):
     username = util.encode_username(username)
     self.publish(u"feeds.user.orders.%s" % username, order)
Ejemplo n.º 4
0
 def on_transaction(self, username, transaction):
     username = util.encode_username(username)
     self.publish(u"feeds.user.transactions.%s" % username, transaction)
Ejemplo n.º 5
0
 def on_fill(self, username, fill):
     username = util.encode_username(username)
     self.publish(u"feeds.user.fills.%s" % username, fill)
Ejemplo n.º 6
0
    def authorize(self, router, session, uri, action):
        debug("Checking permissions for %s(%s) to %s %s" % \
              (session._authid, session._authrole, \
               IRouter.ACTION_TO_STRING[action], uri))

        # allow trusted roles to do everything
        if session._authrole == u"trusted":
            log("Authorizing %s(%s) to %s %s" % \
                (session._authid, session._authrole, \
                 IRouter.ACTION_TO_STRING[action], uri))
            return True

        # allow others to only call and subscribe
        if action not in [IRouter.ACTION_CALL, IRouter.ACTION_SUBSCRIBE]:
            return False

        # TODO: We should use URI Patterns instead.
        # p = Pattern(u"rpc.<service:string>.<method:suffix>",
        #             Pattern.URI_TARGET_ENDPOINT)
        # p.match(uti)
        # Currently there is a bug that prevents this from working. A pull
        #   request with a patch has been sent.

        # Rpc calls
        rpc_match = re.compile("^rpc\.([a-z_.]+)\.")
        match = rpc_match.match(uri)
        if match is not None:
            section = match.groups(1)[0]
            # Some sections give to everyone
            if section in ["info", "market", "registrar"]:
                log("Authorizing %s(%s) to %s %s" % \
                    (session._authid, session._authrole, \
                     IRouter.ACTION_TO_STRING[action], uri))
                return True
            if section in ["trader", "private", "token"
                           ] and session._authrole == u"user":
                log("Authorizing %s(%s) to %s %s" % \
                    (session._authid, session._authrole, \
                     IRouter.ACTION_TO_STRING[action], uri))
                return True
            return False

        feed_match = re.compile("^feeds\.([a-z_]+)\.")
        match = feed_match.match(uri)
        if match is not None:
            section = match.groups(1)[0]
            if section in ["market"]:
                return True
            if section in ["user"] and session._authrole == u"user":
                # Make sure we can read this feed
                # Check for sha256
                user_match = re.compile("^feeds\.%s\.[a-z_]+\.([0-9a-f]+)$" %
                                        section)
                match = user_match.match(uri)
                if match is not None:
                    hash = match.groups(1)[0]
                    hashed_username = util.encode_username(session._authid)
                    if hash == hashed_username:
                        log("Authorizing %s(%s) to %s %s" % \
                            (session._authid, session._authrole, \
                             IRouter.ACTION_TO_STRING[action], uri))
                        return True

            # No joy, no access
            return False
Ejemplo n.º 7
0
 def on_order(self, username, order):
     username = util.encode_username(username)
     self.publish(u"feeds.user.orders.%s" % username, order)
Ejemplo n.º 8
0
 def on_transaction(self, username, transaction):
     username = util.encode_username(username)
     self.publish(u"feeds.user.transactions.%s" % username, transaction)
Ejemplo n.º 9
0
 def on_fill(self, username, fill):
     username = util.encode_username(username)
     self.publish(u"feeds.user.fills.%s" % username, fill)
Ejemplo n.º 10
0
    def authorize(self, router, session, uri, action):
        debug("Checking permissions for %s(%s) to %s %s" % \
              (session._authid, session._authrole, \
               IRouter.ACTION_TO_STRING[action], uri))
        
        # allow trusted roles to do everything
        if session._authrole == u"trusted":
            log("Authorizing %s(%s) to %s %s" % \
                (session._authid, session._authrole, \
                 IRouter.ACTION_TO_STRING[action], uri))
            return True

        # allow others to only call and subscribe
        if action not in [IRouter.ACTION_CALL, IRouter.ACTION_SUBSCRIBE]:
            return False

        # TODO: We should use URI Patterns instead.
        # p = Pattern(u"rpc.<service:string>.<method:suffix>",
        #             Pattern.URI_TARGET_ENDPOINT)
        # p.match(uti) 
        # Currently there is a bug that prevents this from working. A pull
        #   request with a patch has been sent.

        # Rpc calls
        rpc_match = re.compile("^rpc\.([a-z_.]+)\.")
        match = rpc_match.match(uri)
        if match is not None:
            section = match.groups(1)[0]
            # Some sections give to everyone
            if section in ["info", "market", "registrar"]:
                log("Authorizing %s(%s) to %s %s" % \
                    (session._authid, session._authrole, \
                     IRouter.ACTION_TO_STRING[action], uri))
                return True
            if section in ["trader", "private", "token"] and session._authrole == u"user":
                log("Authorizing %s(%s) to %s %s" % \
                    (session._authid, session._authrole, \
                     IRouter.ACTION_TO_STRING[action], uri))
                return True
            return False

        feed_match = re.compile("^feeds\.([a-z_]+)\.")
        match = feed_match.match(uri)
        if match is not None:
            section = match.groups(1)[0]
            if section in ["market"]:
                return True
            if section in ["user"] and session._authrole == u"user":
                # Make sure we can read this feed
                # Check for sha256
                user_match = re.compile("^feeds\.%s\.[a-z_]+\.([0-9a-f]+)$" % section)
                match = user_match.match(uri)
                if match is not None:
                    hash = match.groups(1)[0]
                    hashed_username = util.encode_username(session._authid)
                    if hash == hashed_username:
                        log("Authorizing %s(%s) to %s %s" % \
                            (session._authid, session._authrole, \
                             IRouter.ACTION_TO_STRING[action], uri))
                        return True

            # No joy, no access
            return False