Skip to content

Commit 5945389

Browse files
authored
fix: do not pass "jwt" to Authorization calls (#1499)
It's a minor thing but the JWT was being sent as part of the auth params which is called on an RPC call increasing the payload
1 parent a11fc67 commit 5945389

File tree

5 files changed

+13
-22
lines changed

5 files changed

+13
-22
lines changed

lib/realtime/tenants/authorization.ex

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ defmodule Realtime.Tenants.Authorization do
2121
defstruct [:tenant_id, :topic, :headers, :jwt, :claims, :role]
2222

2323
@type t :: %__MODULE__{
24-
:tenant_id => binary() | nil,
25-
:topic => binary() | nil,
26-
:claims => map(),
27-
:headers => keyword({binary(), binary()}),
28-
:jwt => map(),
29-
:role => binary()
24+
:tenant_id => binary | nil,
25+
:topic => binary | nil,
26+
:claims => map,
27+
:headers => list({binary, binary}),
28+
:role => binary
3029
}
3130

3231
@doc """
@@ -35,7 +34,6 @@ defmodule Realtime.Tenants.Authorization do
3534
Requires a map with the following keys:
3635
* topic: The name of the channel being accessed taken from the request
3736
* headers: Request headers when the connection was made or WS was updated
38-
* jwt: JWT String
3937
* claims: JWT claims
4038
* role: JWT role
4139
"""
@@ -45,7 +43,6 @@ defmodule Realtime.Tenants.Authorization do
4543
tenant_id: Map.get(map, :tenant_id),
4644
topic: Map.get(map, :topic),
4745
headers: Map.get(map, :headers),
48-
jwt: Map.get(map, :jwt),
4946
claims: Map.get(map, :claims),
5047
role: Map.get(map, :role)
5148
}

lib/realtime/tenants/batch_broadcast.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ defmodule Realtime.Tenants.BatchBroadcast do
3838
auth_params = %{
3939
tenant_id: tenant.external_id,
4040
headers: conn.req_headers,
41-
jwt: conn.assigns.jwt,
4241
claims: conn.assigns.claims,
4342
role: conn.assigns.role
4443
}

lib/realtime_web/channels/realtime_channel.ex

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ defmodule RealtimeWeb.RealtimeChannel do
6060
:ok <- limit_joins(socket),
6161
:ok <- limit_channels(socket),
6262
:ok <- limit_max_users(socket),
63-
{:ok, claims, confirm_token_ref, access_token, _} <- confirm_token(socket),
64-
socket = assign_authorization_context(socket, sub_topic, access_token, claims),
63+
{:ok, claims, confirm_token_ref} <- confirm_token(socket),
64+
socket = assign_authorization_context(socket, sub_topic, claims),
6565
{:ok, db_conn} <- Connect.lookup_or_start_connection(tenant_id),
6666
{:ok, socket} <- maybe_assign_policies(sub_topic, db_conn, socket) do
6767
tenant_topic = Tenants.tenant_topic(tenant_id, sub_topic, !socket.assigns.private?)
@@ -308,7 +308,7 @@ defmodule RealtimeWeb.RealtimeChannel do
308308

309309
def handle_info(:confirm_token, %{assigns: %{pg_change_params: pg_change_params}} = socket) do
310310
case confirm_token(socket) do
311-
{:ok, claims, confirm_token_ref, _, _} ->
311+
{:ok, claims, confirm_token_ref} ->
312312
pg_change_params = Enum.map(pg_change_params, &Map.put(&1, :claims, claims))
313313
{:noreply, assign(socket, %{confirm_token_ref: confirm_token_ref, pg_change_params: pg_change_params})}
314314

@@ -393,8 +393,8 @@ defmodule RealtimeWeb.RealtimeChannel do
393393
# Update token and reset policies
394394
socket = assign(socket, %{access_token: refresh_token, policies: nil})
395395

396-
with {:ok, claims, confirm_token_ref, _, socket} <- confirm_token(socket),
397-
socket = assign_authorization_context(socket, channel_name, refresh_token, claims),
396+
with {:ok, claims, confirm_token_ref} <- confirm_token(socket),
397+
socket = assign_authorization_context(socket, channel_name, claims),
398398
{:ok, db_conn} <- Connect.lookup_or_start_connection(tenant_id),
399399
{:ok, socket} <- maybe_assign_policies(channel_name, db_conn, socket) do
400400
Helpers.cancel_timer(pg_sub_ref)
@@ -567,7 +567,7 @@ defmodule RealtimeWeb.RealtimeChannel do
567567
interval = min(@confirm_token_ms_interval, exp_diff * 1000)
568568
ref = Process.send_after(self(), :confirm_token, interval)
569569

570-
{:ok, claims, ref, access_token, socket}
570+
{:ok, claims, ref}
571571
else
572572
{:error, :token_malformed} ->
573573
{:error, :token_malformed, "The token provided is not a valid JWT"}
@@ -696,13 +696,12 @@ defmodule RealtimeWeb.RealtimeChannel do
696696
end)
697697
end
698698

699-
defp assign_authorization_context(socket, topic, access_token, claims) do
699+
defp assign_authorization_context(socket, topic, claims) do
700700
authorization_context =
701701
Authorization.build_authorization_params(%{
702702
tenant_id: socket.assigns.tenant,
703703
topic: topic,
704704
headers: Map.get(socket.assigns, :headers, []),
705-
jwt: access_token,
706705
claims: claims,
707706
role: claims["role"]
708707
})

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Realtime.MixProject do
44
def project do
55
[
66
app: :realtime,
7-
version: "2.42.4",
7+
version: "2.42.5",
88
elixir: "~> 1.17.3",
99
elixirc_paths: elixirc_paths(Mix.env()),
1010
start_permanent: Mix.env() == :prod,

test/realtime/tenants/authorization_test.exs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,11 @@ defmodule Realtime.Tenants.AuthorizationTest do
237237
create_rls_policies(db_conn, context.policies, %{topic: topic})
238238

239239
claims = %{sub: random_string(), role: context.role, exp: Joken.current_time() + 1_000}
240-
signer = Joken.Signer.create("HS256", "secret")
241-
242-
jwt = Joken.generate_and_sign!(%{}, claims, signer)
243240

244241
authorization_context =
245242
Authorization.build_authorization_params(%{
246243
tenant_id: tenant.external_id,
247244
topic: topic,
248-
jwt: jwt,
249245
claims: claims,
250246
headers: [{"header-1", "value-1"}],
251247
role: claims.role

0 commit comments

Comments
 (0)