Packages
ash_authentication_phoenix
3.0.0-rc.2
3.0.0-rc.9
3.0.0-rc.8
3.0.0-rc.7
3.0.0-rc.6
3.0.0-rc.4
3.0.0-rc.3
3.0.0-rc.2
3.0.0-rc.1
3.0.0-rc.0
2.17.2
2.17.1
2.17.0
2.16.0
2.15.0
2.14.1
2.14.0
2.13.1
2.13.0
2.12.2
2.12.1
2.12.0
2.11.0
2.10.5
2.10.4
2.10.3
2.10.2
2.10.1
2.10.0
2.9.0
2.8.0
2.7.0
2.6.3
2.6.2
2.6.1
2.6.0
2.5.4
2.5.3
2.5.2
2.5.1
2.5.0
2.4.8
2.4.7
2.4.6
2.4.5
2.4.4
2.4.3
2.4.2
2.4.1
2.4.0
2.3.0
2.2.1
2.2.0
2.1.11
2.1.10
2.1.9
2.1.8
2.1.7
2.1.6
2.1.5
2.1.4
2.1.3
2.1.2
2.1.1
2.1.0
2.0.2
2.0.1
2.0.0
2.0.0-rc.2
2.0.0-rc.1
2.0.0-rc.0
1.9.4
1.9.3
1.9.2
1.9.1
1.9.0
1.8.7
1.8.6
1.8.5
1.8.4
1.8.3
1.8.2
1.8.1
1.8.0
1.7.3
1.7.2
1.7.1
1.7.0
1.6.6
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.1
1.5.0
1.4.8
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.1
1.3.0
1.2.0
1.1.0
1.0.1
1.0.0
Phoenix integration for Ash Authentication
Current section
Files
Jump to
Current section
Files
lib/mix/tasks/ash_authentication_phoenix.add_strategy.ex
# SPDX-FileCopyrightText: 2022 Alembic Pty Ltd
#
# SPDX-License-Identifier: MIT
# credo:disable-for-this-file Credo.Check.Design.AliasUsage
if Code.ensure_loaded?(Igniter) do
defmodule Mix.Tasks.AshAuthenticationPhoenix.AddStrategy do
use Igniter.Mix.Task
@example "mix ash_authentication_phoenix.add_strategy password"
@shortdoc "Adds a strategy to your user resource with Phoenix integration"
@aa_strategy_tasks %{
"password" => "ash_authentication.add_strategy.password",
"magic_link" => "ash_authentication.add_strategy.magic_link",
"api_key" => "ash_authentication.add_strategy.api_key",
"totp" => "ash_authentication.add_strategy.totp",
"recovery_code" => "ash_authentication.add_strategy.recovery_code"
}
@aap_strategy_tasks %{
"password" => "ash_authentication_phoenix.add_strategy.password",
"magic_link" => "ash_authentication_phoenix.add_strategy.magic_link",
"totp" => "ash_authentication_phoenix.add_strategy.totp",
"recovery_code" => "ash_authentication_phoenix.add_strategy.recovery_code"
}
@strategy_names Map.keys(@aa_strategy_tasks)
@strategy_explanation [
password:
"Register and sign in with a username/email and a password.",
magic_link:
"Register and sign in with a magic link, sent via email to the user.",
api_key: "Sign in with an API key.",
totp: "Authenticate with a time-based one-time password (TOTP).",
recovery_code:
"Authenticate with one-time recovery codes as a 2FA fallback."
]
|> Enum.map_join("\n", fn {name, description} ->
" * `#{name}` - #{description}"
end)
@moduledoc """
#{@shortdoc}
This task composes both the ash_authentication resource-level task and the
ash_authentication_phoenix Phoenix integration task for each strategy.
The following strategies are available:
#{@strategy_explanation}
## Example
```bash
#{@example}
```
## Options
* `--user`, `-u` - The user resource. Defaults to `YourApp.Accounts.User`
* `--identity-field`, `-i` - The identity field. Defaults to `email`
## Password options
- `--hash-provider` - The hash provider to use, either `bcrypt` or `argon2`. Defaults to `bcrypt`.
## TOTP options
- `--mode`, `-m` - Either `primary` or `2fa`. Defaults to `2fa`.
- `--name`, `-n` - The name of the TOTP strategy. Defaults to `totp`.
"""
def info(_argv, _composing_task) do
%Igniter.Mix.Task.Info{
group: :ash,
example: @example,
extra_args?: false,
only: nil,
positional: [
strategies: [rest: true]
],
composes: Map.values(@aa_strategy_tasks) ++ Map.values(@aap_strategy_tasks),
schema: [
accounts: :string,
user: :string,
identity_field: :string,
hash_provider: :string,
mode: :string,
name: :string
],
aliases: [
a: :accounts,
u: :user,
i: :identity_field,
m: :mode,
n: :name
],
defaults: [
identity_field: "email"
]
}
end
def igniter(igniter) do
strategies = igniter.args.positional[:strategies] || []
invalid_strategy = Enum.find(strategies, &(&1 not in @strategy_names))
if invalid_strategy do
Mix.shell().error("""
Invalid strategy provided: `#{invalid_strategy}`
Available Strategies:
#{@strategy_explanation}
""")
exit({:shutdown, 1})
end
argv = igniter.args.argv_flags
Enum.reduce(strategies, igniter, fn strategy, igniter ->
igniter
|> maybe_compose(strategy, @aa_strategy_tasks, argv)
|> maybe_compose(strategy, @aap_strategy_tasks, argv)
end)
end
defp maybe_compose(igniter, strategy, task_map, argv) do
case Map.fetch(task_map, strategy) do
{:ok, task} -> Igniter.compose_task(igniter, task, argv)
:error -> igniter
end
end
end
else
defmodule Mix.Tasks.AshAuthenticationPhoenix.AddStrategy do
@shortdoc "Adds a strategy to your user resource with Phoenix integration"
@moduledoc @shortdoc
use Mix.Task
def run(_argv) do
Mix.shell().error("""
The task 'ash_authentication_phoenix.add_strategy' requires igniter to be run.
Please install igniter and try again.
For more information, see: https://hexdocs.pm/igniter
""")
exit({:shutdown, 1})
end
end
end