Skip to content

Instantly share code, notes, and snippets.

@willrax
Created February 9, 2016 09:28
Show Gist options
  • Save willrax/9c6ab0ce663c6df17e9a to your computer and use it in GitHub Desktop.
Save willrax/9c6ab0ce663c6df17e9a to your computer and use it in GitHub Desktop.
defmodule Plugger.Authenticate do
def init(opts), do: opts
def call(conn, opts) do
conn
|> get_auth_headers
|> authenticate(opts[:token])
end
def get_auth_headers(conn) do
{conn, Plug.Conn.get_req_header(conn, "authorization")}
end
def authenticate({conn, [token]}, token) do
conn
end
def authenticate({conn, _}, token) do
conn
|> Plug.Conn.send_resp(401, "Not Authorised")
|> Plug.Conn.halt
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment