| 1 |
|
defmodule DaProductAppWeb.Plugs.RawBody do |
| 2 |
|
@moduledoc """ |
| 3 |
|
Plug to capture the raw request body for webhook signature verification. |
| 4 |
|
""" |
| 5 |
|
|
| 6 |
|
@behaviour Plug |
| 7 |
|
|
| 8 |
:-( |
def init(opts), do: opts |
| 9 |
|
|
| 10 |
|
def call(conn, _opts) do |
| 11 |
:-( |
case read_body(conn) do |
| 12 |
|
{:ok, body, conn} -> |
| 13 |
:-( |
Plug.Conn.assign(conn, :raw_body, body) |
| 14 |
|
|
| 15 |
|
{:more, _partial_body, conn} -> |
| 16 |
|
# Handle large bodies if needed |
| 17 |
:-( |
conn |
| 18 |
|
|
| 19 |
|
{:error, _reason} -> |
| 20 |
:-( |
conn |
| 21 |
|
end |
| 22 |
|
end |
| 23 |
|
|
| 24 |
|
defp read_body(conn) do |
| 25 |
:-( |
Plug.Conn.read_body(conn, length: 1_000_000) |
| 26 |
|
end |
| 27 |
|
end |