Skip to content
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

fix: handle null access token #638

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions lib/realtime_web/channels/realtime_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ defmodule RealtimeWeb.RealtimeChannel do
other
end

Logger.debug("Postgres change params: " <> inspect(pg_change_params, pretty: true))
Logger.debug("Postgres change params: " <> inspect(pg_change_params))

if !Enum.empty?(pg_change_params) do
send(self(), :postgres_subscribe)
end

Logger.debug("Start channel: " <> inspect(pg_change_params, pretty: true))
Logger.debug("Start channel: " <> inspect(pg_change_params))

presence_key = presence_key(params)

Expand All @@ -190,28 +190,28 @@ defmodule RealtimeWeb.RealtimeChannel do
})}
else
{:error, :too_many_channels} = error ->
error_msg = inspect(error, pretty: true)
Logger.warn("Start channel error: #{error_msg}")
error_msg = inspect(error)
Logger.warn("Start channel error: " <> error_msg)
{:error, %{reason: error_msg}}

{:error, :too_many_connections} = error ->
error_msg = inspect(error, pretty: true)
Logger.warn("Start channel error: #{error_msg}")
error_msg = inspect(error)
Logger.warn("Start channel error: " <> error_msg)
{:error, %{reason: error_msg}}

{:error, :too_many_joins} = error ->
error_msg = inspect(error, pretty: true)
Logger.warn("Start channel error: #{error_msg}")
error_msg = inspect(error)
Logger.warn("Start channel error: " <> error_msg)
{:error, %{reason: error_msg}}

{:error, [message: "Invalid token", claim: _claim, claim_val: _value]} = error ->
error_msg = inspect(error, pretty: true)
Logger.warn("Start channel error: #{error_msg}")
error_msg = inspect(error)
Logger.warn("Start channel error: " <> error_msg)
{:error, %{reason: error_msg}}

error ->
error_msg = inspect(error, pretty: true)
Logger.error("Start channel error: #{error_msg}")
error_msg = inspect(error)
Logger.error("Start channel error: " <> error_msg)
{:error, %{reason: error_msg}}
end
end
Expand Down Expand Up @@ -286,7 +286,7 @@ defmodule RealtimeWeb.RealtimeChannel do
{:noreply, assign(socket, :pg_sub_ref, nil)}

error ->
message = "Subscribing to PostgreSQL failed: #{inspect(error)}"
message = "Subscribing to PostgreSQL failed: " <> inspect(error)

push_system_message("postgres_changes", socket, "error", message, channel_name)

Expand All @@ -296,7 +296,7 @@ defmodule RealtimeWeb.RealtimeChannel do
end

nil ->
Logger.warning("Re-subscribed to PostgreSQL with params: #{inspect(pg_change_params)}")
Logger.warning("Re-subscribed to PostgreSQL with params: " <> inspect(pg_change_params))
{:noreply, assign(socket, :pg_sub_ref, postgres_subscribe())}
end
end
Expand All @@ -314,14 +314,14 @@ defmodule RealtimeWeb.RealtimeChannel do
})}

{:error, error} ->
message = "access token has expired: " <> inspect(error, pretty: true)
message = "access token has expired: " <> inspect(error)

shutdown_response(socket, message)
end
end

def handle_info(msg, socket) do
Logger.error("HANDLE_INFO message not handled: #{inspect(msg, pretty: true)}")
Logger.error("HANDLE_INFO message not handled: " <> inspect(msg))
{:noreply, socket}
end

Expand Down Expand Up @@ -351,6 +351,15 @@ defmodule RealtimeWeb.RealtimeChannel do
{:noreply, socket}
end

def handle_in(
"access_token",
%{"access_token" => refresh_token},
%{assigns: %{access_token: _access_token}} = socket
)
when is_nil(refresh_token) do
{:noreply, socket}
chasers marked this conversation as resolved.
Show resolved Hide resolved
end

def handle_in(
"access_token",
%{"access_token" => refresh_token},
Expand Down Expand Up @@ -452,14 +461,21 @@ defmodule RealtimeWeb.RealtimeChannel do
{:reply, result, socket}
end

def handle_in(_, _, socket) do
def handle_in(type, payload, socket) do
socket = count(socket)
chasers marked this conversation as resolved.
Show resolved Hide resolved

# Log info here so that bad messages from clients won't flood Logflare
# Can subscribe to a Channel with `log_level` `info` to see these messages
Logger.info(
"Unexpected message from client of type `#{type}` with payload: " <> inspect(payload)
)

{:noreply, socket}
end

@impl true
def terminate(reason, _state) do
Logger.debug(%{terminate: reason})
Logger.debug("Channel terminated with reason: " <> inspect(reason))
:telemetry.execute([:prom_ex, :plugin, :realtime, :disconnected], %{})
:ok
end
Expand Down Expand Up @@ -502,7 +518,7 @@ defmodule RealtimeWeb.RealtimeChannel do
end

other ->
Logger.error("Unexpected error for #{tenant} #{inspect(other)}")
Logger.error("Unexpected error for #{tenant}: " <> inspect(other))
{:error, other}
end
end
Expand Down Expand Up @@ -636,7 +652,7 @@ defmodule RealtimeWeb.RealtimeChannel do
when is_binary(message) do
push_system_message("system", socket, "error", message, channel_name)

Logger.error(message)
Logger.error("Channel shutting down with message: " <> message)

{:stop, :shutdown, socket}
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Realtime.MixProject do
def project do
[
app: :realtime,
version: "2.20.1",
version: "2.20.2",
elixir: "~> 1.14.0",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down
Loading