Skip to content

Commit

Permalink
Rename otp -> totp
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinBoers committed Sep 21, 2024
1 parent c7f050a commit a006ea4
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 50 deletions.
26 changes: 13 additions & 13 deletions priv/templates/phx.gen.auth/context_functions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -373,11 +373,11 @@
## 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
def change_<%= schema.singular %>_totp(<%= schema.singular %>, attrs \\ %{}) do
<%= inspect schema.alias %>.otp_changeset(<%= schema.singular %>, attrs)
end

Expand All @@ -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 %>.validate_totp(code)
|> <%= inspect schema.alias %>.login_changeset()
|> Repo.update()
end
Expand All @@ -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 %>.validate_totp(code, for: :<%= schema.singular %>)
|> <%= inspect schema.alias %>.validate_current_password(password)
|> <%= inspect schema.alias %>.login_changeset()
|> Repo.update()
Expand Down
2 changes: 1 addition & 1 deletion priv/templates/phx.gen.auth/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>

Expand Down
10 changes: 5 additions & 5 deletions priv/templates/phx.gen.auth/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>

Expand Down Expand Up @@ -97,7 +97,7 @@ defmodule <%= inspect schema.module %> do
A <%= schema.singular %> changeset for changing the OTP secret.
"""
def otp_changeset(<%= schema.singular %>, attrs) do
cast(<%= schema.singular %>, attrs, [:otp_secret])
cast(<%= schema.singular %>, attrs, [:totp_secret])
end

@doc """
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions priv/templates/phx.gen.auth/session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions priv/templates/phx.gen.auth/settings_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -51,15 +51,15 @@ 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 %>

{:ok, secret} = Base.decode64(secret)

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.")
Expand All @@ -72,13 +72,13 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
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.")
Expand All @@ -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(:otp_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
8 changes: 4 additions & 4 deletions priv/templates/phx.gen.auth/settings_edit.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@
</.simple_form>
</div>
<div>
<%%= if @current_<%= schema.singular %>.otp_secret do %>
<%%= if @current_<%= schema.singular %>.totp_secret do %>
<.simple_form :let={f} for={@otp_changeset} action={~p"<%= schema.route_prefix %>/settings"} id="otp_form">
<.header class="text-center">
Turn off verification in two steps
<:subtitle>Enter the code provided by your 2FA app</:subtitle>
</.header>

<input name="action" type="hidden" value="disable_otp" />
<input name="action" type="hidden" value="disable_totp" />

<.input field={f[:code]} type="text" maxlength="6" label="Code" required />
<.input field={f[:current_password]} type="password" label="Current password" required />
Expand All @@ -91,8 +91,8 @@
|> raw() %>
</div>

<input name="action" type="hidden" value="enable_otp" />
<input name={f[:secret].name} type="hidden" value={@otp_secret} />
<input name="action" type="hidden" value="enable_totp" />
<input name={f[:secret].name} type="hidden" value={@totp_secret} />

<.input
field={f[:code]}
Expand Down
24 changes: 12 additions & 12 deletions priv/templates/phx.gen.auth/settings_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
</.simple_form>
</div>
<div>
<%%= 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={@otp_form} id="otp_form" phx-submit="disable_totp">
<.header class="text-center">
Turn off verification in two steps
<:subtitle>Enter the code provided by your 2FA app</:subtitle>
Expand All @@ -91,7 +91,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
</:actions>
</.simple_form>
<%% else %>
<.simple_form for={@otp_form} id="otp_form" phx-submit="enable_otp">
<.simple_form for={@otp_form} id="otp_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</:subtitle>
Expand Down Expand Up @@ -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 %>)
otp_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
Expand All @@ -153,7 +153,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
|> assign(:password_form, to_form(password_changeset))
|> assign(:otp_form, to_form(otp_changeset))
|> assign(:trigger_submit, false)
|> assign(:otp_secret, otp_secret)
|> assign(:totp_secret, totp_secret)
|> assign(:otp_url, otp_url)
{:ok, socket}
Expand Down Expand Up @@ -221,15 +221,15 @@ 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
Expand All @@ -242,14 +242,14 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
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
Expand Down
4 changes: 2 additions & 2 deletions priv/templates/phx.gen.auth/totp_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit a006ea4

Please sign in to comment.