Current section
25 Versions
Jump to
Current section
25 Versions
Compare versions
15
files changed
+204
additions
-53
deletions
| @@ -19,7 +19,7 @@ Add `mux` to your list of dependencies in `mix.exs`: | |
| 19 19 | ```elixir |
| 20 20 | def deps do |
| 21 21 | [ |
| 22 | - {:mux, "~> 1.3.0"} |
| 22 | + {:mux, "~> 1.4.0"} |
| 23 23 | ] |
| 24 24 | end |
| 25 25 | ``` |
| @@ -80,3 +80,36 @@ def create(conn, params, mux_client) do | |
| 80 80 | # ... |
| 81 81 | end |
| 82 82 | ``` |
| 83 | + |
| 84 | + #### Verifying Webhook Signatures in Phoenix |
| 85 | + |
| 86 | + Note that when calling `Mux.Webhooks.verify_header/3` in Phoenix you will need to pass in the raw request |
| 87 | + body, not the parsed JSON. Phoenix has a nice solution for doing this [example](https://github.com/phoenixframework/phoenix/issues/459#issuecomment-440820663). |
| 88 | + |
| 89 | + Read more about verifying webhook signatures in [our guide](https://docs.mux.com/docs/webhook-security) |
| 90 | + |
| 91 | + ```elixir |
| 92 | + defmodule MyAppWeb.BodyReader do |
| 93 | + def read_body(conn, opts) do |
| 94 | + {:ok, body, conn} = Plug.Conn.read_body(conn, opts) |
| 95 | + body = [body | conn.private[:raw_body] || []] |
| 96 | + conn = Plug.Conn.put_private(conn, :raw_body, body) |
| 97 | + {:ok, body, conn} |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + # endpoint.ex |
| 102 | + plug Plug.Parsers, |
| 103 | + parsers: [:urlencoded, :multipart, :json], |
| 104 | + pass: ["*/*"], |
| 105 | + body_reader: {MyAppWeb.BodyReader, :read_body, []}, |
| 106 | + json_decoder: Phoenix.json_library() |
| 107 | + |
| 108 | + # controller |
| 109 | + signature_header = List.first(get_req_header(conn, "mux-signature")) |
| 110 | + raw_body = List.first(conn.private[:raw_body]) |
| 111 | + Mux.Webhooks.verify_header(raw_body, signature_header, secret) |
| 112 | + ``` |
| 113 | + |
| 114 | + You will most likely have to store the raw body before it gets parsed and then extract it later and |
| 115 | + pass it into `Mux.Webhooks.verify_header/3` |
| @@ -4,16 +4,16 @@ | |
| 4 4 | <<"Official Elixir package for interacting with the Mux APIs">>}. |
| 5 5 | {<<"elixir">>,<<"~> 1.6">>}. |
| 6 6 | {<<"files">>, |
| 7 | - [<<"lib">>,<<"lib/mux">>,<<"lib/mux/base.ex">>,<<"lib/mux/data">>, |
| 8 | - <<"lib/mux/data/errors.ex">>,<<"lib/mux/data/exports.ex">>, |
| 9 | - <<"lib/mux/data/filters.ex">>,<<"lib/mux/data/incidents.ex">>, |
| 10 | - <<"lib/mux/data/metrics.ex">>,<<"lib/mux/data/video_views.ex">>, |
| 11 | - <<"lib/mux/exception.ex">>,<<"lib/mux/middleware">>, |
| 12 | - <<"lib/mux/middleware/simplify_response.ex">>,<<"lib/mux/support">>, |
| 13 | - <<"lib/mux/support/fixtures.ex">>,<<"lib/mux/token.ex">>, |
| 14 | - <<"lib/mux/video">>,<<"lib/mux/video/assets.ex">>, |
| 15 | - <<"lib/mux/video/live_streams.ex">>,<<"lib/mux/video/playback_ids.ex">>, |
| 16 | - <<"lib/mux/video/signing_keys.ex">>,<<"lib/mux/video/uploads.ex">>, |
| 7 | + [<<"lib">>,<<"lib/mux">>,<<"lib/mux/middleware">>, |
| 8 | + <<"lib/mux/middleware/simplify_response.ex">>,<<"lib/mux/video">>, |
| 9 | + <<"lib/mux/video/live_streams.ex">>,<<"lib/mux/video/uploads.ex">>, |
| 10 | + <<"lib/mux/video/signing_keys.ex">>,<<"lib/mux/video/playback_ids.ex">>, |
| 11 | + <<"lib/mux/video/assets.ex">>,<<"lib/mux/exception.ex">>, |
| 12 | + <<"lib/mux/token.ex">>,<<"lib/mux/webhooks.ex">>,<<"lib/mux/support">>, |
| 13 | + <<"lib/mux/support/fixtures.ex">>,<<"lib/mux/base.ex">>,<<"lib/mux/data">>, |
| 14 | + <<"lib/mux/data/video_views.ex">>,<<"lib/mux/data/exports.ex">>, |
| 15 | + <<"lib/mux/data/metrics.ex">>,<<"lib/mux/data/errors.ex">>, |
| 16 | + <<"lib/mux/data/incidents.ex">>,<<"lib/mux/data/filters.ex">>, |
| 17 17 | <<"lib/mux.ex">>,<<"mix.exs">>,<<"README.md">>,<<"LICENSE">>]}. |
| 18 18 | {<<"licenses">>,[<<"MIT">>]}. |
| 19 19 | {<<"links">>,[{<<"GitHub">>,<<"https://github.com/muxinc/mux-elixir">>}]}. |
| @@ -34,4 +34,4 @@ | |
| 34 34 | {<<"optional">>,false}, |
| 35 35 | {<<"repository">>,<<"hexpm">>}, |
| 36 36 | {<<"requirement">>,<<"~> 1.9">>}]]}. |
| 37 | - {<<"version">>,<<"1.3.0">>}. |
| 37 | + {<<"version">>,<<"1.4.0">>}. |
| @@ -27,7 +27,13 @@ defmodule Mux do | |
| 27 27 | } |
| 28 28 | |
| 29 29 | """ |
| 30 | - def client(options \\ []), do: client(Application.get_env(:mux, :access_token_id), Application.get_env(:mux, :access_token_secret), options) |
| 30 | + def client(options \\ []), |
| 31 | + do: |
| 32 | + client( |
| 33 | + Application.get_env(:mux, :access_token_id), |
| 34 | + Application.get_env(:mux, :access_token_secret), |
| 35 | + options |
| 36 | + ) |
| 31 37 | |
| 32 38 | @doc """ |
| 33 39 | Creates a new connection struct that takes an access token ID and |
| @@ -3,8 +3,8 @@ defmodule Mux.Base do | |
| 3 3 | |
| 4 4 | use Tesla |
| 5 5 | |
| 6 | - plug Mux.Middleware.SimplifyResponse |
| 7 | - plug Tesla.Middleware.JSON |
| 6 | + plug(Mux.Middleware.SimplifyResponse) |
| 7 | + plug(Tesla.Middleware.JSON) |
| 8 8 | |
| 9 9 | @defaults [ |
| 10 10 | base_url: "https://api.mux.com" |
| @@ -43,9 +43,9 @@ defmodule Mux.Base do | |
| 43 43 | def new(token_id, token_secret, opts \\ []) do |
| 44 44 | opts = Keyword.merge(@defaults, opts) |> Enum.into(%{}) |
| 45 45 | |
| 46 | - Tesla.build_client [ |
| 46 | + Tesla.build_client([ |
| 47 47 | {Tesla.Middleware.BaseUrl, opts.base_url}, |
| 48 48 | {Tesla.Middleware.BasicAuth, %{username: token_id, password: token_secret}} |
| 49 | - ] |
| 49 | + ]) |
| 50 50 | end |
| 51 51 | end |
| @@ -15,12 +15,12 @@ defmodule Mux.Data.Errors do | |
| 15 15 | iex> client = Mux.client("my_token_id", "my_token_secret") |
| 16 16 | iex> {:ok, errors, _env} = Mux.Data.Errors.list(client) |
| 17 17 | iex> errors |
| 18 | - #{inspect Fixtures.errors["data"]} |
| 18 | + #{inspect(Fixtures.errors()["data"])} |
| 19 19 | |
| 20 20 | iex> client = Mux.client("my_token_id", "my_token_secret") |
| 21 21 | iex> {:ok, errors, _env} = Mux.Data.Errors.list(client, filters: ["operating_system:windows"], timeframe: ["24:hours"]) |
| 22 22 | iex> errors |
| 23 | - #{inspect Fixtures.errors["data"]} |
| 23 | + #{inspect(Fixtures.errors()["data"])} |
| 24 24 | |
| 25 25 | """ |
| 26 26 | def list(client, params \\ []) do |
Loading more files…