francesco.pessina

francesco.pessina

Hello everyone :slight_smile:
We are experiencing a weird problem with Phoenix Websockets.

We are exposing an authenticated channel. The authentication is made getting the token from the token parameter given during the connection, and then if the token is valid the connection is established.

The problem is that the token is passed as query param in the url of the connection (for example …/socket/websocket?token=…).
For us this is a problem because in some cases we have very long token (2500 chars) which generates a url which have a length greater than the one accepted by our load balancer.

So my question is: is there an alternative way to pass a token along a websocket connection (maybe through a header on into the request body)?

Showing Posts 1 to 6

bartblast

bartblast

Creator of Hologram

You don’t control how the token is generated?

francesco.pessina

francesco.pessina OP

Not really. The token is generated by Keycloak and the data contained in it is used by also other applications against the user authenticates with the token.

dodo

dodo

You cannot set additional headers unfortunately.

You could allow anyone to connect, but then require the first message to be the auth token, and deny every other message if the user is not authenticated.

the-mikedavis

the-mikedavis

So you can pass the token as a header by configuring the websocket like so

# lib/my_app_web/endpoint.ex

socket("/socket", MyAppWeb.UserSocket,
  websocket: [connect_info: [:x_headers]],
  longpoll: false
)

And then passing some {"x-auth-token", auth_token} in the client and handling the token in the connect/3 callback

# lib/my_app_web/channels/user_socket.ex

def connect(_params, socket, %{x_headers: x_headers}) do
  with {:ok, proposed_token} <-
         Enum.find_value(x_headers, :error, fn {k, v} -> k == "x-auth-token" && {:ok, v} end),
       :ok <- MyTokenAuthenticator.authenticate_token(proposed_token) do
    {:ok, socket}
  else
    _ ->
      :error
  end
end

But this won’t work if you’re connecting to the socket in a browser because the WebSocket API in browsers does not allow setting custom headers. If you’re connecting from browsers, I think the only option is to allow all connections:

# lib/my_app_web/channels/user_socket.ex

def connect(_params, socket, _connect_info) do
  {:ok, socket}
end

And instead check the token in the c:Phoenix.Channel.join/3 callback. In fact that documentation has an example of this:

def join("room:lobby", payload, socket) do
  if authorized?(payload) do
    {:ok, socket}
  else
    {:error, %{reason: "unauthorized"}}
  end
end

And that payload in join/3 is encoded in a websocket frame, so it shouldn’t trip up your load balancer.

rogerweb

rogerweb

If you allow anyone to connect, you probably want to disconnect non-authenticated users after a certain timeout, otherwise your server will be vulnerable from a security perspective.

It looks Phoenix.Socket is not a long-running process, so sending a message to it using
Process.send_after/3 is not an option.

Any ideas on how to implement it?

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews