2016-02-25 40 views
3

我目前正試圖通過Crossbar/Autobahn使用Websockets實現用戶通知系統。我已經做了多次試驗,並經歷的文檔,但是,我不知道是否有具有以下工作流程的解決方案:在Web應用程序使用Crossbar/Autobahn進行用戶通知的身份驗證?

  1. 用戶的跡象 - 這是通過JWT
  2. 完成
  3. Frontend建立與正在運行的crossbar實例的websocket連接。
  4. 前端嘗試訂閱特定於用戶通知的URI:即com.example.notifications.user.23com.example.user.23.notifications'. Where 23`是用戶標識。
  5. 檢查用戶的智威湯遜是否允許用戶訪問訂閱。
  6. 當活動生成並導致通知時,後端發佈用戶特定的URI。

對於第3步,我不知道當前的支持auth方法是否有我需要的。理想情況下,我想要一個可以自定義的auth方法(爲了在Crossbar中實現JWT身份驗證器),我可以將其應用於URI模式,但不能將整個模式授予訂閱用戶。這是部分地由動態AUTH方法解決,但缺少後半部分:

例如(我的理想工作流程):

  1. 用戶嘗試訂閱URI com.example.user.23.notifications
  2. URI com.example.user..notifications(在http://crossbar.io/docs/Pattern-Based-Subscriptions/通配符模式)
  3. 驗證令牌驗證,並且用戶被給予com.example.user.23.notifications訪問相匹配。

以上是否可以通過簡單的方式實現?從我所知道的情況來看,只有當我以某種方式生成一個包含所有用戶ID的URI排列的.crossbar/config.json ...併爲每個新用戶自動生成一個新配置時纔可能 - 這完全不是一個合理的解決方案。

任何幫助表示讚賞!

回答

3

使用授權人。

http://crossbar.io/docs/Authorization/#dynamic-authorization

註冊一個動態的授權用戶角色會被分配在接合時/認證:

  { 
       "name": "authorizer", 
       "permissions": [ 
       { 
        "uri": "com.example.authorize", 
        "register": true 
       } 
       ] 
      }, 
      { 
       "name": "authenticator", 
       "permissions": [ 
       { 
        "uri": "com.example.authenticate", 
        "register": true 
       } 
       ] 
      }, 
      { 
       "name": "user", 
       "authorizer": "com.example.authorize" 
      }, 
... 
"components": [ 
    { 
     "type": "class", 
     "classname": "example.AuthenticatorSession", 
     "realm": "realm1", 
     "role": "authenticator", 
     "extra": { 
     "backend_base_url": "http://localhost:8080/ws" 
     } 
    }, 
    { 
     "type": "class", 
     "classname": "example.AuthorizerSession", 
     "realm": "realm1", 
     "role": "authorizer" 
    } 
    ] 

編寫類

class AuthorizerSession(ApplicationSession): 
    @inlineCallbacks 
    def onJoin(self, details): 
     print("In AuthorizerSession.onJoin({})".format(details)) 
     try: 
      yield self.register(self.authorize, 'com.example.authorize') 
      print("AuthorizerSession: authorizer registered") 
     except Exception as e: 
      print("AuthorizerSession: failed to register authorizer procedure ({})".format(e)) 

    def authorize(self, session, uri, action): 
     print("AuthorizerSession.authorize({}, {}, {})".format(session, uri, action)) 
     if session['authrole'] == u'backend': # backnend can do whatever 
      return True 
     [Authorization logic here] 
     return authorized 
+1

的確就是這樣。我其實曾嘗試過,但我沒有意識到這是多個事件被調用。在'session','uri'和'action'中提供的數據對於我的目的來說也是不夠的,所以我已經將URI數據傳入'authorize'。 – ineedtosleep