-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ease authorization with configurable Turbo::StreamChannel superclass
`ApplicationCable::Connection` provides a simple and intuitive way to authenticate both custom ActionCable Channels and the `Turbo::Broadcastable` broadcasts made on `Turbo::StreamsChannel`. In multi-tenancy applications, authenticating the user is often not enough as an evicted user could subscribe while being authenticated via another tenant, given that they kept note of a signed stream name. We'll allow applications to configure `Turbo::StreamsChannel`'s superclass with the intention of implementing application-specific authorization logic, e.g in `ApplicationCable::Channel`. This API is symmetrical with how authentication can be implemented in `ApplicationCable::Connection`. Before this, `Turbo::StreamsChannel` needs to be monkey patched to implement authorization. With this change, applications can opt-in to an application-owned `Turbo::StreamsChannel` superclass with: ```rb config.turbo.base_stream_channel_class = "ApplicationCable::Channel" ``` …and implement authorization with `current_user` from `ApplicationCable::Connection` and `locate_streamable(s)` from a new `Turbo::Streams::LocatableName` convenience concern: ```rb module ApplicationCable class Channel < ActionCable::Channel::Base def authorized? current_user.can_access? locate_streamable end end end ``` By default, the superclass is unchanged and `authorized?` returns `true`, thus causing no compatibility issues when upgrading `turbo-rails` in existing applications.
- Loading branch information
Showing
7 changed files
with
188 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# When streaming from a model instance using <tt>turbo_stream_from @post</tt>, it can be useful to locate the instance | ||
# in <tt>config.turbo.base_stream_channel_class</tt>. These helper methods are available as a convenience for applications | ||
# to implement custom logic such as authorization. | ||
module Turbo::Streams::LocatableName | ||
# Locate a single streamable. Useful when subscribing with <tt>turbo_stream_from @post</tt>. It can be used e.g to | ||
# implement application-specific authorization, ex: <tt>current_user.can_access? locate_streamable</tt> | ||
def locate_streamable | ||
@locate_streamable ||= GlobalID::Locator.locate(verified_stream_name_from_params) | ||
end | ||
|
||
# Locate multiple streamables. Useful when subscribing with <tt>turbo_stream_from @post1, @post2</tt>. It can be | ||
# used e.g to implement application-specific authorization, ex: | ||
# <tt>locate_streamables.present? && locate_streamables.all? { |streamable| current_user.can_access?(streamable) }</tt> | ||
def locate_streamables | ||
@locate_streamables ||= GlobalID::Locator.locate_many(verified_stream_name_parts_from_params) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters