ash_authentication/lib/ash_authentication/secret_function.ex
James Harton 1c8f138c67
improvement!: Major redesign of DSL and code structure. (#35)
Highlights:

* Replaced `AshAuthentication.Provider` with the much more flexible `AshAuthentication.Strategy`.
* Moved strategies to within the `authentication` DSL using entities and removed excess extensions.
* Added a lot more documentation and test coverage.
2022-11-23 09:09:41 +13:00

28 lines
856 B
Elixir

defmodule AshAuthentication.SecretFunction do
@moduledoc """
Implements `AshAuthentication.Secret` for functions that are provided to the
DSL instead of modules.
"""
use AshAuthentication.Secret
alias Ash.Resource
@doc false
@impl true
@spec secret_for(secret_name :: [atom], Resource.t(), keyword) :: {:ok, String.t()} | :error
def secret_for(secret_name, resource, opts) do
case Keyword.pop(opts, :fun) do
{fun, _opts} when is_function(fun, 2) ->
fun.(secret_name, resource)
{fun, opts} when is_function(fun, 3) ->
fun.(secret_name, resource, opts)
{{m, f, a}, _opts} when is_atom(m) and is_atom(f) and is_list(a) ->
apply(m, f, [secret_name, resource | a])
{nil, opts} ->
raise "Invalid options given to `secret_for/3` callback: `#{inspect(opts)}`."
end
end
end