fix(Strategy.Password): Preparations should allow strategy to be passed in. (#314)

Basically a copy-pasta of the `find_strategy/3` function from `HashPasswordChange` to allow for the strategy to be passed in via the context(s) or preparation options.
This commit is contained in:
James Harton 2023-05-29 10:54:32 +12:00 committed by GitHub
parent 38a1f29c6e
commit 39d25cadea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 4 deletions

View file

@ -20,8 +20,8 @@ defmodule AshAuthentication.Strategy.Password.SignInPreparation do
@doc false
@impl true
@spec prepare(Query.t(), keyword, Preparation.context()) :: Query.t()
def prepare(query, _opts, _context) do
strategy = Info.strategy_for_action!(query.resource, query.action.name)
def prepare(query, options, context) do
{:ok, strategy} = find_strategy(query, context, options)
identity_field = strategy.identity_field
identity = Query.get_argument(query, identity_field)
@ -122,4 +122,19 @@ defmodule AshAuthentication.Strategy.Password.SignInPreparation do
record
end
end
defp find_strategy(query, context, options) do
with :error <- Info.strategy_for_action(query.resource, query.action.name),
:error <- Map.fetch(query.context, :strategy_name),
:error <- Map.fetch(context, :strategy_name),
:error <- Keyword.fetch(options, :strategy_name) do
:error
else
{:ok, strategy_name} when is_atom(strategy_name) ->
Info.strategy(query.resource, strategy_name)
{:ok, strategy} ->
{:ok, strategy}
end
end
end

View file

@ -13,8 +13,8 @@ defmodule AshAuthentication.Strategy.Password.SignInWithTokenPreparation do
@doc false
@impl true
@spec prepare(Query.t(), keyword, Preparation.context()) :: Query.t()
def prepare(query, _opts, _context) do
strategy = Info.strategy_for_action!(query.resource, query.action.name)
def prepare(query, options, context) do
{:ok, strategy} = find_strategy(query, context, options)
query
|> check_sign_in_token_configuration(strategy)
@ -156,4 +156,19 @@ defmodule AshAuthentication.Strategy.Password.SignInWithTokenPreparation do
defp extract_primary_keys_from_subject(_, _),
do: {:error, "The token does not contain a subject"}
defp find_strategy(query, context, options) do
with :error <- Info.strategy_for_action(query.resource, query.action.name),
:error <- Map.fetch(query.context, :strategy_name),
:error <- Map.fetch(context, :strategy_name),
:error <- Keyword.fetch(options, :strategy_name) do
:error
else
{:ok, strategy_name} when is_atom(strategy_name) ->
Info.strategy(query.resource, strategy_name)
{:ok, strategy} ->
{:ok, strategy}
end
end
end