From e5a9f27c6a8c57fdb242de4410e7c388037e2b49 Mon Sep 17 00:00:00 2001 From: "Vitor M. A. da Cruz" Date: Mon, 5 Aug 2024 17:09:36 -0300 Subject: [PATCH] improvement: enable custom `http_adapters` (#760) Allow users to customize the behavior of the http client used by the oauth-based strategies by configuring a http adapter. --- lib/ash_authentication.ex | 12 ++++++++++++ lib/ash_authentication/strategies/oauth2/plug.ex | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/ash_authentication.ex b/lib/ash_authentication.ex index ac6378f..48adff8 100644 --- a/lib/ash_authentication.ex +++ b/lib/ash_authentication.ex @@ -67,6 +67,18 @@ defmodule AshAuthentication do 3. `AshAuthentication.Strategy.MagicLink` - authenticate by sending a single-use link to the user. + ### HTTP client settings + + Most of the authentication strategies based on `OAuth2` wrap the [`assent`](https://hex.pm/packages/assent) package. + + If you needs to customize the behavior of the http client used by `assent`, define a custom `http_adapter` in the + application settings: + + `config :ash_authentication, :http_adapter, {Assent.HTTPAdapter.Finch, supervisor: MyApp.CustomFinch}` + + See [`assent's documentation`](https://hexdocs.pm/assent/README.html#http-client) for more details on the supported + http clients and their configuration. + ## Add-ons Add-ons are like strategies, except that they don't actually provide diff --git a/lib/ash_authentication/strategies/oauth2/plug.ex b/lib/ash_authentication/strategies/oauth2/plug.ex index 0525c92..47a6247 100644 --- a/lib/ash_authentication/strategies/oauth2/plug.ex +++ b/lib/ash_authentication/strategies/oauth2/plug.ex @@ -82,13 +82,13 @@ defmodule AshAuthentication.Strategy.OAuth2.Plug do config = strategy |> Map.take(@raw_config_attrs) - |> Map.put(:http_adapter, {Finch, supervisor: AshAuthentication.Finch}) with {:ok, config} <- add_secret_value(config, strategy, :base_url), {:ok, config} <- add_secret_value(config, strategy, :authorize_url, !!strategy.base_url), {:ok, config} <- add_secret_value(config, strategy, :client_id, !!strategy.base_url), {:ok, config} <- add_secret_value(config, strategy, :client_secret, !!strategy.base_url), {:ok, config} <- add_secret_value(config, strategy, :token_url, !!strategy.base_url), + {:ok, config} <- add_http_adapter(config), {:ok, config} <- add_secret_value( config, @@ -200,4 +200,14 @@ defmodule AshAuthentication.Strategy.OAuth2.Plug do {:error, reason} end end + + defp add_http_adapter(config) do + http_adapter = Application.get_env( + :ash_authentication, + :http_adapter, + {Finch, supervisor: AshAuthentication.Finch} + ) + + {:ok, Map.put(config, :http_adapter, http_adapter)} + end end