【问题标题】:Secure Action cable with Doorkeeper Authorisation带门卫授权的安全动作电缆
【发布时间】:2023-01-12 19:33:13
【问题描述】:

我正在处理 ActionCable 并在我的 Rails 应用程序中实现了 Doorkeeper 授权。

我想用Doorkeeper::AccessTokenActionCable实现我的客户authenticate

这是我现在的身份验证方式:

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user
    identified_by :room_id

    def connect
      self.current_user = find_verified_user
      self.room_id = @user.ac_channel_room
    end

    def disconnect
      # When user will disconnect action cable, this method call will be executed.
    end

    private

    def find_verified_user 
      check_access_token
      @user = User.find_by_id(@resource_owner_id) if @resource_owner_id
      reject_unauthorized_connection unless @user
    end

    def check_access_token
      # Check provided token is valid or not
      params = request.query_parameters
      @access_token ||= Doorkeeper::AccessToken.by_token(params[:access_token])
      @resource_owner_id = @access_token&.resource_owner_id
    end
  end
end

问题是这也允许有经验的访问令牌。

请帮忙!

【问题讨论】:

    标签: ruby-on-rails actioncable doorkeeper


    【解决方案1】:

    您的问题将允许操作电缆与过期的Doorkeeper::AccessToken 对象连接。

    这是解决方案:

    module ApplicationCable
      class Connection < ActionCable::Connection::Base
        identified_by :current_user
    
        def connect
          self.current_user = authenticate!
        end
    
        protected
    
        def authenticate!
          reject_unauthorized_connection unless doorkeeper_token&.acceptable?(@_doorkeeper_scopes)
    
          # this will still allow expired tokens
          # you will need to check if token is valid with something like
          # doorkeeper_token&.acceptable?(@_doorkeeper_scopes)
    
          user = User.find_by(id: doorkeeper_token.try(:resource_owner_id))
    
          user || reject_unauthorized_connection
        end
    
        def doorkeeper_token
          ::Doorkeeper.authenticate(request)
        end
      end
    end
    
    # ...
    
    class SomeChannel < ApplicationCable::Channel
      def subscribed
         reject unless current_user
         stream_from 'some'
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2018-09-21
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      相关资源
      最近更新 更多