View Source DigexRequest

A digest access authentication implementation for Elixir. It tries to follow RFC 7616 as much as possible.

installation

Installation

If available in Hex, the package can be installed by adding digex_request to your list of dependencies in mix.exs:

def deps do
  [
    {:digex_request, "~> 0.2.0"}
  ]
end

usage

Usage

To use the digest request, you need to create a module and use the DigexRequest behaviour

def DigexClient do
  use DigexRequest
end

after that, build a request and send it:

DigexRequest.new(:get, "http://www.example.com", "username", "password") |> DigexClient.request()

By default the http client used is :httpc, you can override this behaviour by implementing the handle_request/5 function.

request function accept a keyword list that will be passed as it is to the handle_request/5, this is useful to configure some options like timeout.

For example if we want to use Finch to send the request

def DigexClient do
  use DigexRequest

  @impl DigexRequest
  def handle_request(method, url, headers, body, _opts) do
    case Finch.build(method, url, headers, body) |> Finch.request(MyFinch) do
      {:ok, response} ->
        {:ok, 
          %DigexRequest.Response{
            status: response.status, 
            headers: response.headers, 
            body: response.body}}

      {:error, error} ->
        {:error, error}
    end
  end
end

authorization-header-refresh

Authorization header refresh

Each call made by the client will update the DigexRequest struct, so you need to keep this request in order to reuse the authorization header.

req = DigexRequest.new(:get, "http://www.example.com", "username", "password")
{:ok, resp, req} = DigexClient.request(req)

# re-use the authorization header for another path
req = %DigexRequest{req | url: "http://www.example.com/another/path"}
{:ok, resp, req} = DigexClient.request(req)