apoeco

apoeco

Hello everyone, this is my first post here!

I am a newbie in Elixir and Phoenix (ex Ruby/Rails :wink: ) and I am having some hard time
trying to figure out how to properly set authentication in my project (Phoenix/React).

I was looking at Phoenix.Token today (as I will probably try to roll out my own authentication library/plug)
and noticed that it is using Plug.Crypto.MessageVerifier.

As MessageVerifier docs are mentioning

MessageVerifier makes it easy to generate and verify messages
which are signed to prevent tampering.

On the other hand I took a look at Plug.Crypto.MessageEncryptor .

As MessageEncryptor docs are mentioning

MessageEncryptor is a simple way to encrypt values which get stored
somewhere you don’t trust.
The encrypted key, initialization vector, cipher text, and cipher tag
are base64url encoded and returned to you.
This can be used in situations similar to the MessageVerifier, but where
you don’t want users to be able to determine the value of the payload.

So I need to ask this!

Given the fact that our js/socket clients are not trusted, why does Phoenix.Token use MessageVerifier over MessageEncryptor?
I must also note that there is a get_secret private function defined and used inside of Phoenix.Token that I really didn’t check out :blush: …

Showing Posts 1 to 4

bobbypriambodo

bobbypriambodo

Hello and welcome!

Because most of the time, signed token (as opposed to encrypted token) is enough. You usually only store safe-to-know values as the token payload, such as the user’s ID. This is also the case with JWT, the messages are signed to prevent tampering, but they’re not encrypted, so you can inspect the payload with JWT decoders available online.

Encrypted messages make sense if the payload of the message is sensitive, maybe if you store the user’s home address or any kind of private values in the payload (I don’t know why you would, though).

To sum up, authentication tokens usually are safe enough (meaning that the payload is not that private) such that using MessageVerifier will suffice. It is also secure in the sense that end users can’t tamper with the data, since the token is signed using your secret_key_base.

Do note that both tokens generated by MessageVerifier and MessageEncryptor are vulnerable to a form of session hijacking if someone can get hold of your token (even when encrypted).

apoeco

apoeco OP

First of all, thank you a lot for that quick response!

Well yes, that makes sense… :smile:

Maybe off topic question

That’s true. Does this hold true with JWT also? I guess so…

bobbypriambodo

bobbypriambodo

Yup! So does with the usual session key.

It all boils down to how you store the token. Are you storing it as a cookie or using localStorage? If it’s cookie, is it HttpOnly to prevent XSS? Is it Secure to prevent sending tokens via HTTP (not HTTPS)? Is your site safe from CSRF?

apoeco

apoeco OP

This is exactly what I imagined… :smiling_imp:
My goal is to build a phoenix project that exposes an API (for a mobile app later on) and also serves a SPA (React) .
So I was thinking that I could probably do the register/login part with plain old server-rendered html forms (using form_for) to prevent CSRF attacks and after a successful login redirect to the SPA (something like "/app") passing down a Phoenix.Token through window.userToken that only stores the user’s ID.
After that the SPA would connect to a Phoenix.Channel and interact with the API only via the channel, using the token for authorization/authentication.

Even if I don’t save the token in localStorage and save it in-memory (redux store), it may still be possible that the token might be leaked in someway from the Javascript application I think…

I wonder what is the best way to achieve the behaviour I mentioned above (SPA consuming the API via Phoenix.Channels)… :confounded: :confounded:

P.S. I am considering https and wss as standards… :smiley_cat:

— All posts loaded —

Where Next? Top

Trending in Questions Top

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews