adarsh1021

adarsh1021

CSRF issue when using session based authentication for json APIs

My current authentication system looks like this:

  1. On entering username and password, the login endpoint is called (not a form action) that authenticates the user.
  2. A new session (row in an internal table) is created for this user, and this session id is put in the session cookie.
  3. On every request, I fetch the session from cookie, get the session id, do a lookup to identify the user.

On running sobelow, I get the CSRF error because is uses the :fetch_session plug. On adding the :protect_from_forgery, naturally it throws an error because I do not have a CSRF token set for my APIs.

The alternative is to use a bearer token mechanism for authentication of APIs. I also need the user to be logged in for an extended period of time. This means along with the bearer token I need to use a refresh token mechanism as well. I also figured out storing the refresh token on the client side is a bad idea. I came across the solution to use a HttpOnly cookie to store the refresh token.

But I am confused by the overall flow here:

  1. Obtain a CSRF token for the client.
  2. Send the CSRF token along with the login credentials.
  3. Once authenticated, generate refresh token and bearer token. Put refresh token in the HttpOnly cookie and send back bearer token.
  4. For every API call use the bearer token for authentication.
  5. Once the bearer token expires, what do I do? To get access to the refresh token I need access to the cookies, which again means I need a CSRF token?

What am I missing here? I feel like there has to be a simpler way.
Would really appreciate any help / guidance on this :smile:

References:

Marked As Solved

cospin

cospin

Before the token expires, a request is made to an endpoint that is responsible for generating another access token. The server does have access to the refresh token, so once it receives the request, it verifies the refresh token stored in the cookies, and if everything is fine, it returns a new access token to the client, and now the client uses that.

This is a simple way to accomplish it, there are other more advanced and secure measures such as using a fingerprint. This guide is very comprehensive, although it is somewhat focused on GraphQL, it explains all the concepts very well: Your GraphQL guide to handling JWTs on frontend clients

Last Post!

adarsh1021

adarsh1021

This is helpful, thank you!!

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
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

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement