Skip to content
Basic Access Authentication in Plug applications
Elixir
Latest commit e18e199 Aug 15, 2015 @lexmag Release v1.2.1
Failed to load latest commit information.
lib Header fix for Phoenix 0.13.1 Jun 16, 2015
test Remove usage of deprecated `Plug.Test.conn/4` Aug 15, 2015
.gitignore Initial commit Nov 4, 2014
.travis.yml Update .travis.yml Jul 25, 2015
LICENSE Initial commit Nov 4, 2014
README.md Release v1.2.1 Aug 15, 2015
mix.exs Release v1.2.1 Aug 15, 2015
mix.lock Accept Plug v1.0.0 Aug 15, 2015

README.md

Blaguth Build Status

Basic Access Authentication in Plug applications.

Installation

Add Blaguth as a dependency to your mix.exs file:

defp deps do
  [{:blaguth, "~> 1.2.0"}]
end

After you are done, run mix deps.get in your shell to fetch the dependencies.

Usage

Add Blaguth on top of a Plug Stack as follows:

defmodule CavePlug do
  import Plug.Conn
  use Plug.Builder

  plug Blaguth, realm: "Secret",
    credentials: {"Ali Baba", "Open Sesame"}

  plug :index

  def index(conn, _opts) do
    send_resp(conn, 200, "Hello Ali Baba")
  end
end

If you need more precise control over authentication process:

defmodule AdvancedPlug do
  import Plug.Conn
  use Plug.Router

  plug Blaguth

  plug :match
  plug :dispatch

  get "/" do
    send_resp(conn, 200, "Everyone can see me!")
  end

  get "/secret" do
    if authenticated?(conn.assigns) do
      send_resp(conn, 200, "I'm only accessible if you know the password")
    else
      Blaguth.halt_with_login(conn, "Secret")
    end
  end

  defp authenticated?(%{credentials: {user, pass}}) do
    User.authenticate(user, pass)
  end
end

License

This software is licensed under the ISC license.

Something went wrong with that request. Please try again.