mirror of
https://github.com/team-alembic/ash_authentication.git
synced 2024-09-19 21:03:23 +12:00
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:
parent
38a1f29c6e
commit
39d25cadea
2 changed files with 34 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue