riverrun / phauxth

Not actively maintained - Authentication library for Phoenix, and other Plug-based, web applications
409 stars 20 forks source link

How to use Phauxth.Token.verify inside connect function of UserSocket #56

Closed acrolink closed 6 years ago

acrolink commented 6 years ago

I am trying to verify the token inside:

  def connect(%{"token" => token}, socket) do
    IO.puts token
    case Phoenix.Token.verify(socket, "user salt", token, max_age: 86400) do
      {:ok, user_id} ->
        IO.puts user_id
        socket = assign(socket, :user, Repo.get!(User, user_id))
        {:ok, socket}
      {:error, _} ->
        IO.puts "error"
        :error
    end
  end

But getting errors. How to use Phauxth.Token.verify instead of Phoenix.Token.verify to verify the Phauxthtoken in this context (channels and sockets)?

riverrun commented 6 years ago

The arguments to verify are key_source, token, max_age, opts, so in your example, you would write Phauxth.Token.verify(socket, token, 86400) (the opts are optional).

acrolink commented 6 years ago

@riverrun I have tried that, but I am getting this error:

Request: GET /socket/websocket?token=SFMyNTY.eyJzaWduZWQiOjE1MjUzNzk5MjUsImRhdGEiOjF9.JyIVTHaEozGkclPEh1QpiuTHjOCrXJV1feeZUvOZSE8&vsn=2.0.0
** (exit) an exception was raised:
    ** (ArithmeticError) bad argument in arithmetic expression
        (phauxth) lib/phauxth/token.ex:126: Phauxth.Token.handle_verify/2
        (onlist) lib/onlist_web/channels/user_socket.ex:24: OnlistWeb.UserSocket.connect/2
        (phoenix) lib/phoenix/socket/transport.ex:191: Phoenix.Socket.Transport.do_connect/7
        (phoenix) lib/phoenix/transports/websocket.ex:89: Phoenix.Transports.WebSocket.init/2
        (phoenix) lib/phoenix/endpoint/cowboy_websocket.ex:12: Phoenix.Endpoint.CowboyWebSocket.init/3
        (cowboy) /srv/phx/onlist/deps/cowboy/src/cowboy_handler.erl:64: :cowboy_handler.handler_init/4
        (cowboy) /srv/phx/onlist/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
acrolink commented 6 years ago

Works as designed, I was passing the max_agewrongly to the function.