Skip to content

Commit

Permalink
fix: allow overriding strategy defaults
Browse files Browse the repository at this point in the history
`auto_set_fields` doesn't set defaults, it sets the values *always*
and so things like `authorization_params` were not settable
  • Loading branch information
zachdaniel committed Aug 11, 2024
1 parent ccd0eb0 commit 854d83c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elixir 1.15.6
erlang 26.1
elixir 1.17.2-otp-27
erlang 27.0
10 changes: 2 additions & 8 deletions lib/ash_authentication/strategies/auth0/dsl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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([]))

Check warning on line 29 in lib/ash_authentication/strategies/auth0/dsl.ex

View workflow job for this annotation

GitHub Actions / mix credo --strict

Nested modules could be aliased at the top of the invoking module.
end

defp strategy_override_docs(strategy) do
Expand Down
21 changes: 21 additions & 0 deletions lib/ash_authentication/strategies/custom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->

Check warning on line 124 in lib/ash_authentication/strategies/custom.ex

View workflow job for this annotation

GitHub Actions / mix credo --strict

Function body is nested too deep (max depth is 2, was 3).
existing
|> Keyword.put(:default, value)
|> Keyword.delete(:required)
end
)
end)
end)
end
end
10 changes: 2 additions & 8 deletions lib/ash_authentication/strategies/github/dsl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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([]))

Check warning on line 29 in lib/ash_authentication/strategies/github/dsl.ex

View workflow job for this annotation

GitHub Actions / mix credo --strict

Nested modules could be aliased at the top of the invoking module.
end

defp strategy_override_docs(strategy) do
Expand Down
10 changes: 2 additions & 8 deletions lib/ash_authentication/strategies/google/dsl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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([]))

Check warning on line 30 in lib/ash_authentication/strategies/google/dsl.ex

View workflow job for this annotation

GitHub Actions / mix credo --strict

Nested modules could be aliased at the top of the invoking module.
end

defp strategy_override_docs(strategy) do
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -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"},
Expand Down
1 change: 1 addition & 0 deletions test/support/example/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 854d83c

Please sign in to comment.