|
| 1 | +defmodule AshStorage.Plug.Redirect do |
| 2 | + @moduledoc """ |
| 3 | + A Plug that redirects to the storage service's URL instead of proxying file bytes. |
| 4 | +
|
| 5 | + Useful when you want to: |
| 6 | + - Apply application-level auth/signature checks before granting access, but |
| 7 | + - Avoid the cost of streaming bytes through the application (presigned S3/Azure |
| 8 | + URLs let the client fetch directly from the storage service). |
| 9 | +
|
| 10 | + Compared to `AshStorage.Plug.Proxy`, this plug never calls `download/2` on |
| 11 | + the underlying service. It calls `url/2` and issues an HTTP redirect. |
| 12 | +
|
| 13 | + ## Usage |
| 14 | +
|
| 15 | + In your router: |
| 16 | +
|
| 17 | + forward "/storage", AshStorage.Plug.Redirect, |
| 18 | + service: {AshStorage.Service.S3, bucket: "my-bucket", region: "us-east-1"} |
| 19 | +
|
| 20 | + With signed URL verification (matches `AshStorage.Plug.Proxy`): |
| 21 | +
|
| 22 | + forward "/storage", AshStorage.Plug.Redirect, |
| 23 | + service: {AshStorage.Service.S3, bucket: "my-bucket"}, |
| 24 | + secret: "a-long-secret-key" |
| 25 | +
|
| 26 | + ## Disposition / filename forwarding |
| 27 | +
|
| 28 | + When the inbound request includes `?disposition=attachment&filename=foo.pdf`, |
| 29 | + those values are merged into the service options before `url/2` is called, |
| 30 | + so backends that support response-header overrides (S3 |
| 31 | + `response-content-disposition`, Azure SAS `rscd`) can encode them into the |
| 32 | + generated URL. |
| 33 | +
|
| 34 | + ## Options |
| 35 | +
|
| 36 | + - `:service` - (required) the `{module, opts}` tuple for the storage service. |
| 37 | + - `:secret` - secret key for verifying signed app-level URLs. When set, |
| 38 | + requests without a valid `token`/`expires` query pair are rejected with 403. |
| 39 | + - `:status` - HTTP status to use for the redirect (default: `302`). Use `307` |
| 40 | + if method preservation matters for clients/tools that consume the URL. |
| 41 | + """ |
| 42 | + |
| 43 | + @behaviour Plug |
| 44 | + |
| 45 | + @impl true |
| 46 | + def init(opts) do |
| 47 | + {service_mod, service_opts} = Keyword.fetch!(opts, :service) |
| 48 | + |
| 49 | + %{ |
| 50 | + service_mod: service_mod, |
| 51 | + service_opts: service_opts, |
| 52 | + secret: Keyword.get(opts, :secret), |
| 53 | + status: Keyword.get(opts, :status, 302) |
| 54 | + } |
| 55 | + end |
| 56 | + |
| 57 | + @impl true |
| 58 | + def call(conn, opts) do |
| 59 | + key = conn.path_info |> Enum.join("/") |
| 60 | + |
| 61 | + if key == "" do |
| 62 | + conn |> Plug.Conn.send_resp(404, "Not Found") |> Plug.Conn.halt() |
| 63 | + else |
| 64 | + case verify_signature(conn, opts) do |
| 65 | + :ok -> |
| 66 | + ctx = AshStorage.Service.Context.new(merged_service_opts(conn, opts)) |
| 67 | + url = opts.service_mod.url(key, ctx) |
| 68 | + |
| 69 | + conn |
| 70 | + |> Plug.Conn.put_resp_header("location", url) |
| 71 | + |> Plug.Conn.put_resp_header("cache-control", "no-store, private") |
| 72 | + |> Plug.Conn.send_resp(opts.status, "") |
| 73 | + |> Plug.Conn.halt() |
| 74 | + |
| 75 | + {:error, :forbidden} -> |
| 76 | + conn |> Plug.Conn.send_resp(403, "Forbidden") |> Plug.Conn.halt() |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + defp merged_service_opts(conn, opts) do |
| 82 | + params = Plug.Conn.fetch_query_params(conn).query_params |
| 83 | + |
| 84 | + opts.service_opts |
| 85 | + |> maybe_put(:disposition, params["disposition"]) |
| 86 | + |> maybe_put(:filename, params["filename"]) |
| 87 | + end |
| 88 | + |
| 89 | + defp maybe_put(keyword, _key, nil), do: keyword |
| 90 | + defp maybe_put(keyword, _key, ""), do: keyword |
| 91 | + defp maybe_put(keyword, key, value), do: Keyword.put(keyword, key, value) |
| 92 | + |
| 93 | + defp verify_signature(_conn, %{secret: nil}), do: :ok |
| 94 | + |
| 95 | + defp verify_signature(conn, %{secret: secret}) do |
| 96 | + params = Plug.Conn.fetch_query_params(conn).query_params |
| 97 | + |
| 98 | + with token when is_binary(token) <- params["token"], |
| 99 | + expires when is_binary(expires) <- params["expires"], |
| 100 | + {expires_at, ""} <- Integer.parse(expires), |
| 101 | + true <- expires_at > System.system_time(:second) do |
| 102 | + key = conn.path_info |> Enum.join("/") |
| 103 | + expected = AshStorage.Token.sign(secret, key, expires_at) |
| 104 | + |
| 105 | + if Plug.Crypto.secure_compare(token, expected) do |
| 106 | + :ok |
| 107 | + else |
| 108 | + {:error, :forbidden} |
| 109 | + end |
| 110 | + else |
| 111 | + _ -> {:error, :forbidden} |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments