Packages

Fast. Reusable. Quenya framework helps you generate and build OpenAPIv3 compatible API apps easily from a spec. It greatly reduced the time to build APIs from ideation to production.

Current section

Files

Jump to
quenya lib plugs api_key_plug.ex
Raw

lib/plugs/api_key_plug.ex

defmodule Quenya.Plug.ApiKeyPlug do
@moduledoc """
ApiKey plug to handle authentication with API key
"""
import Plug.Conn
alias Quenya.Plug.UnauthenticatedError
@behaviour Plug
@spec init(keyword()) :: keyword()
def init(opts) do
opts
end
@spec call(Plug.Conn.t(), keyword()) :: Plug.Conn.t()
def call(conn, opts) do
case get_req_header(conn, opts[:api_key]) do
[v] ->
verify_api_key(conn, v)
[] ->
raise(
UnauthenticatedError,
"Expected an API key in the `authorization` header. Got nothing."
)
end
end
defp verify_api_key(conn, _value) do
conn
end
end