NoWayJose.Jwks.HttpClient behaviour (NoWayJose v1.0.2)

View Source

Behaviour for HTTP clients used by JWKS fetchers.

The default implementation uses Req (if available). You can provide a custom implementation by passing the :http_client option to NoWayJose.start_jwks_fetcher/3.

Options

The following options are supported by the default implementation:

  • :timeout - Connection and receive timeout in milliseconds (default: 30000)
  • :connect_options - Options passed to Req's :connect_options, including:
    • :transport_opts - Options for the underlying socket, such as SSL settings

SSL Configuration

To disable SSL certificate verification (useful for self-signed certificates in development/staging environments):

NoWayJose.start_jwks_fetcher("auth0", url,
  http_opts: [
    connect_options: [
      transport_opts: [verify: :verify_none]
    ]
  ]
)

To use a custom CA certificate:

NoWayJose.start_jwks_fetcher("auth0", url,
  http_opts: [
    connect_options: [
      transport_opts: [
        verify: :verify_peer,
        cacertfile: "/path/to/ca-cert.pem"
      ]
    ]
  ]
)

Custom Implementation

To implement a custom HTTP client:

defmodule MyApp.HttpClient do
  @behaviour NoWayJose.Jwks.HttpClient

  @impl true
  def fetch(url, opts) do
    case HTTPoison.get(url, [], opts) do
      {:ok, %{status_code: 200, body: body}} -> {:ok, body}
      {:ok, %{status_code: status}} -> {:error, {:http_error, status}}
      {:error, reason} -> {:error, reason}
    end
  end
end

Then use it:

NoWayJose.start_jwks_fetcher("auth0", url,
  http_client: MyApp.HttpClient
)

Summary

Callbacks

Fetches the content at the given URL.

Functions

Default implementation using Req.

Types

body()

@type body() :: String.t()

error()

@type error() :: {:http_error, non_neg_integer()} | term()

opts()

@type opts() :: keyword()

url()

@type url() :: String.t()

Callbacks

fetch(url, opts)

@callback fetch(url(), opts()) :: {:ok, body()} | {:error, error()}

Fetches the content at the given URL.

Should return {:ok, body} on success (HTTP 200), or {:error, reason} on failure.

Functions

fetch(url, opts \\ [])

@spec fetch(url(), opts()) :: {:ok, body()} | {:error, error()}

Default implementation using Req.

Falls back to a simple error if Req is not available.