setop

setop

How to prevent a user to access data of an other user?

I’m working on a codebase composed of Phoenix+Absinthe+Ecto.

I’m looking for the right pattern to prevent a user to access data of another user through the GraphQL API.

For example let’s say a user has a login and an email (for authentication) and posts. I want other users to be able to see author of a post (ie: login of a user) but not other user’s email.

I have seen many concepts related to authorizations/permissions in Absinthe:

  • meta
    • private: true (don’t know what it enforce)
  • middleware
  • scopes (like scope: false or scope: Some.Module)
  • rules
  • Rajska.Authorization

I have also some part of the schema that goes directly from Absinthe to Ecto, without any resolver (some black magic introspection like might operate).

Most of them are already used in the code base. But they mainly ensure that the user is authenticated, is of role “user” and have some permissions like “read:profile”. But with that, a user can read the profile of an other.

I find the documentation or Absinthe pretty scarce on this aspect and tutorial I found on the web don’t go that deep.

What is the right approach to secure my API ?

First 4 of 4 Posts Switch mode

setop

setop OP

:open_mouth:
󠀠󠀠󠀠󠀠󠀠

jtippett

jtippett

Welcome to the “fun” of graphql, where you have to keep track at all times of who’s asking. You’re about to become very familiar with the word “resolvers”.

You’ll need something like this:

# in your user type
object :user do
  field!(:id, :id)
  field!(:login, :string)
  field(:email, :string, resolve: &Resolvers.UserResolver.resolve_email/3)
end

# the resolver it talks to
defmodule MyAppWeb.Resolvers.UserResolver do
  def resolve_email(%{id: user_id, email: email}, _args, %{context: %{current_user: current_user}}) do
    if current_user.id == user_id do
      {:ok, email}
    else
      {:error, :unauthorized}
    end
  end
end

In a nutshell, this overrides the way the user’s email is looked up from the user struct, and delegates it to the resolve_email method, which receives the user struct it’s acting on in the first argument. The context is available in the 3rd argument and you can check its current_user to figure out if you should be sending back the email or not.

I agree the docs are somewhat lacking, this is a basic pattern you will be doing A LOT and it took me longer than i would like to admit to figure it out too.

setop

setop OP

That’s what I opted for and it works.
Thanks a lot for you reply.

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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 & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement