簡體   English   中英

如何通過python中的票證對WAMP連接進行身份驗證

[英]How to authenticate a WAMP connection via a ticket in python

我正在嘗試從配置了某些角色的其他應用程序連接到WAMP總線。 這些角色已使用靜態票證進行了身份驗證,因此我認為我需要聲明要連接的角色以及關聯的票證是什么。 我正在用Python編寫此代碼,並且已設置了大多數組件,但是找不到任何有關如何進行這種身份驗證的文檔。

from autobahn.twisted.component import Component, run

COMP = Component(
    realm=u"the-realm-to-connect",
    transports=u"wss://this.is.my.url/topic",
    authentication={
        # This is where I need help
        # u"ticket"?
        # u"authid"?
    }
)

如果沒有身份驗證,當它在計算機上本地運行時,我就可以連接並發布到WAMP總線,但是該WAMP總線已配置為允許匿名用戶發布。 我的生產WAMP總線不允許匿名用戶發布,因此我需要驗證此角色的身份。 Autobahn | Python文檔暗示可以在Python中完成,但是我只能在Crossbar.io文檔中找到如何在JavaScript / JSON中進行操作的示例。

該文檔不是最新的。 對於組件,必須對票證執行以下操作:

from autobahn.twisted.component import Component, run

component = Component(
    realm=u"the-realm-to-connect",
    transports=u"wss://this.is.my.url/topic",
    authentication={
        "ticket": {
            "authid": "username", 
            "ticket": "secrettoken"
        }
    },
)

這是一些對您有幫助的示例:

https://github.com/crossbario/crossbar-examples/tree/master/authentication

我認為您需要使用WAMP-Ticket動態身份驗證方法。

WAMP票證動態身份驗證是一種簡單的明文質詢方案。 客戶端連接到某個authid下的領域,並請求authmethod = ticket。 Crossbar.io將“挑戰”客戶,要求提供票證。 客戶端發送票證,然后Crossbar.io將調用用戶實現的WAMP程序對票證進行實際驗證。

因此,您需要創建一個附加組件來驗證用戶身份:

from autobahn.twisted.wamp import ApplicationSession
from autobahn.wamp.exception import ApplicationError

class AuthenticatorSession(ApplicationSession):

   @inlineCallbacks
   def onJoin(self, details):

      def authenticate(realm, authid, details):
         ticket = details['ticket']
         print("WAMP-Ticket dynamic authenticator invoked: realm='{}', authid='{}', ticket='{}'".format(realm, authid, ticket))
         pprint(details)

         if authid in PRINCIPALS_DB:
            if ticket == PRINCIPALS_DB[authid]['ticket']:
               return PRINCIPALS_DB[authid]['role']
            else:
               raise ApplicationError("com.example.invalid_ticket", "could not authenticate session - invalid ticket '{}' for principal {}".format(ticket, authid))
         else:
            raise ApplicationError("com.example.no_such_user", "could not authenticate session - no such principal {}".format(authid))

      try:
         yield self.register(authenticate, 'com.example.authenticate')
         print("WAMP-Ticket dynamic authenticator registered!")
      except Exception as e:
         print("Failed to register dynamic authenticator: {0}".format(e))

並在配置中添加身份驗證方法:

"transports": [
                {
                    "type": "web",
                    "endpoint": {
                        "type": "tcp",
                        "port": 8080
                    },
                    "paths": {
                        "ws": {
                            "type": "websocket",
                            "serializers": [
                                "json"
                            ],
                            "auth": {
                                "ticket": {
                                    "type": "dynamic",
                                    "authenticator": "com.example.authenticate"
                                }
                            }
                        }
                    }
                }
            ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM