kamaroly

kamaroly

How do you limit file download to authorised users only, even if they have a direct link?

I am working on app with options to upload files and share them among users, but my challenge is that a user can access the file without signing in, if they have the link or file path. I want to limit file access to only those who are logged in so that I can control who can see what file.

How can I achieve that in Phoenix? An example would be helpful.

Showing Posts 1 to 5

Stefano1990

Stefano1990

Use mix phx.gen.auth and have a look at the files it generated.

Phxie

Phxie

To clarify, do you want:

  • Unauthorized users can view but not download
  • Unauthorized users cannot view or download

If you want the second option, then its pretty straight forward as you can just make authorized routes in the router.

If you run mix.gen.auth you will get something like the below in the router.

  scope "/", AppWeb do
    pipe_through [:browser, :require_authenticated_user]
  
    live_session :require_authenticated_user,
      on_mount: [{AppWeb.UserAuth, :ensure_authenticated}] do

Routes within the above scope will get automatically redirected if the user fails to pass the UserAuth requirements.

If you want the first option, to allow users to view content but not download it, I’d assume it would be more of an Nginx type thing or whatever you choose to use.

kokolegorille

kokolegorille

You need to serve your uploaded files through a controller, not plug static. You also need to persists metadata along side your uploads, for authorization purpose, like user_id, and more if You want a more granular access.

In the controller, You will be able to check who is allowed to access your data, and use send_download.

Hermanverschooten

Hermanverschooten

You can continue using Plug.Static by doing something similar as in this article.
We have recently done this in an app, create the static pipeline, then combine it with a plug that does the authentication.

  pipeline :static do
    plug :accepts, ["html"]
    plug MyAppWeb.Plugs.Authenticate

    plug Plug.Static,
      at: "/help",
      from: {__MODULE__, :pages_path, []}

    plug :needs_index
  end

  scope "/help", MyAppHelpWeb do
    pipe_through :static
    get "/*path", HelpController, :index
  end

the :needs_index plug just checks to see if we are asking for a path instead of a file, and tags on index.html.
The controller renders a 404, because we only get there if the file doesn’t exist.

kamaroly

kamaroly OP

This is what I was looking for. I ended up having a controller that looks like this

defmodule MyAppWeb.DownloadController do
  use MyAppWeb, :controller

  @upload_storage_path Application.get_env(:my_app, :uploads_folder)
  def download(%{assigns: %{current_user: user}} = conn, %{"file_id" => file_id}) do
    actor = get_user(user)

    case my_app.Files.get_file(file_id, actor: actor) do
      {:ok, file} ->
        path = get_download_path(file)
        send_download(conn, {:file, path})

      {:error, %Ash.Error.Query.NotFound{}} ->
        conn
        |> put_flash(:error, "The file you are looking for does not exist.")
        |> redirect(to: ~p"/files")
    end
  end

  def get_download_path(file) do
    Path.join(
      Application.app_dir(:my_app, @upload_storage_path),
      Path.basename(file.storage_path)
    )
  end
end
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews