如何在每个会话的基础上限制Autobahn python订阅

时间:2014-11-04 17:10:29

标签: python websocket twisted autobahn

我在服务器端使用带有扭曲(wamp)的autobahnpython,在浏览器中使用autobahnjs。是否有直接的方式允许/限制每个会话的订阅?例如,客户端不应该订阅与其他用户相关的主题。

虽然我没有使用crossbar.io,但我尝试使用'示例'中显示的Python代码。本页末尾的部分http://crossbar.io/docs/Authorization/,其中首先使用RPC调用来授予客户端权限。当然,我正在使用自己的授权逻辑。一旦此授权成功,我就希望授予客户权限以订阅仅与此客户相关的主题,例如' com.example.user_id'。我的问题是,即使auth通过,我还没有找到一种方法来限制授权发生的ApplicationSession类中的订阅请求。如何阻止使用user_id = user_a授权的客户订阅' com.example.user_b'?

2 个答案:

答案 0 :(得分:0)

您可以通过创建自己的路由器进行授权。为此,子类化Router()并覆盖(在最小值)authorize()方法:

def authorize(self, session, uri, action):
    return True

此方法非常简单,如果返回True,则会话被授权执行任何尝试。您可以制定一个规则,所有订阅必须以' com.example.USER_ID'开头,因此,您的python代码将拆分uri,取第三个字段,并将其与当前会话ID进行比较,返回True如果匹配,否则为假。这是事情变得有点奇怪的地方。我有类似的代码,这是我的authorize()方法:

@inlineCallbacks
def authorize(self, session, uri, action):
    authid = session._authid
    if authid is None:
        authid = 1
    log.msg("AuthorizeRouter.authorize: {} {} {} {} {}".format(authid,
        session._session_id, uri, IRouter.ACTION_TO_STRING[action], action))
    if authid != 1:
        rv = yield self.check_permission(authid, uri, IRouter.ACTION_TO_STRING[action])
    else:
        rv = yield True

    log.msg("AuthorizeRouter.authorize: rv is {}".format(rv))

    if not uri.startswith(self.svar['topic_base']):
        self.sessiondb.activity(session._session_id, uri, IRouter.ACTION_TO_STRING[action], rv)

    returnValue(rv)

    return

请注意,我深入会话以获取_authid,这是一个糟糕的业力(我认为),因为我不应该看这些私有变量。但是,我不知道还能在哪里获得它。

另外值得注意的是,这与身份验证密切相关。在我的实现中,_authid是经过身份验证的用户ID,类似于unix用户ID(正唯一整数)。我很确定这可以是任何东西,比如字符串,所以你应该对你的' user_b'如果你愿意,可以作为_auth_id。

-g

答案 1 :(得分:0)

我找到了一个使用Node guest的相对简单的解决方案。这是代码:

    // crossbar setup
var autobahn = require('autobahn');

var connection = new autobahn.Connection({
        url: 'ws://127.0.0.1:8080/ws',
        realm: 'realm1'
    }
);

// Websocket to Scratch setup
// pull in the required node packages and assign variables for the entities
var WebSocketServer = require('websocket').server;
var http = require('http');

var ipPort = 1234; // ip port number for Scratch to use

// this connection is a crossbar connection
connection.onopen = function (session) {

    // create an http server that will be used to contain a WebSocket server
    var server = http.createServer(function (request, response) {
        // We are not processing any HTTP, so this is an empty function. 'server' is a wrapper for the
        // WebSocketServer we are going to create below.
    });

    // Create an IP listener using the http server
    server.listen(ipPort, function () {
        console.log('Webserver created and listening on port ' + ipPort);
    });

    // create the WebSocket Server and associate it with the httpServer
    var wsServer = new WebSocketServer({
        httpServer: server
    });

    // WebSocket server has been activated and a 'request' message has been received from client websocket
    wsServer.on('request', function (request) {
        // accept a connection request from Xi4S
        //myconnection is the WS connection to Scratch
        myconnection = request.accept(null, request.origin); // The server is now 'online'

        // Process Xi4S messages
        myconnection.on('message', function (message) {

            console.log('message received: ' + message.utf8Data);
            session.publish('com.serial.data', [message.utf8Data]);

            // Process each message type received
            myconnection.on('close', function (myconnection) {
                console.log('Client closed connection');
                boardReset();
            });
        });
    });
};

connection.open();