Skip to content

Commit

Permalink
improvement: validate that tokens are enabled when password resets ar…
Browse files Browse the repository at this point in the history
…e enabled. (#758)

Closes #232.
  • Loading branch information
jimsynz authored Aug 5, 2024
1 parent 9b9f11c commit a8d98ac
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion lib/ash_authentication/strategies/password/verifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ defmodule AshAuthentication.Strategy.Password.Verifier do
@spec verify(Password.t(), map) :: :ok | {:error, Exception.t()}
def verify(strategy, dsl_state) do
with :ok <- validate_behaviour(strategy.hash_provider, HashProvider),
:ok <- validate_tokens_enabled_for_sign_in_tokens(dsl_state, strategy) do
:ok <- validate_tokens_enabled_for_sign_in_tokens(dsl_state, strategy),
:ok <- validate_tokens_enabled_for_resettable(dsl_state, strategy) do
maybe_validate_resettable_sender(dsl_state, strategy)
end
end
Expand Down Expand Up @@ -69,6 +70,41 @@ defmodule AshAuthentication.Strategy.Password.Verifier do

defp validate_tokens_enabled_for_sign_in_tokens(_, _), do: :ok

defp validate_tokens_enabled_for_resettable(dsl_state, %{resettable: resettable, name: name})
when is_struct(resettable) do
resource = Verifier.get_persisted(dsl_state, :module)

if Info.authentication_tokens_enabled?(dsl_state) do
:ok
else
{:error,
DslError.exception(
module: resource,
path: [
:authentication,
:strategies,
:password,
name,
:resettable
],
message: """
The `resettable` option requires that tokens are enabled for your resource. For example:
authentication do
...
tokens do
enabled? true
end
end
"""
)}
end
end

defp validate_tokens_enabled_for_resettable(_, _), do: :ok

defp maybe_validate_resettable_sender(dsl_state, %{resettable: resettable})
when is_struct(resettable) do
with {:ok, {sender, _opts}} <- Map.fetch(resettable, :sender),
Expand Down

0 comments on commit a8d98ac

Please sign in to comment.