defmodule BasicAuth do
  @moduledoc """
  Plug for adding basic authentication.

  Deprecated as `Plug.BasicAuth` is now part of Plug
  """

  @deprecated "Use `Plug.BasicAuth`"

  defmodule Callback do
    @moduledoc false
    defstruct callback: nil, realm: nil
  end

  def init(use_config: config_options, custom_response: custom_response) do
    BasicAuth.Configured.init(config_options, custom_response)
  end

  def init(use_config: config_options) do
    BasicAuth.Configured.init(config_options)
  end

  def init(options) when is_list(options) do
    BasicAuth.WithCallback.init(options)
  end

  def init(_) do
    raise ArgumentError, """

    Usage of BasicAuth using application config:
    plug BasicAuth, use_config: {:your_app, :your_config}

    -OR-
    Using custom authentication function:
    plug BasicAuth, callback: &MyCustom.function/3

    Where :callback takes either
    * a conn, username and password and returns a conn.
    * a conn and a key and returns a conn
    """
  end

  def call(conn, options) do
    header_content = Plug.Conn.get_req_header(conn, "authorization")
    respond(conn, header_content, options)
  end

  defp respond(conn, header_content, config_options = %BasicAuth.Configured{}) do
    BasicAuth.Configured.respond(conn, header_content, config_options)
  end

  defp respond(conn, header_content, config_options = %BasicAuth.WithCallback{}) do
    BasicAuth.WithCallback.respond(conn, header_content, config_options)
  end
end
