我想通过Action Cable使用Doorkeeper授权。可能吗?
我使用before_action doorkeeper_authorize!
的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
答案 0 :(得分:0)
更新的答案
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