From 5f0981815c01da21f2b148980bcdc5b267b336d7 Mon Sep 17 00:00:00 2001 From: Robin Date: Sat, 21 Sep 2024 14:27:09 +0200 Subject: [PATCH] Rename `otp` -> `totp` --- .../phx.gen.auth/context_functions.ex | 32 +++++++-------- priv/templates/phx.gen.auth/migration.ex | 2 +- priv/templates/phx.gen.auth/schema.ex | 12 +++--- .../phx.gen.auth/session_controller.ex | 4 +- .../phx.gen.auth/settings_controller.ex | 30 +++++++------- .../phx.gen.auth/settings_edit.html.heex | 12 +++--- priv/templates/phx.gen.auth/settings_live.ex | 40 +++++++++---------- .../templates/phx.gen.auth/totp_controller.ex | 4 +- 8 files changed, 68 insertions(+), 68 deletions(-) diff --git a/priv/templates/phx.gen.auth/context_functions.ex b/priv/templates/phx.gen.auth/context_functions.ex index 703a5cc3c9..6f49cccd52 100644 --- a/priv/templates/phx.gen.auth/context_functions.ex +++ b/priv/templates/phx.gen.auth/context_functions.ex @@ -349,22 +349,22 @@ We're also allowing codes to be 30 seconds in the past or future, to account for slightly mismatching times on different devices. """ - def valid_<%= schema.singular %>_otp?(<%= schema.singular %>_or_secret, validation_code, offset \\ 30, opts \\ []) + def valid_<%= schema.singular %>_totp?(<%= schema.singular %>_or_secret, validation_code, offset \\ 30, opts \\ []) - def valid_<%= schema.singular %>_otp?(%<%= inspect schema.alias %>{} = <%= schema.singular %>, validation_code, offset, opts) - when is_binary(validation_code) and is_binary(<%= schema.singular %>.otp_secret) do + def valid_<%= schema.singular %>_totp?(%<%= inspect schema.alias %>{} = <%= schema.singular %>, validation_code, offset, opts) + when is_binary(validation_code) and is_binary(<%= schema.singular %>.totp_secret) do opts = Keyword.put_new(opts, :since, <%= schema.singular %>.last_login) - valid_<%= schema.singular %>_otp?(<%= schema.singular %>.otp_secret, validation_code, offset, opts) + valid_<%= schema.singular %>_totp?(<%= schema.singular %>.totp_secret, validation_code, offset, opts) end - def valid_<%= schema.singular %>_otp?(otp_secret, validation_code, offset, opts) when is_binary(validation_code) do - {:ok, otp_secret} = Base.decode64(otp_secret) + def valid_<%= schema.singular %>_totp?(totp_secret, validation_code, offset, opts) when is_binary(validation_code) do + {:ok, totp_secret} = Base.decode64(totp_secret) Enum.any?([-offset, 0, offset], fn offset -> time = Keyword.get(opts, :time, System.os_time(:second)) opts = Keyword.put(opts, :time, time + offset) - NimbleTOTP.valid?(otp_secret, validation_code, opts) + NimbleTOTP.valid?(totp_secret, validation_code, opts) end) end @@ -373,12 +373,12 @@ ## Examples - iex> change_<%= schema.singular %>_otp(<%= schema.singular %>) + iex> change_<%= schema.singular %>_totp(<%= schema.singular %>) %Ecto.Changeset{data: %<%= inspect schema.alias %>{}} """ - def change_<%= schema.singular %>_otp(<%= schema.singular %>, attrs \\ %{}) do - <%= inspect schema.alias %>.otp_changeset(<%= schema.singular %>, attrs) + def change_<%= schema.singular %>_totp(<%= schema.singular %>, attrs \\ %{}) do + <%= inspect schema.alias %>.totp_changeset(<%= schema.singular %>, attrs) end @doc """ @@ -396,11 +396,11 @@ """ def enable_<%= schema.singular %>_2fa(<%= schema.singular %>, secret, code) do secret = Base.encode64(secret) - attrs = %{otp_secret: secret} + attrs = %{totp_secret: secret} <%= schema.singular %> - |> <%= inspect schema.alias %>.otp_changeset(attrs) - |> <%= inspect schema.alias %>.validate_otp(code) + |> <%= inspect schema.alias %>.totp_changeset(attrs) + |> <%= inspect schema.alias %>.validate_totp(code) |> <%= inspect schema.alias %>.login_changeset() |> Repo.update() end @@ -410,11 +410,11 @@ the OTP secret on the account. """ def disable_<%= schema.singular %>_2fa(<%= schema.singular %>, password, code) do - attrs = %{otp_secret: nil} + attrs = %{totp_secret: nil} <%= schema.singular %> - |> <%= inspect schema.alias %>.otp_changeset(attrs) - |> <%= inspect schema.alias %>.validate_otp(code, for: :<%= schema.singular %>) + |> <%= inspect schema.alias %>.totp_changeset(attrs) + |> <%= inspect schema.alias %>.validate_totp(code, for: :<%= schema.singular %>) |> <%= inspect schema.alias %>.validate_current_password(password) |> <%= inspect schema.alias %>.login_changeset() |> Repo.update() diff --git a/priv/templates/phx.gen.auth/migration.ex b/priv/templates/phx.gen.auth/migration.ex index d485285311..daad91c1d6 100644 --- a/priv/templates/phx.gen.auth/migration.ex +++ b/priv/templates/phx.gen.auth/migration.ex @@ -8,7 +8,7 @@ defmodule <%= inspect schema.repo %>.Migrations.Create<%= Macro.camelize(schema. <%= if schema.binary_id do %> add :id, :binary_id, primary_key: true <% end %> <%= migration.column_definitions[:email] %> add :hashed_password, :string, null: false - add :otp_secret, :string + add :totp_secret, :string add :last_login, :naive_datetime add :confirmed_at, <%= inspect schema.timestamp_type %> diff --git a/priv/templates/phx.gen.auth/schema.ex b/priv/templates/phx.gen.auth/schema.ex index f06b6dcadb..db5f05db7b 100644 --- a/priv/templates/phx.gen.auth/schema.ex +++ b/priv/templates/phx.gen.auth/schema.ex @@ -10,7 +10,7 @@ defmodule <%= inspect schema.module %> do field :password, :string, virtual: true, redact: true field :hashed_password, :string, redact: true field :current_password, :string, virtual: true, redact: true - field :otp_secret, :string, redact: true + field :totp_secret, EctoBase64, redact: true field :last_login, :naive_datetime field :confirmed_at, <%= inspect schema.timestamp_type %> @@ -96,8 +96,8 @@ defmodule <%= inspect schema.module %> do @doc """ A <%= schema.singular %> changeset for changing the OTP secret. """ - def otp_changeset(<%= schema.singular %>, attrs) do - cast(<%= schema.singular %>, attrs, [:otp_secret]) + def totp_changeset(<%= schema.singular %>, attrs) do + cast(<%= schema.singular %>, attrs, [:totp_secret]) end @doc """ @@ -119,14 +119,14 @@ defmodule <%= inspect schema.module %> do `:<%= schema.singular %>`, the code will be checked against the OTP secret on the <%= schema.singular %>. Defaults to `:given`. """ - def validate_otp(changeset, code, opts \\ []) do + def validate_totp(changeset, code, opts \\ []) do secret = case Keyword.get(opts, :for, :given) do - :given -> Map.fetch!(changeset.changes, :otp_secret) + :given -> Map.fetch!(changeset.changes, :totp_secret) :<%= schema.singular %> -> changeset.data end - if <%= inspect context.alias %>.valid_<%= schema.singular %>_otp?(secret, code) do + if <%= inspect context.alias %>.valid_<%= schema.singular %>_totp?(secret, code) do changeset else add_error(changeset, :code, "did not match") diff --git a/priv/templates/phx.gen.auth/session_controller.ex b/priv/templates/phx.gen.auth/session_controller.ex index d854ad93ed..3989171a10 100644 --- a/priv/templates/phx.gen.auth/session_controller.ex +++ b/priv/templates/phx.gen.auth/session_controller.ex @@ -24,7 +24,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web remember_me = Map.get(<%= schema.singular %>_params, "remember_me", false) case <%= inspect context.alias %>.get_<%= schema.singular %>_by_email_and_password(email, password) do - %<%= inspect schema.alias %>{otp_secret: nil} = <%= schema.singular %> -> + %<%= inspect schema.alias %>{totp_secret: nil} = <%= schema.singular %> -> conn |> put_flash(:info, info) |> <%= inspect schema.alias %>Auth.log_in_<%= schema.singular %>(<%= schema.singular %>, <%= schema.singular %>_params) @@ -53,7 +53,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web %{"email" => email, "password" => password} = <%= schema.singular %>_params case <%= inspect context.alias %>.get_<%= schema.singular %>_by_email_and_password(email, password) do - %<%= inspect schema.alias %>{otp_secret: nil} = <%= schema.singular %> -> + %<%= inspect schema.alias %>{totp_secret: nil} = <%= schema.singular %> -> conn |> put_flash(:info, "Welcome back!") |> <%= inspect schema.alias %>Auth.log_in_<%= schema.singular %>(<%= schema.singular %>, <%= schema.singular %>_params) diff --git a/priv/templates/phx.gen.auth/settings_controller.ex b/priv/templates/phx.gen.auth/settings_controller.ex index 75231fdbcd..93ff19cdfa 100644 --- a/priv/templates/phx.gen.auth/settings_controller.ex +++ b/priv/templates/phx.gen.auth/settings_controller.ex @@ -4,8 +4,8 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web alias <%= inspect context.module %> alias <%= inspect auth_module %> - plug :assign_email_otp_and_password_changesets - plug :assign_otp_secret_and_url + plug :assign_email_totp_and_password_changesets + plug :assign_totp_secret_and_url def edit(conn, _params) do render(conn, :edit) @@ -51,7 +51,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web end end - def update(conn, %{"action" => "enable_otp", "<%= schema.singular %>" => <%= schema.singular %>_params}) do + def update(conn, %{"action" => "enable_totp", "<%= schema.singular %>" => <%= schema.singular %>_params}) do %{"code" => code, "secret" => secret} = <%= schema.singular %>_params <%= schema.singular %> = conn.assigns.current_<%= schema.singular %> @@ -59,35 +59,35 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web case <%= inspect context.alias %>.enable_<%= schema.singular %>_2fa(<%= schema.singular %>, secret, code) do {:ok, <%= schema.singular %>} -> - changeset = <%= inspect context.alias %>.change_<%= schema.singular %>_otp(<%= schema.singular %>) + changeset = <%= inspect context.alias %>.change_<%= schema.singular %>_totp(<%= schema.singular %>) conn |> put_flash(:info, "2FA enabled successfully.") |> assign(:current_<%= schema.singular %>, <%= schema.singular %>) - |> assign(:otp_changeset, changeset) + |> assign(:totp_changeset, changeset) |> render(:edit) {:error, changeset} -> - render(conn, :edit, otp_changeset: changeset) + render(conn, :edit, totp_changeset: changeset) end end - def update(conn, %{"action" => "disable_otp", "<%= schema.singular %>" => <%= schema.singular %>_params}) do + def update(conn, %{"action" => "disable_totp", "<%= schema.singular %>" => <%= schema.singular %>_params}) do %{"code" => code, "current_password" => password} = <%= schema.singular %>_params <%= schema.singular %> = conn.assigns.current_<%= schema.singular %> case <%= inspect context.alias %>.disable_<%= schema.singular %>_2fa(<%= schema.singular %>, password, code) do {:ok, <%= schema.singular %>} -> - changeset = <%= inspect context.alias %>.change_<%= schema.singular %>_otp(<%= schema.singular %>) + changeset = <%= inspect context.alias %>.change_<%= schema.singular %>_totp(<%= schema.singular %>) conn |> put_flash(:info, "2FA disabled successfully.") |> assign(:current_<%= schema.singular %>, <%= schema.singular %>) - |> assign(:otp_changeset, changeset) + |> assign(:totp_changeset, changeset) |> render(:edit) {:error, changeset} -> - render(conn, :edit, otp_changeset: changeset) + render(conn, :edit, totp_changeset: changeset) end end @@ -105,24 +105,24 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web end end - defp assign_email_otp_and_password_changesets(conn, _opts) do + defp assign_email_totp_and_password_changesets(conn, _opts) do <%= schema.singular %> = conn.assigns.current_<%= schema.singular %> conn |> assign(:email_changeset, <%= inspect context.alias %>.change_<%= schema.singular %>_email(<%= schema.singular %>)) |> assign(:password_changeset, <%= inspect context.alias %>.change_<%= schema.singular %>_password(<%= schema.singular %>)) - |> assign(:otp_changeset, <%= inspect context.alias %>.change_<%= schema.singular %>_otp(<%= schema.singular %>)) + |> assign(:totp_changeset, <%= inspect context.alias %>.change_<%= schema.singular %>_totp(<%= schema.singular %>)) end - defp assign_otp_secret_and_url(conn, _opts) do + defp assign_totp_secret_and_url(conn, _opts) do <%= schema.singular %> = conn.assigns.current_<%= schema.singular %> - secret = <%= schema.singular %>.otp_secret || NimbleTOTP.secret() + secret = <%= schema.singular %>.totp_secret || NimbleTOTP.secret() encoded = Base.encode64(secret) url = NimbleTOTP.otpauth_uri("Dummy - #{<%= schema.singular %>.email}", secret, issuer: "Dummy") conn - |> assign(:otp_secret, encoded) + |> assign(:totp_secret, encoded) |> assign(:otp_url, url) end end diff --git a/priv/templates/phx.gen.auth/settings_edit.html.heex b/priv/templates/phx.gen.auth/settings_edit.html.heex index 4583497f4e..c8bc16317e 100644 --- a/priv/templates/phx.gen.auth/settings_edit.html.heex +++ b/priv/templates/phx.gen.auth/settings_edit.html.heex @@ -61,14 +61,14 @@
- <%%= if @current_<%= schema.singular %>.otp_secret do %> - <.simple_form :let={f} for={@otp_changeset} action={~p"<%= schema.route_prefix %>/settings"} id="otp_form"> + <%%= if @current_<%= schema.singular %>.totp_secret do %> + <.simple_form :let={f} for={@totp_changeset} action={~p"<%= schema.route_prefix %>/settings"} id="totp_form"> <.header class="text-center"> Turn off verification in two steps <:subtitle>Enter the code provided by your 2FA app - + <.input field={f[:code]} type="text" maxlength="6" label="Code" required /> <.input field={f[:current_password]} type="password" label="Current password" required /> @@ -78,7 +78,7 @@ <%% else %> - <.simple_form :let={f} for={@otp_changeset} action={~p"<%= schema.route_prefix %>/settings"} id="otp_form"> + <.simple_form :let={f} for={@totp_changeset} action={~p"<%= schema.route_prefix %>/settings"} id="totp_form"> <.header class="text-center"> Turn on verification in two steps <:subtitle>Scan the QR code below with your favorite 2FA app @@ -91,8 +91,8 @@ |> raw() %>
- - + + <.input field={f[:code]} diff --git a/priv/templates/phx.gen.auth/settings_live.ex b/priv/templates/phx.gen.auth/settings_live.ex index 1d1e6ab3cb..3af2f10314 100644 --- a/priv/templates/phx.gen.auth/settings_live.ex +++ b/priv/templates/phx.gen.auth/settings_live.ex @@ -70,17 +70,17 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
- <%%= if @current_<%= schema.singular %>.otp_secret do %> - <.simple_form for={@otp_form} id="otp_form" phx-submit="disable_otp"> + <%%= if @current_<%= schema.singular %>.totp_secret do %> + <.simple_form for={@totp_form} id="totp_form" phx-submit="disable_totp"> <.header class="text-center"> Turn off verification in two steps <:subtitle>Enter the code provided by your 2FA app - <.input field={@otp_form[:code]} type="text" maxlength="6" label="Code" required /> + <.input field={@totp_form[:code]} type="text" maxlength="6" label="Code" required /> <.input - field={@otp_form[:current_password]} + field={@totp_form[:current_password]} type="password" label="Current password" required @@ -91,7 +91,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web <%% else %> - <.simple_form for={@otp_form} id="otp_form" phx-submit="enable_otp"> + <.simple_form for={@totp_form} id="totp_form" phx-submit="enable_totp"> <.header class="text-center"> Turn on verification in two steps <:subtitle>Scan the QR code below with your favorite 2FA app @@ -105,7 +105,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
<.input - field={@otp_form[:code]} + field={@totp_form[:code]} type="text" maxlength="6" label="Enter the code provided by your 2FA app" @@ -139,10 +139,10 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web <%= schema.singular %> = socket.assigns.current_<%= schema.singular %> email_changeset = <%= inspect context.alias %>.change_<%= schema.singular %>_email(<%= schema.singular %>) password_changeset = <%= inspect context.alias %>.change_<%= schema.singular %>_password(<%= schema.singular %>) - otp_changeset = <%= inspect context.alias %>.change_<%= schema.singular %>_otp(<%= schema.singular %>) + totp_changeset = <%= inspect context.alias %>.change_<%= schema.singular %>_totp(<%= schema.singular %>) - otp_secret = <%= schema.singular %>.otp_secret || NimbleTOTP.secret() - otp_url = NimbleTOTP.otpauth_uri("Dummy - #{<%= schema.singular %>.email}", otp_secret, issuer: "Dummy") + totp_secret = <%= schema.singular %>.totp_secret || NimbleTOTP.secret() + otp_url = NimbleTOTP.otpauth_uri("Dummy - #{<%= schema.singular %>.email}", totp_secret, issuer: "Dummy") socket = socket @@ -151,9 +151,9 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web |> assign(:current_email, <%= schema.singular %>.email) |> assign(:email_form, to_form(email_changeset)) |> assign(:password_form, to_form(password_changeset)) - |> assign(:otp_form, to_form(otp_changeset)) + |> assign(:totp_form, to_form(totp_changeset)) |> assign(:trigger_submit, false) - |> assign(:otp_secret, otp_secret) + |> assign(:totp_secret, totp_secret) |> assign(:otp_url, otp_url) {:ok, socket} @@ -221,44 +221,44 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web end end - def handle_event("enable_otp", %{"<%= schema.singular %>" => <%= schema.singular %>_params}, socket) do + def handle_event("enable_totp", %{"<%= schema.singular %>" => <%= schema.singular %>_params}, socket) do %{"code" => code} = <%= schema.singular %>_params <%= schema.singular %> = socket.assigns.current_<%= schema.singular %> - secret = socket.assigns.otp_secret + secret = socket.assigns.totp_secret case <%= inspect context.alias %>.enable_<%= schema.singular %>_2fa(<%= schema.singular %>, secret, code) do {:ok, <%= schema.singular %>} -> info = "2FA enabled successfully." - changeset = <%= inspect context.alias %>.change_<%= schema.singular %>_otp(<%= schema.singular %>) + changeset = <%= inspect context.alias %>.change_<%= schema.singular %>_totp(<%= schema.singular %>) {:noreply, socket |> put_flash(:info, info) |> assign(:current_<%= schema.singular %>, <%= schema.singular %>) - |> assign(:otp_form, to_form(changeset))} + |> assign(:totp_form, to_form(changeset))} {:error, changeset} -> - {:noreply, assign(socket, :otp_form, to_form(Map.put(changeset, :action, :insert)))} + {:noreply, assign(socket, :totp_form, to_form(Map.put(changeset, :action, :insert)))} end end - def handle_event("disable_otp", %{"<%= schema.singular %>" => <%= schema.singular %>_params}, socket) do + def handle_event("disable_totp", %{"<%= schema.singular %>" => <%= schema.singular %>_params}, socket) do %{"code" => code, "current_password" => password} = <%= schema.singular %>_params <%= schema.singular %> = socket.assigns.current_<%= schema.singular %> case <%= inspect context.alias %>.disable_<%= schema.singular %>_2fa(<%= schema.singular %>, password, code) do {:ok, <%= schema.singular %>} -> info = "2FA disabled successfully." - changeset = <%= inspect context.alias %>.change_<%= schema.singular %>_otp(<%= schema.singular %>) + changeset = <%= inspect context.alias %>.change_<%= schema.singular %>_totp(<%= schema.singular %>) {:noreply, socket |> put_flash(:info, info) |> assign(:current_<%= schema.singular %>, <%= schema.singular %>) - |> assign(:otp_form, to_form(changeset))} + |> assign(:totp_form, to_form(changeset))} {:error, changeset} -> - {:noreply, assign(socket, :otp_form, to_form(Map.put(changeset, :action, :insert)))} + {:noreply, assign(socket, :totp_form, to_form(Map.put(changeset, :action, :insert)))} end end end diff --git a/priv/templates/phx.gen.auth/totp_controller.ex b/priv/templates/phx.gen.auth/totp_controller.ex index 11934f4984..d7d08654cc 100644 --- a/priv/templates/phx.gen.auth/totp_controller.ex +++ b/priv/templates/phx.gen.auth/totp_controller.ex @@ -9,7 +9,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web %{"code" => code} = <%= schema.singular %>_params <%= schema.singular %> = conn.assigns.unauthenticated_<%= schema.singular %> - if <%= schema.singular %> && <%= inspect context.module %>.valid_<%= schema.singular %>_otp?(<%= schema.singular %>, code) do + if <%= schema.singular %> && <%= inspect context.module %>.valid_<%= schema.singular %>_totp?(<%= schema.singular %>, code) do conn |> put_flash(:info, "Welcome back!") |> <%= inspect auth_module %>.log_in_<%= schema.singular %>(<%= schema.singular %>, <%= schema.singular %>_params) @@ -32,7 +32,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web %{"code" => code} = <%= schema.singular %>_params <%= schema.singular %> = conn.assigns.unauthenticated_<%= schema.singular %> - if <%= schema.singular %> && <%= inspect context.module %>.valid_<%= schema.singular %>_otp?(<%= schema.singular %>, code) do + if <%= schema.singular %> && <%= inspect context.module %>.valid_<%= schema.singular %>_totp?(<%= schema.singular %>, code) do conn |> put_flash(:info, "Welcome back!") |> <%= inspect auth_module %>.log_in_<%= schema.singular %>(<%= schema.singular %>, <%= schema.singular %>_params)