-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Support transport agnostic token passing in channels #6086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ defmodule Phoenix.Transports.WebSocket do | |
|
|
||
| @connect_info_opts [:check_csrf] | ||
|
|
||
| @auth_token_prefix "base64url.bearer.authorization.phx." | ||
|
|
||
| import Plug.Conn | ||
|
|
||
| alias Phoenix.Socket.{V1, V2, Transport} | ||
|
|
@@ -35,12 +37,23 @@ defmodule Phoenix.Transports.WebSocket do | |
| def init(opts), do: opts | ||
|
|
||
| def call(%{method: "GET"} = conn, {endpoint, handler, opts}) do | ||
| subprotocols = | ||
| if opts[:auth_token] do | ||
| # when using Sec-WebSocket-Protocol for passing an auth token | ||
| # the server must reply with one of the subprotocols in the request; | ||
| # therefore we include "phoenix" as allowed subprotocol and include it on the client | ||
| ["phoenix" | Keyword.get(opts, :subprotocols, [])] | ||
| else | ||
| opts[:subprotocols] | ||
| end | ||
|
|
||
| conn | ||
| |> fetch_query_params() | ||
| |> Transport.code_reload(endpoint, opts) | ||
| |> Transport.transport_log(opts[:transport_log]) | ||
| |> Transport.check_origin(handler, endpoint, opts) | ||
| |> Transport.check_subprotocols(opts[:subprotocols]) | ||
| |> maybe_auth_token_from_header(opts[:auth_token]) | ||
| |> Transport.check_subprotocols(subprotocols) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly, it feels we should just bring this function to this module and have it deal with both auth_token and subprotocols at once, or move auth_token to check subprotocols.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is part of the public API, so we can't really change it, can we?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we cannot, but maybe we can move the auth token handling there anyway? |
||
| |> case do | ||
| %{halted: true} = conn -> | ||
| conn | ||
|
|
@@ -82,4 +95,36 @@ defmodule Phoenix.Transports.WebSocket do | |
| def call(conn, _), do: send_resp(conn, 400, "") | ||
|
|
||
| def handle_error(conn, _reason), do: send_resp(conn, 403, "") | ||
|
|
||
| defp maybe_auth_token_from_header(conn, true) do | ||
| case get_req_header(conn, "sec-websocket-protocol") do | ||
| [] -> | ||
| conn | ||
|
|
||
| [subprotocols_header | _] -> | ||
| request_subprotocols = | ||
| subprotocols_header | ||
| |> Plug.Conn.Utils.list() | ||
| |> Enum.split_with(&String.starts_with?(&1, @auth_token_prefix)) | ||
|
|
||
| case request_subprotocols do | ||
| {[@auth_token_prefix <> encoded_token], actual_subprotocols} -> | ||
| token = Base.decode64!(encoded_token, padding: false) | ||
|
|
||
| conn | ||
| |> put_private(:__phoenix_transport_auth_token, token) | ||
SteffenDE marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| |> set_actual_subprotocols(actual_subprotocols) | ||
|
|
||
| _ -> | ||
| conn | ||
| end | ||
| end | ||
| end | ||
|
|
||
| defp maybe_auth_token_from_header(conn, _), do: conn | ||
|
|
||
| defp set_actual_subprotocols(conn, []), do: delete_req_header(conn, "sec-websocket-protocol") | ||
|
|
||
| defp set_actual_subprotocols(conn, subprotocols), | ||
| do: put_req_header(conn, "sec-websocket-protocol", Enum.join(subprotocols, ", ")) | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.