plug_require_header alternatives and similar packages
Based on the "Framework Components" category.
Alternatively, view plug_require_header alternatives based on common mentions on social networks and blogs.
-
commanded
Use Commanded to build Elixir CQRS/ES applications -
surface
A server-side rendering component library for Phoenix -
ex_admin
ExAdmin is an auto administration package for Elixir and the Phoenix Framework -
phoenix_html
Phoenix.HTML functions for working with HTML strings and templates -
phoenix_ecto
Phoenix and Ecto integration with support for concurrent acceptance testing -
absinthe_plug
Plug support for Absinthe, the GraphQL toolkit for Elixir -
react_phoenix
Make rendering React.js components in Phoenix easy -
phoenix_live_reload
Provides live-reload functionality for Phoenix -
params
Easy parameters validation/casting with Ecto.Schema, akin to Rails' strong parameters. -
phoenix_pubsub_redis
The Redis PubSub adapter for the Phoenix framework -
rummage_ecto
Search, Sort and Pagination for ecto queries -
dayron
A repository `similar` to Ecto.Repo that maps to an underlying http client, sending requests to an external rest api instead of a database -
rummage_phoenix
Full Phoenix Support for Rummage. It can be used for searching, sorting and paginating collections in phoenix. -
phoenix_token_auth
Token authentication solution for Phoenix. Useful for APIs for e.g. single page apps. -
recaptcha
A simple reCaptcha 2 library for Elixir applications. -
sentinel
DEPRECATED - Phoenix Authentication library that wraps Guardian for extra functionality -
plug_graphql
Plug (Phoenix) integration for GraphQL Elixir -
phx_component_helpers
Extensible Phoenix liveview components, without boilerplate -
plug_rails_cookie_session_store
Rails compatible Plug session store -
filterable
Filtering from incoming params in Elixir/Ecto/Phoenix with easy to use DSL. -
multiverse
Elixir package that allows to add compatibility layers via API gateways. -
access pass
provides a full user authentication experience for an API. Includes login,logout,register,forgot password, forgot username, confirmation email and all that other good stuff. Includes plug for checking for authenticated users and macro for generating the required routes. -
scrivener_headers
Scrivener pagination with headers and web linking -
better_params
Cleaner request parameters in Elixir web applications ๐ -
phoenix_pubsub_rabbitmq
RabbitMQ adapter for Phoenix's PubSub layer -
plug_checkup
PlugCheckup provides a Plug for adding simple health checks to your app -
plug_statsd
Send connection response time and count to statsd -
plug_rest
REST behaviour and Plug router for hypermedia web applications in Elixir -
trailing_format_plug
An elixir plug to support legacy APIs that use a rails-like trailing format: http://api.dev/resources.json -
Votex
Implements vote / like / follow functionality for Ecto models in Elixir. Inspired from Acts as Votable gem in Ruby on Rails -
phoenix_html_simplified_helpers
Some helpers for phoenix html( truncate, time_ago_in_words, number_with_delimiter, url_for, current_page? )
Static code analysis for 29 languages.
Do you think we are missing an alternative of plug_require_header or a related project?
README
PlugRequireHeader
An Elixir Plug for requiring and extracting a given header.
Usage
Update your mix.exs
file and run mix deps.get
.
defp deps do
[{:plug_require_header, "~> 0.8"}]
end
Add the plug to e.g. a pipeline in a Phoenix
controller. In this case we will require the request header x-api-key
to be set,
extract its first value and assign it the connection (a Plug.Conn
) for later use
in another plug or action.
defmodule MyPhoenixApp.MyController do
use MyPhoenixApp.Web, :controller
alias Plug.Conn.Status
plug PlugRequireHeader, headers: [api_key: "x-api-key"]
plug :action
def index(conn, _params) do
conn
|> put_status(Status.code :ok)
|> text "The API key used is: #{conn.assigns[:api_key]}"
end
end
Notice how the first value required header "x-api-key"
has been extracted
and can be retrieved using conn.assigns[:api_key]
. An alternative is to use
Plug.Conn.get_req_header/2
to get all the values associated with a given header.
By default, a missing header will return a status code of 403 (forbidden) and halt
the plug pipeline, i.e. no subsequent plugs will be executed. The same is true if
the required header is explicitly set to nil
as the underlying HTTP server will
not include the header. This behaviour however is configurable.
defmodule MyPhoenixApp.MyOtherController do
use MyPhoenixApp.Web, :controller
alias Plug.Conn.Status
plug PlugRequireHeader, headers: [api_key: "x-api-key"],
on_missing: [status: 418, message: %{error: "I'm a teapot!"}, as: :json]
plug :action
def index(conn, _params) do
conn
|> put_status(Status.code :ok)
|> text "The API key used is: #{conn.assigns[:api_key]}"
end
The :on_missing
handling can be given a keyword list of options on how to handle
a missing header.
:status
- aninteger
oratom
to specify the status code. If it's an atom, it'll be looked up using thePlug.Status.code
function. Default is:forbidden
.:message
- abinary
sent as the response body. Default is an empty string.:as
- anatom
describing the content type and encoding. Currently supported alternatives are:text
for plain text and:json
for JSON. Default is:text
.
You can also provide a function that handles the missing header by specifying a
module/function pair in a tuple as the :on_missing
value.
defmodule MyPhoenixApp.MyOtherController do
use MyPhoenixApp.Web, :controller
alias Plug.Conn.Status
plug PlugRequireHeader, headers: [api_key: "x-api-key"],
on_missing: {__MODULE__, :handle_missing_header}
plug :action
def index(conn, _params) do
conn
|> put_status(Status.code :ok)
|> text("The API key used is: #{conn.assigns[:api_key]}")
end
def handle_missing_header(conn, {_connection_assignment_key, missing_header_key}) do
conn
|> send_resp(Status.code(:bad_request), "Missing header: #{missing_header_key}")
|> halt
end
end
If the header is missing or set to nil
the status code, a status code of 400
(bad request) will be returned before the plug pipeline is halted. Notice that
the function specified as a callback needs to be a public function as it'll be
invoked from another module. Also notice that the callback must return a Plug.Conn
struct.
Lastly, it's possible to extract multiple headers at the same time.
plug PlugRequireHeader, headers: [api_key: "x-api-key", magic: "x-magic"]
If extracting multiple headers and specifying an :on_missing
callback, be aware
that the callback will be invoked once for each missing header. Be careful to not send
a response as you can easily run into raising a Plug.Conn.AlreadySentError
. A way of
avoiding this is to have your callback function pattern match on the state of the conn
.
plug PlugRequireHeader, headers: [api_key: "x-api-key", secret: "x-secret"],
on_missing: {__MODULE__, :handle_missing_header}
def handle_missing_header(%Plug.Conn{state: :sent} = conn, _), do: conn
def handle_missing_header(conn, {_connection_assignment_key, missing_header_key}) do
conn
|> send_resp(Status.code(:bad_request), "Missing header: #{missing_header_key}")
|> halt
end
This example will only send a response for the first missing header.
Online documentation
For more information, see the full documentation.
Contributing
- Fork this repository
- Create your feature branch (
git checkout -b I-say-we-take-off-and-nuke-it-from-orbit
) - Commit your changes (
git commit -am 'It is the only way to be sure!'
) - Push to the branch (
git push origin I-say-we-take-off-and-nuke-it-from-orbit
) - Create a new Pull Request