是否可以使用带有行动电缆的门卫?

时间:2018-04-11 15:13:17

标签: ruby-on-rails ruby-on-rails-5 actioncable doorkeeper

我想通过Action Cable使用Doorkeeper授权。可能吗? 我使用before_action doorkeeper_authorize!

的Fot控制器

的routes.rb

use_doorkeeper do
    skip_controllers :authorizations, :applications, :authorized_applications
end

mount ActionCable.server => '/cable'

更新

看起来方法与此处相同: http://www.rubydoc.info/github/rails/actioncable/ActionCable/Connection/Base

How To use devise_token_auth with ActionCable for authenticating user?

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

    def connect
      self.current_user = find_verified_user
    end

    protected

      def find_verified_user 
        #CODE RELATED TO DOORKEEPER ????

        if user && user.valid_token?
          user
        else
          reject_unauthorized_connection
        end
      end
  end
end

1 个答案:

答案 0 :(得分:0)

更新的答案

开发人员added this example

OLD ANSWER

结果类似:

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

    def connect
      self.current_user = find_verified_user
    end

    protected

      def find_verified_user 
        user = User.find_by(id: access_token.resource_owner_id) if access_token 

        if user 
          user
        else
          reject_unauthorized_connection
        end
      end

      def access_token
        params = request.query_parameters()

        @access_token ||= Doorkeeper::AccessToken.by_token(params[:access_token])
      end
  end
end

class CarsLocationsChannel < ApplicationCable::Channel
  def subscribed
     reject unless current_user
     stream_from "cars_locations"
  end
end