Intended to be compatible with the ruby version at http://www.github.com/zvkemp/hmac_auth.
Note:
If used to verify POST requests, the included HMACAuthEx.Plug
must be used before Plug.Parsers
(it requires
access to the raw request body, which is removed by json parsing et al).
If available in Hex, the package can be installed as:
- Add
hmac_auth_ex
to your list of dependencies inmix.exs
:
def deps do
[{:hmac_auth_ex, "~> 0.3.0"}]
end
- Add the required config in
config/{env}.exs
:
config :hmac_auth_ex, keys: %{key => value}
- Ensure
hmac_auth_ex
is started before your application:
def application do
[applications: [:hmac_auth_ex]]
end
- (Optional) If using the plug, add this to your endpoint (usually before the router):
plug HMACAuthEx.Plug
This will add an hmac_verified: boolean
key to conn.private
. A basic authentication function might look like this:
defp authenticate(conn, _) do
cond do
Mix.env == :dev -> conn # to skip verification in the dev environment
conn.private.hmac_verified -> conn
true ->
conn
|> put_status(:unauthorized)
|> json(%{error: :signature})
|> halt
end
end