-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: add
mix ash_authentication.add_strategy password
- Loading branch information
1 parent
56d6e65
commit cbe21cc
Showing
10 changed files
with
573 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
lib/ash_authentication/strategies/password/request_password_reset.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
defmodule AshAuthentication.Strategy.Password.RequestPasswordReset do | ||
@moduledoc """ | ||
Requests a password reset. | ||
This implementation performs three jobs: | ||
1. looks up the user with the given action and field | ||
2. if a matching user is found: | ||
a. a reset token is generated | ||
b. and the password reset sender is invoked | ||
""" | ||
use Ash.Resource.Actions.Implementation | ||
alias Ash.{Query, Resource.Actions.Implementation} | ||
alias AshAuthentication.{Info, Strategy.Password} | ||
require Ash.Query | ||
require Logger | ||
|
||
@doc false | ||
@impl true | ||
@spec run(Ash.ActionInput.t(), keyword, context :: Implementation.Context) :: Query.t() | ||
Check warning on line 19 in lib/ash_authentication/strategies/password/request_password_reset.ex GitHub Actions / mix dialyzercallback_spec_arg_type_mismatch
Check warning on line 19 in lib/ash_authentication/strategies/password/request_password_reset.ex GitHub Actions / mix dialyzercallback_spec_type_mismatch
|
||
def run(action_input, opts, context) do | ||
read_action = opts[:action] | ||
|
||
strategy = Info.strategy_for_action!(action_input.resource, action_input.action.name) | ||
|
||
if strategy.resettable && strategy.resettable.sender do | ||
identity_field = strategy.identity_field | ||
identity = Ash.ActionInput.get_argument(action_input, identity_field) | ||
select_for_senders = Info.authentication_select_for_senders!(action_input.resource) | ||
{sender, send_opts} = strategy.resettable.sender | ||
|
||
context = | ||
if context[:private][:ash_authentication?] do | ||
%{private: %{ash_authentication?: true}} | ||
else | ||
%{} | ||
end | ||
|
||
query_result = | ||
action_input.resource | ||
|> Ash.Query.new() | ||
|> Ash.Query.set_context(context) | ||
|> Ash.Query.for_read(read_action, %{ | ||
identity_field => identity | ||
}) | ||
|> Ash.Query.ensure_selected(select_for_senders) | ||
|> Ash.read_one() | ||
|
||
with {:ok, user} when not is_nil(user) <- query_result, | ||
{:ok, token} <- Password.reset_token_for(strategy, user) do | ||
sender.send(user, token, send_opts) | ||
else | ||
{:ok, nil} -> | ||
:ok | ||
|
||
{:error, error} -> | ||
Logger.warning( | ||
""" | ||
Something went wrong resetting password for #{inspect(action_input.resource)} `#{identity}` | ||
#{Exception.format(:error, error)} | ||
""" | ||
) | ||
|
||
:ok | ||
end | ||
else | ||
:ok | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.