From 854d83c3cf6d491500b23dbc74fb65d3250579f3 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Sun, 11 Aug 2024 08:33:10 -0400 Subject: [PATCH] fix: allow overriding strategy defaults `auto_set_fields` doesn't set defaults, it sets the values *always* and so things like `authorization_params` were not settable --- .tool-versions | 4 ++-- .../strategies/auth0/dsl.ex | 10 ++------- lib/ash_authentication/strategies/custom.ex | 21 +++++++++++++++++++ .../strategies/github/dsl.ex | 10 ++------- .../strategies/google/dsl.ex | 10 ++------- mix.lock | 2 +- test/support/example/user.ex | 1 + 7 files changed, 31 insertions(+), 27 deletions(-) diff --git a/.tool-versions b/.tool-versions index 021359a5..9471d325 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,2 +1,2 @@ -elixir 1.15.6 -erlang 26.1 +elixir 1.17.2-otp-27 +erlang 27.0 diff --git a/lib/ash_authentication/strategies/auth0/dsl.ex b/lib/ash_authentication/strategies/auth0/dsl.ex index 739bda15..f52d6eb8 100644 --- a/lib/ash_authentication/strategies/auth0/dsl.ex +++ b/lib/ash_authentication/strategies/auth0/dsl.ex @@ -24,15 +24,9 @@ defmodule AshAuthentication.Strategy.Auth0.Dsl do #{strategy_override_docs(Assent.Strategy.Auth0)} """, - auto_set_fields: strategy_fields(Assent.Strategy.Auth0, icon: :auth0) + auto_set_fields: [assent_strategy: Assent.Strategy.Auth0, icon: :auth0] }) - end - - defp strategy_fields(strategy, params) do - [] - |> strategy.default_config() - |> Keyword.put(:assent_strategy, strategy) - |> Keyword.merge(params) + |> Custom.set_defaults(Assent.Strategy.Github.default_config([])) end defp strategy_override_docs(strategy) do diff --git a/lib/ash_authentication/strategies/custom.ex b/lib/ash_authentication/strategies/custom.ex index e8e7231d..edaf57db 100644 --- a/lib/ash_authentication/strategies/custom.ex +++ b/lib/ash_authentication/strategies/custom.ex @@ -109,4 +109,25 @@ defmodule AshAuthentication.Strategy.Custom do dsl_patches: [%Dsl.Patch.AddEntity{section_path: path, entity: entity}] end end + + def set_defaults(dsl, defaults) do + Map.update!(dsl, :schema, fn schema -> + Enum.reduce(defaults, schema, fn {key, value}, schema -> + Keyword.update( + schema, + key, + [ + type: :any, + default: value, + hide: true + ], + fn existing -> + existing + |> Keyword.put(:default, value) + |> Keyword.delete(:required) + end + ) + end) + end) + end end diff --git a/lib/ash_authentication/strategies/github/dsl.ex b/lib/ash_authentication/strategies/github/dsl.ex index 15d521fc..a13a53cc 100644 --- a/lib/ash_authentication/strategies/github/dsl.ex +++ b/lib/ash_authentication/strategies/github/dsl.ex @@ -24,15 +24,9 @@ defmodule AshAuthentication.Strategy.Github.Dsl do #{strategy_override_docs(Assent.Strategy.Github)} """, - auto_set_fields: strategy_fields(Assent.Strategy.Github, icon: :github) + auto_set_fields: [icon: :github, assent_strategy: Assent.Strategy.Github] }) - end - - defp strategy_fields(strategy, params) do - [] - |> strategy.default_config() - |> Keyword.put(:assent_strategy, strategy) - |> Keyword.merge(params) + |> Custom.set_defaults(Assent.Strategy.Github.default_config([])) end defp strategy_override_docs(strategy) do diff --git a/lib/ash_authentication/strategies/google/dsl.ex b/lib/ash_authentication/strategies/google/dsl.ex index 498ce89e..945182da 100644 --- a/lib/ash_authentication/strategies/google/dsl.ex +++ b/lib/ash_authentication/strategies/google/dsl.ex @@ -25,15 +25,9 @@ defmodule AshAuthentication.Strategy.Google.Dsl do #{strategy_override_docs(Assent.Strategy.Google)} """, - auto_set_fields: strategy_fields(Assent.Strategy.Google, icon: :google) + auto_set_fields: [icon: :google, assent_strategy: Assent.Strategy.Google] }) - end - - defp strategy_fields(strategy, params) do - [] - |> strategy.default_config() - |> Keyword.put(:assent_strategy, strategy) - |> Keyword.merge(params) + |> Custom.set_defaults(Assent.Strategy.Google.default_config([])) end defp strategy_override_docs(strategy) do diff --git a/mix.lock b/mix.lock index 1d9a35af..d6659699 100644 --- a/mix.lock +++ b/mix.lock @@ -1,5 +1,5 @@ %{ - "absinthe": {:hex, :absinthe, "1.7.6", "0b897365f98d068cfcb4533c0200a8e58825a4aeeae6ec33633ebed6de11773b", [:mix], [{:dataloader, "~> 1.0.0 or ~> 2.0", [hex: :dataloader, repo: "hexpm", optional: true]}, {:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}, {:opentelemetry_process_propagator, "~> 0.2.1", [hex: :opentelemetry_process_propagator, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e7626951ca5eec627da960615b51009f3a774765406ff02722b1d818f17e5778"}, + "absinthe": {:hex, :absinthe, "1.7.8", "43443d12ad2b4fcce60e257ac71caf3081f3d5c4ddd5eac63a02628bcaf5b556", [:mix], [{:dataloader, "~> 1.0.0 or ~> 2.0", [hex: :dataloader, repo: "hexpm", optional: true]}, {:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}, {:opentelemetry_process_propagator, "~> 0.2.1 or ~> 0.3", [hex: :opentelemetry_process_propagator, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c4085df201892a498384f997649aedb37a4ce8a726c170d5b5617ed3bf45d40b"}, "absinthe_plug": {:hex, :absinthe_plug, "1.5.8", "38d230641ba9dca8f72f1fed2dfc8abd53b3907d1996363da32434ab6ee5d6ab", [:mix], [{:absinthe, "~> 1.5", [hex: :absinthe, repo: "hexpm", optional: false]}, {:plug, "~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "bbb04176647b735828861e7b2705465e53e2cf54ccf5a73ddd1ebd855f996e5a"}, "ash": {:hex, :ash, "3.3.2", "e5bb78f4fde87e3b445675e0d1691c81aecc3f5835bac625b6cc72ab8da13061", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:igniter, ">= 0.3.11 and < 1.0.0-0", [hex: :igniter, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: true]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:reactor, "~> 0.9", [hex: :reactor, repo: "hexpm", optional: false]}, {:simple_sat, ">= 0.1.1 and < 1.0.0-0", [hex: :simple_sat, repo: "hexpm", optional: true]}, {:spark, ">= 2.2.8 and < 3.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, "~> 0.2", [hex: :splode, repo: "hexpm", optional: false]}, {:stream_data, "~> 1.0", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "dd264da49cf737318506f8b6b3c3b1aeb925e5ad6ec8ca27786b341ef9566446"}, "ash_graphql": {:hex, :ash_graphql, "1.2.0", "b4b7a754ef722cff1c84cf35291e2ff0402fc91d805e2a01405157087f908a9b", [:mix], [{:absinthe, "~> 1.7", [hex: :absinthe, repo: "hexpm", optional: false]}, {:absinthe_plug, "~> 1.4", [hex: :absinthe_plug, repo: "hexpm", optional: false]}, {:ash, "~> 3.0", [hex: :ash, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "d16986527788f74b2fe8085827d81bad08f1574d8562bc52619c00d43e75aa52"}, diff --git a/test/support/example/user.ex b/test/support/example/user.ex index 1523d4d7..ce46a339 100644 --- a/test/support/example/user.ex +++ b/test/support/example/user.ex @@ -230,6 +230,7 @@ defmodule Example.User do client_id &get_config/2 redirect_uri &get_config/2 client_secret &get_config/2 + authorization_params scope: "openid profile email" end only_marty do