bobbypriambodo

bobbypriambodo

How is Phoenix.Token different from JWT?

I notice that there has been a thread with the similar title here: Guardian/JWT vs Phoenix.Token?, but it turned into exchanges about why and when (or should you) use JWT and JWT vs. sessions, with the sole mention of Phoenix.Token only on the first post.

I’m starting a new thread to ask about what the title says: how is JWT and Phoenix.Token different? The docs doesn’t seem to say anything about that. I mean, it’s great that I don’t have to add a dependency for generating API tokens if I use Phoenix.Token, but as they say you shouldn’t roll out your own security mechanisms if you can help it. I suppose JWT is well-tested; how about Phoenix.Token?

I hope someone can shed a light into this :slight_smile:

Most Liked

bobbypriambodo

bobbypriambodo

After some digging, I’ve found the following sources of information: the issue (#699) about token auth, the corresponding PR (#820), and the Phoenix.Token documentation itself. I’m writing this mainly as a note for myself, hoping for some clarification, and I figured it might help someone along the way.

Based on the issue, the motivation of the Phoenix.Token module itself is the need to have a general API for generating tokens to be used for channel and API authentication. It doesn’t actually dictate how the implementation itself works. The issue specifically discussed about how the API would look like and, consequently, how it would be implemented.

There were two options for the implementation, one using Plug Tokens (through Plug.Crypto) and the other JWT. The discussion kind of ended abruptly (possibly because a more thorough brainstorming was conducted somewhere else) stating that the chosen approach is the Plug one. I inferred that one of the reasons might be that by using Plug Tokens there’s no need to add another dependency and it works for most cases.

So, to answer the question: Phoenix.Token (with Plug Tokens) vs. JWT, several points I noticed:

  1. Phoenix.Token doesn’t let you specify signing algorithms (defaulting to sha256 according to this line on MessageVerifier). JWT (for example using Joken) does.

  2. Both encodes some form of payload into Base64, and the encoding can be read by clients (so you shouldn’t include sensitive information).

  3. JWT allows you to encode arbitrary JSON objects as payload, Phoenix.Token uses Erlang terms serialization so you can’t really do that (I’m not sure I fully understand this one, see this rejected PR for details). That said, the docs doesn’t really tell you what kind of data you can use as payload; I’m not sure everyone is familiar what Erlang terms are.

Other than that, I think the token generated using Phoenix.Token and JWT behaves the same.

A question that still bugs my mind is about the salt parameter. The docs doesn’t really specify whether you should use hardcoded strings like "user" (and commit it to version control) or should we use randomized secrets strings for that, and how it will affect the tokens. I’d be happy to put up some PRs for the docs but as of now I don’t think I have the sufficient knowledge for that.

17
Post #2
josevalim

josevalim

Creator of Elixir

This means what you encode with JWT is only want can be encoded to JSON while Phoenix.Token is able to encode all terms. On the other side, JWT is interoperable (JSON is widespread) while Phoenix.Token not quite.

The salt can be hard coded. PRs to make the docs clearer are always welcome. :slight_smile:

josevalim

josevalim

Creator of Elixir

Yes, we use the salt for key derivation and namespacing tokens. Calling it salt, although technically correct, is likely confusing. namespace could be a better name.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127536 1222
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

We're in Beta

About us Mission Statement