diff --git a/CHANGELOG.md b/CHANGELOG.md index b8fe6c2b6..ad7e43877 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file. - Add last 24h plots to /sites view - Add site pinning to /sites view - Add support for JSON logger, via LOG_FORMAT=json environment variable +- Add support for 2FA authentication ### Removed - Removed the nested custom event property breakdown UI when filtering by a goal in Goal Conversions diff --git a/lib/plausible/auth/totp.ex b/lib/plausible/auth/totp.ex index a5cae9198..5854edb2f 100644 --- a/lib/plausible/auth/totp.ex +++ b/lib/plausible/auth/totp.ex @@ -18,19 +18,17 @@ defmodule Plausible.Auth.TOTP do After initiation, user is expected to confirm valid setup with `enable/2`, providing TOTP code from their authenticator app. After code validation passes successfully, the `User.totp_enabled` flag is set to `true`. - Finally, the user must be immediately presented with a list of recovery codes - generated with `generate_recovery_codes/1`. The codes should be presented + returned by the same call of `enable/2`. The codes should be presented in copy/paste friendly form, ideally also with a print-friendly view option. - The function can be run more than once, giving the user ability to regenerate - codes from the final stage of setup if needed. The `initiate/1` and `enable/1` functions can be safely called multiple times, allowing user to abort and restart setup up to these stages. ## Management - State of TOTP for a particular user can be chcecked by calling `enabled?/1`. + The state of TOTP for a particular user can be chcecked by calling + `enabled?/1` or `initiated?/1`. TOTP can be disabled with `disable/2`. User is expected to provide their current password for safety. Once disabled, all TOTP user settings are @@ -38,9 +36,9 @@ defmodule Plausible.Auth.TOTP do can be safely run more than once. If the user needs to regenerate the recovery codes outside of setup procedure, - they must do it via `generate_recovery_codes_protected/2`, providing - their current password for safety. They must be warned that any existing - recovery codes will be invalidated. + they must do it via `generate_recovery_codes/2`, providing their current + password for safety. They must be warned that any existing recovery codes + will be invalidated. ## Validation @@ -65,6 +63,18 @@ defmodule Plausible.Auth.TOTP do In case of recovery codes, each code is deleted immediately after use. They are strictly one-time use only. + ## TOTP Token + + TOTP token is an alternate method of authenticating user session. + It's main use case is "trust this device" functionality, where user + can decide to skip 2FA verification for a particular browser session + for next N days. The token should then be stored in an encrypted, + signed cookie with a proper expiration timestamp. + + The token should be reset each time it either fails to match + or when other credentials (like password) are reset. This should + effectively invalidate all trusted devices for a given user. + """ import Ecto.Changeset, only: [change: 2] @@ -106,14 +116,15 @@ defmodule Plausible.Auth.TOTP do user |> change( totp_enabled: false, - totp_secret: secret + totp_secret: secret, + totp_token: nil ) |> Repo.update!() {:ok, user, %{totp_uri: totp_uri(user), secret: readable_secret(user)}} end - @spec enable(Auth.User.t(), String.t(), Keyword.t()) :: + @spec enable(Auth.User.t(), String.t() | :skip_verify, Keyword.t()) :: {:ok, Auth.User.t(), %{recovery_codes: [String.t()]}} | {:error, :invalid_code | :not_initiated} def enable(user, code, opts \\ []) @@ -122,26 +133,37 @@ defmodule Plausible.Auth.TOTP do {:error, :not_initiated} end + def enable(user, :skip_verify, _opts) do + do_enable(user) + end + def enable(user, code, opts) do with {:ok, user} <- do_validate_code(user, code, opts) do - {:ok, {user, recovery_codes}} = - Repo.transaction(fn -> - user = - user - |> change(totp_enabled: true) - |> Repo.update!() + do_enable(user) + end + end + + defp do_enable(user) do + {:ok, {user, recovery_codes}} = + Repo.transaction(fn -> + user = + user + |> change( + totp_enabled: true, + totp_token: generate_token() + ) + |> Repo.update!() - {:ok, recovery_codes} = do_generate_recovery_codes(user) + {:ok, recovery_codes} = do_generate_recovery_codes(user) - {user, recovery_codes} - end) + {user, recovery_codes} + end) - user - |> Email.two_factor_enabled_email() - |> Plausible.Mailer.send() + user + |> Email.two_factor_enabled_email() + |> Plausible.Mailer.send() - {:ok, user, %{recovery_codes: recovery_codes}} - end + {:ok, user, %{recovery_codes: recovery_codes}} end @spec disable(Auth.User.t(), String.t()) :: {:ok, Auth.User.t()} | {:error, :invalid_password} @@ -157,6 +179,7 @@ defmodule Plausible.Auth.TOTP do user |> change( totp_enabled: false, + totp_token: nil, totp_secret: nil, totp_last_used_at: nil ) @@ -173,6 +196,18 @@ defmodule Plausible.Auth.TOTP do end end + @spec reset_token(Auth.User.t()) :: Auth.User.t() + def reset_token(user) do + new_token = + if user.totp_enabled do + generate_token() + end + + user + |> change(totp_token: new_token) + |> Repo.update!() + end + @spec generate_recovery_codes(Auth.User.t(), String.t()) :: {:ok, [String.t()]} | {:error, :invalid_password | :not_enabled} def generate_recovery_codes(%{totp_enabled: false}) do @@ -303,4 +338,10 @@ defmodule Plausible.Auth.TOTP do |> change(totp_last_used_at: now) |> Repo.update!() end + + defp generate_token() do + 20 + |> :crypto.strong_rand_bytes() + |> Base.encode64(padding: false) + end end diff --git a/lib/plausible/auth/user.ex b/lib/plausible/auth/user.ex index d34431dd2..b0ecc5b1a 100644 --- a/lib/plausible/auth/user.ex +++ b/lib/plausible/auth/user.ex @@ -38,6 +38,7 @@ defmodule Plausible.Auth.User do # Fields for TOTP authentication. See `Plausible.Auth.TOTP`. field :totp_enabled, :boolean, default: false field :totp_secret, Plausible.Auth.TOTP.EncryptedBinary + field :totp_token, :string field :totp_last_used_at, :naive_datetime embeds_one :grace_period, Plausible.Auth.GracePeriod, on_replace: :update diff --git a/lib/plausible_web/components/generic.ex b/lib/plausible_web/components/generic.ex index ffeb3fcd8..e1b35f468 100644 --- a/lib/plausible_web/components/generic.ex +++ b/lib/plausible_web/components/generic.ex @@ -32,7 +32,7 @@ defmodule PlausibleWeb.Components.Generic do type={@type} disabled={@disabled} class={[ - "inline-flex items-center justify-center gap-x-2 rounded-md bg-indigo-600 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-700 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 disabled:bg-gray-400", + "inline-flex items-center justify-center gap-x-2 rounded-md bg-indigo-600 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-700 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 disabled:bg-gray-400 dark:disabled:text-white dark:disabled:text-gray-400 dark:disabled:bg-gray-700", @class ]} {@rest} @@ -53,7 +53,7 @@ defmodule PlausibleWeb.Components.Generic do <.link href={@href} class={[ - "inline-flex items-center justify-center gap-x-2 rounded-md bg-indigo-600 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-700 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 disabled:bg-gray-400", + "inline-flex items-center justify-center gap-x-2 rounded-md bg-indigo-600 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-700 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 disabled:bg-gray-400 dark:disabled:bg-gray-800", @class ]} {@rest} @@ -248,6 +248,28 @@ defmodule PlausibleWeb.Components.Generic do end end + attr :class, :any, default: "" + + def spinner(assigns) do + ~H""" + + + + + + + """ + end + defp icon_class(link_assigns) do if String.contains?(link_assigns[:class], "text-sm") do ["w-3 h-3"] diff --git a/lib/plausible_web/components/two_factor.ex b/lib/plausible_web/components/two_factor.ex new file mode 100644 index 000000000..2233ddd5a --- /dev/null +++ b/lib/plausible_web/components/two_factor.ex @@ -0,0 +1,155 @@ +defmodule PlausibleWeb.Components.TwoFactor do + @moduledoc """ + Reusable components specific to 2FA + """ + use Phoenix.Component + + attr :text, :string, required: true + attr :scale, :integer, default: 4 + + def qr_code(assigns) do + qr_code = + assigns.text + |> EQRCode.encode() + |> EQRCode.svg(%{width: 160}) + + assigns = assign(assigns, :code, qr_code) + + ~H""" + <%= Phoenix.HTML.raw(@code) %> + """ + end + + attr :id, :string, default: "verify-button" + attr :form, :any, required: true + attr :field, :any, required: true + attr :class, :string, default: "" + + def verify_2fa_input(assigns) do + ~H""" +
+ <%= Phoenix.HTML.Form.text_input(@form, @field, + autocomplete: "off", + class: + "font-mono tracking-[0.5em] w-36 pl-5 font-medium shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block border-gray-300 dark:border-gray-500 dark:text-gray-200 dark:bg-gray-900 rounded-l-md", + oninput: + "this.value=this.value.replace(/[^0-9]/g, ''); if (this.value.length >= 6) document.getElementById('verify-button').focus()", + onclick: "this.select();", + oninvalid: "document.getElementById('verify-button').disabled = false", + maxlength: "6", + placeholder: "••••••", + value: "", + required: "required" + ) %> + + + Verify → + + + + + Verifying... + + +
+ """ + end + + attr :id, :string, required: true + attr :state_param, :string, required: true + attr :form_data, :any, required: true + attr :form_target, :string, required: true + attr :onsubmit, :string, default: nil + attr :title, :string, required: true + + slot :icon, required: true + slot :inner_block, required: true + slot :buttons, required: true + + def modal(assigns) do + ~H""" + + """ + end +end diff --git a/lib/plausible_web/controllers/auth_controller.ex b/lib/plausible_web/controllers/auth_controller.ex index a297004d6..23f4d6e85 100644 --- a/lib/plausible_web/controllers/auth_controller.ex +++ b/lib/plausible_web/controllers/auth_controller.ex @@ -1,8 +1,11 @@ defmodule PlausibleWeb.AuthController do use PlausibleWeb, :controller use Plausible.Repo + alias Plausible.Auth alias Plausible.Billing.Quota + alias PlausibleWeb.TwoFactor + require Logger plug( @@ -11,7 +14,11 @@ defmodule PlausibleWeb.AuthController do :register, :register_from_invitation, :login_form, - :login + :login, + :verify_2fa_form, + :verify_2fa, + :verify_2fa_recovery_code_form, + :verify_2fa_recovery_code ] ) @@ -28,10 +35,29 @@ defmodule PlausibleWeb.AuthController do :delete_me, :activate_form, :activate, - :request_activation_code + :request_activation_code, + :initiate_2fa, + :verify_2fa_setup_form, + :verify_2fa_setup, + :disable_2fa, + :generate_2fa_recovery_codes ] ) + plug( + :clear_2fa_user + when action not in [ + :verify_2fa_form, + :verify_2fa, + :verify_2fa_recovery_code_form, + :verify_2fa_recovery_code + ] + ) + + defp clear_2fa_user(conn, _opts) do + TwoFactor.Session.clear_2fa_user(conn) + end + def register(conn, %{"user" => %{"email" => email, "password" => password}}) do with {:ok, user} <- login_user(conn, email, password) do conn = set_user_session(conn, user) @@ -198,12 +224,13 @@ defmodule PlausibleWeb.AuthController do def login(conn, %{"email" => email, "password" => password}) do with {:ok, user} <- login_user(conn, email, password) do - login_dest = get_session(conn, :login_dest) || Routes.site_path(conn, :index) - - conn - |> set_user_session(user) - |> put_session(:login_dest, nil) - |> redirect(to: login_dest) + if Auth.TOTP.enabled?(user) and not TwoFactor.Session.remember_2fa?(conn, user) do + conn + |> TwoFactor.Session.set_2fa_user(user) + |> redirect(to: Routes.auth_path(conn, :verify_2fa)) + else + set_user_session_and_redirect(conn, user) + end end end @@ -242,8 +269,22 @@ defmodule PlausibleWeb.AuthController do end end + defp redirect_to_login(conn) do + redirect(conn, to: Routes.auth_path(conn, :login_form)) + end + + defp set_user_session_and_redirect(conn, user) do + login_dest = get_session(conn, :login_dest) || Routes.site_path(conn, :index) + + conn + |> set_user_session(user) + |> put_session(:login_dest, nil) + |> redirect(external: login_dest) + end + defp set_user_session(conn, user) do conn + |> TwoFactor.Session.clear_2fa_user() |> put_session(:current_user_id, user.id) |> put_resp_cookie("logged_in", "true", http_only: false, @@ -259,6 +300,7 @@ defmodule PlausibleWeb.AuthController do @login_interval 60_000 @login_limit 5 + defp check_ip_rate_limit(conn) do ip_address = PlausibleWeb.RemoteIp.get(conn) @@ -268,6 +310,13 @@ defmodule PlausibleWeb.AuthController do end end + defp check_user_rate_limit(user) do + case Hammer.check_rate("login:user:#{user.id}", @login_interval, @login_limit) do + {:allow, _} -> :ok + {:deny, _} -> {:rate_limit, :user} + end + end + defp find_user(email) do user = Repo.one( @@ -279,13 +328,6 @@ defmodule PlausibleWeb.AuthController do if user, do: {:ok, user}, else: :user_not_found end - defp check_user_rate_limit(user) do - case Hammer.check_rate("login:user:#{user.id}", @login_interval, @login_limit) do - {:allow, _} -> :ok - {:deny, _} -> {:rate_limit, :user} - end - end - defp check_password(user, password) do if Plausible.Auth.Password.match?(password, user.password_hash || "") do :ok @@ -299,8 +341,9 @@ defmodule PlausibleWeb.AuthController do end def user_settings(conn, _params) do - settings_changeset = Auth.User.settings_changeset(conn.assigns[:current_user]) - email_changeset = Auth.User.settings_changeset(conn.assigns[:current_user]) + user = conn.assigns.current_user + settings_changeset = Auth.User.settings_changeset(user) + email_changeset = Auth.User.settings_changeset(user) render_settings(conn, settings_changeset: settings_changeset, @@ -308,8 +351,186 @@ defmodule PlausibleWeb.AuthController do ) end + def initiate_2fa_setup(conn, _params) do + case Auth.TOTP.initiate(conn.assigns.current_user) do + {:ok, user, %{totp_uri: totp_uri, secret: secret}} -> + render(conn, "initiate_2fa_setup.html", user: user, totp_uri: totp_uri, secret: secret) + + {:error, :already_setup} -> + conn + |> put_flash(:error, "Two-Factor Authentication is already setup for this account.") + |> redirect(to: Routes.auth_path(conn, :user_settings) <> "#setup-2fa") + end + end + + def verify_2fa_setup_form(conn, _params) do + if Auth.TOTP.initiated?(conn.assigns.current_user) do + render(conn, "verify_2fa_setup.html") + else + redirect(conn, to: Routes.auth_path(conn, :user_settings) <> "#setup-2fa") + end + end + + def verify_2fa_setup(conn, %{"code" => code}) do + case Auth.TOTP.enable(conn.assigns.current_user, code) do + {:ok, _, %{recovery_codes: codes}} -> + conn + |> put_flash(:success, "Two-Factor Authentication is fully enabled") + |> render("generate_2fa_recovery_codes.html", recovery_codes: codes, from_setup: true) + + {:error, :invalid_code} -> + conn + |> put_flash(:error, "The provided code is invalid. Please try again") + |> render("verify_2fa_setup.html") + + {:error, :not_initiated} -> + conn + |> put_flash(:error, "Please enable Two-Factor Authentication for this account first.") + |> redirect(to: Routes.auth_path(conn, :user_settings) <> "#setup-2fa") + end + end + + def disable_2fa(conn, %{"password" => password}) do + case Auth.TOTP.disable(conn.assigns.current_user, password) do + {:ok, _} -> + conn + |> TwoFactor.Session.clear_remember_2fa() + |> put_flash(:success, "Two-Factor Authentication is disabled") + |> redirect(to: Routes.auth_path(conn, :user_settings) <> "#setup-2fa") + + {:error, :invalid_password} -> + conn + |> put_flash(:error, "Incorrect password provided") + |> redirect(to: Routes.auth_path(conn, :user_settings) <> "#setup-2fa") + end + end + + def generate_2fa_recovery_codes(conn, %{"password" => password}) do + case Auth.TOTP.generate_recovery_codes(conn.assigns.current_user, password) do + {:ok, codes} -> + conn + |> put_flash(:success, "New Recovery Codes generated") + |> render("generate_2fa_recovery_codes.html", recovery_codes: codes, from_setup: false) + + {:error, :invalid_password} -> + conn + |> put_flash(:error, "Incorrect password provided") + |> redirect(to: Routes.auth_path(conn, :user_settings) <> "#setup-2fa") + + {:error, :not_enabled} -> + conn + |> put_flash(:error, "Please enable Two-Factor Authentication for this account first.") + |> redirect(to: Routes.auth_path(conn, :user_settings) <> "#setup-2fa") + end + end + + def verify_2fa_form(conn, _) do + case TwoFactor.Session.get_2fa_user(conn) do + {:ok, user} -> + if Auth.TOTP.enabled?(user) do + render(conn, "verify_2fa.html", + remember_2fa_days: TwoFactor.Session.remember_2fa_days(), + layout: {PlausibleWeb.LayoutView, "focus.html"} + ) + else + redirect_to_login(conn) + end + + {:error, :not_found} -> + redirect_to_login(conn) + end + end + + def verify_2fa(conn, %{"code" => code} = params) do + with {:ok, user} <- get_2fa_user_limited(conn) do + case Auth.TOTP.validate_code(user, code) do + {:ok, user} -> + conn + |> TwoFactor.Session.maybe_set_remember_2fa(user, params["remember_2fa"]) + |> set_user_session_and_redirect(user) + + {:error, :invalid_code} -> + maybe_log_failed_login_attempts( + "wrong 2FA verification code provided for #{user.email}" + ) + + conn + |> put_flash(:error, "The provided code is invalid. Please try again") + |> render("verify_2fa.html", + remember_2fa_days: TwoFactor.Session.remember_2fa_days(), + layout: {PlausibleWeb.LayoutView, "focus.html"} + ) + + {:error, :not_enabled} -> + set_user_session_and_redirect(conn, user) + end + end + end + + def verify_2fa_recovery_code_form(conn, _params) do + case TwoFactor.Session.get_2fa_user(conn) do + {:ok, user} -> + if Auth.TOTP.enabled?(user) do + render(conn, "verify_2fa_recovery_code.html", + layout: {PlausibleWeb.LayoutView, "focus.html"} + ) + else + redirect_to_login(conn) + end + + {:error, :not_found} -> + redirect_to_login(conn) + end + end + + def verify_2fa_recovery_code(conn, %{"recovery_code" => recovery_code}) do + with {:ok, user} <- get_2fa_user_limited(conn) do + case Auth.TOTP.use_recovery_code(user, recovery_code) do + :ok -> + set_user_session_and_redirect(conn, user) + + {:error, :invalid_code} -> + maybe_log_failed_login_attempts("wrong 2FA recovery code provided for #{user.email}") + + conn + |> put_flash(:error, "The provided recovery code is invalid. Please try another one") + |> render("verify_2fa_recovery_code.html", + layout: {PlausibleWeb.LayoutView, "focus.html"} + ) + + {:error, :not_enabled} -> + set_user_session_and_redirect(conn, user) + end + end + end + + defp get_2fa_user_limited(conn) do + case TwoFactor.Session.get_2fa_user(conn) do + {:ok, user} -> + with :ok <- check_ip_rate_limit(conn), + :ok <- check_user_rate_limit(user) do + {:ok, user} + else + {:rate_limit, _} -> + maybe_log_failed_login_attempts("too many logging attempts for #{user.email}") + + conn + |> TwoFactor.Session.clear_2fa_user() + |> render_error( + 429, + "Too many login attempts. Wait a minute before trying again." + ) + end + + {:error, :not_found} -> + conn + |> redirect(to: Routes.auth_path(conn, :login_form)) + end + end + def save_settings(conn, %{"user" => user_params}) do - changes = Auth.User.settings_changeset(conn.assigns[:current_user], user_params) + user = conn.assigns.current_user + changes = Auth.User.settings_changeset(user, user_params) case Repo.update(changes) do {:ok, _user} -> @@ -318,14 +539,18 @@ defmodule PlausibleWeb.AuthController do |> redirect(to: Routes.auth_path(conn, :user_settings)) {:error, changeset} -> - email_changeset = Auth.User.settings_changeset(conn.assigns[:current_user]) + email_changeset = Auth.User.settings_changeset(user) - render_settings(conn, settings_changeset: changeset, email_changeset: email_changeset) + render_settings(conn, + settings_changeset: changeset, + email_changeset: email_changeset + ) end end def update_email(conn, %{"user" => user_params}) do - changes = Auth.User.email_changeset(conn.assigns[:current_user], user_params) + user = conn.assigns.current_user + changes = Auth.User.email_changeset(user, user_params) case Repo.update(changes) do {:ok, user} -> @@ -337,9 +562,12 @@ defmodule PlausibleWeb.AuthController do end {:error, changeset} -> - settings_changeset = Auth.User.settings_changeset(conn.assigns[:current_user]) + settings_changeset = Auth.User.settings_changeset(user) - render_settings(conn, settings_changeset: settings_changeset, email_changeset: changeset) + render_settings(conn, + settings_changeset: settings_changeset, + email_changeset: changeset + ) end end @@ -386,7 +614,8 @@ defmodule PlausibleWeb.AuthController do site_limit: Quota.site_limit(user), site_usage: Quota.site_usage(user), pageview_limit: Quota.monthly_pageview_limit(user.subscription), - pageview_usage: Quota.monthly_pageview_usage(user) + pageview_usage: Quota.monthly_pageview_usage(user), + totp_enabled?: Auth.TOTP.enabled?(user) ) end diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex index d9bace13c..282010b29 100644 --- a/lib/plausible_web/email.ex +++ b/lib/plausible_web/email.ex @@ -72,7 +72,7 @@ defmodule PlausibleWeb.Email do priority_email() |> to(user) |> tag("two-factor-enabled-email") - |> subject("Plausible two-factor authentication enabled") + |> subject("Plausible Two-Factor Authentication enabled") |> render("two_factor_enabled_email.html", user: user) end @@ -80,7 +80,7 @@ defmodule PlausibleWeb.Email do priority_email() |> to(user) |> tag("two-factor-disabled-email") - |> subject("Plausible two-factor authentication disabled") + |> subject("Plausible Two-Factor Authentication disabled") |> render("two_factor_disabled_email.html", user: user) end diff --git a/lib/plausible_web/live/components/form.ex b/lib/plausible_web/live/components/form.ex index f7cbadb4b..62779a55e 100644 --- a/lib/plausible_web/live/components/form.ex +++ b/lib/plausible_web/live/components/form.ex @@ -86,11 +86,15 @@ defmodule PlausibleWeb.Live.Components.Form do def input_with_clipboard(assigns) do ~H"""
+
+ <.label for={@id}> + <%= @label %> + +
<.input id={@id} name={@name} - label={@label} value={@value} type="text" readonly="readonly" @@ -100,10 +104,12 @@ defmodule PlausibleWeb.Live.Components.Form do - COPY + + + COPY +
diff --git a/lib/plausible_web/live/reset_password_form.ex b/lib/plausible_web/live/reset_password_form.ex index 5ffd6b7ef..49451afbc 100644 --- a/lib/plausible_web/live/reset_password_form.ex +++ b/lib/plausible_web/live/reset_password_form.ex @@ -82,9 +82,20 @@ defmodule PlausibleWeb.Live.ResetPasswordForm do end def handle_event("set", %{"user" => %{"password" => password}}, socket) do - user = Auth.User.set_password(socket.assigns.user, password) + result = + Repo.transaction(fn -> + changeset = Auth.User.set_password(socket.assigns.user, password) - case Repo.update(user) do + case Repo.update(changeset) do + {:ok, user} -> + Auth.TOTP.reset_token(user) + + {:error, changeset} -> + Repo.rollback(changeset) + end + end) + + case result do {:ok, _user} -> {:noreply, assign(socket, trigger_submit: true)} diff --git a/lib/plausible_web/live/sites.ex b/lib/plausible_web/live/sites.ex index 678bf36b3..c8284788e 100644 --- a/lib/plausible_web/live/sites.ex +++ b/lib/plausible_web/live/sites.ex @@ -558,28 +558,6 @@ defmodule PlausibleWeb.Live.Sites do """ end - attr :class, :any, default: "" - - def spinner(assigns) do - ~H""" - - - - - - - """ - end - def handle_event("pin-toggle", %{"domain" => domain}, socket) do site = Enum.find(socket.assigns.sites.entries, &(&1.domain == domain)) diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex index d9d9c2c54..35dd591dd 100644 --- a/lib/plausible_web/router.ex +++ b/lib/plausible_web/router.ex @@ -194,6 +194,15 @@ defmodule PlausibleWeb.Router do post "/login", AuthController, :login get "/password/request-reset", AuthController, :password_reset_request_form post "/password/request-reset", AuthController, :password_reset_request + post "/2fa/setup/initiate", AuthController, :initiate_2fa_setup + get "/2fa/setup/verify", AuthController, :verify_2fa_setup_form + post "/2fa/setup/verify", AuthController, :verify_2fa_setup + post "/2fa/disable", AuthController, :disable_2fa + post "/2fa/recovery_codes", AuthController, :generate_2fa_recovery_codes + get "/2fa/verify", AuthController, :verify_2fa_form + post "/2fa/verify", AuthController, :verify_2fa + get "/2fa/use_recovery_code", AuthController, :verify_2fa_recovery_code_form + post "/2fa/use_recovery_code", AuthController, :verify_2fa_recovery_code get "/password/reset", AuthController, :password_reset_form post "/password/reset", AuthController, :password_reset get "/avatar/:hash", AvatarController, :avatar diff --git a/lib/plausible_web/templates/auth/activate.html.heex b/lib/plausible_web/templates/auth/activate.html.heex index 93e940525..b406face6 100644 --- a/lib/plausible_web/templates/auth/activate.html.heex +++ b/lib/plausible_web/templates/auth/activate.html.heex @@ -27,7 +27,9 @@ required: "required" ) %> - + + Activate → + <%= error_tag(assigns, :error) %> diff --git a/lib/plausible_web/templates/auth/generate_2fa_recovery_codes.html.heex b/lib/plausible_web/templates/auth/generate_2fa_recovery_codes.html.heex new file mode 100644 index 000000000..139a9d33a --- /dev/null +++ b/lib/plausible_web/templates/auth/generate_2fa_recovery_codes.html.heex @@ -0,0 +1,49 @@ +
+
+

+ <%= if @from_setup do %> + Setup Two-Factor Authentication + <% else %> + Your New Recovery Codes + <% end %> +

+ +
+ Use these recovery codes to log in if you lose access to the authenticator application. Store them somewhere safe! +
+ <%= for code <- @recovery_codes do %> +
<%= code %>
+ <% end %> +
+
+ +
+ + + +
+
+
diff --git a/lib/plausible_web/templates/auth/initiate_2fa_setup.html.heex b/lib/plausible_web/templates/auth/initiate_2fa_setup.html.heex new file mode 100644 index 000000000..39270fbf2 --- /dev/null +++ b/lib/plausible_web/templates/auth/initiate_2fa_setup.html.heex @@ -0,0 +1,71 @@ +
+
+

+ Setup Two-Factor Authentication +

+ +
+ Link your Plausible account to the authenticator app you have installed either on your phone or computer. +
+
+
+ +
+
+ +
+
    +
  1. +
    +
    +
    + Open the authenticator application +
  2. +
  3. +
    +
    +
    + Tap Scan a QR Code +
  4. +
  5. +
    +
    +
    + Scan this code with your phone camera or paste the code manually +
  6. +
+ +
+ +
+
+
+ +
+

+ Changed your mind? + "#setup-2fa"} + class="underline text-indigo-600" + > + Go back to Settings + +

+ <.unstyled_link + id="proceed" + class="button sm:w-auto w-full" + href={Routes.auth_path(@conn, :verify_2fa_setup_form)} + > + Proceed → + +
+
+
+
diff --git a/lib/plausible_web/templates/auth/user_settings.html.heex b/lib/plausible_web/templates/auth/user_settings.html.heex index 19d50f6a9..a3a28fe3a 100644 --- a/lib/plausible_web/templates/auth/user_settings.html.heex +++ b/lib/plausible_web/templates/auth/user_settings.html.heex @@ -1,378 +1,507 @@ -<%= if full_build?() do %> -
-
-

Subscription Plan

-
- - Business - - - <%= present_subscription_status(@subscription.status) %> - +
+ <%= if full_build?() do %> +
+
+

Subscription Plan

+
+ + Business + + + <%= present_subscription_status(@subscription.status) %> + +
-
- -
- +
-
- -
-

Next bill amount

- <%= if @subscription && @subscription.status in [Plausible.Billing.Subscription.Status.active(), Plausible.Billing.Subscription.Status.past_due()] do %> -
- <%= PlausibleWeb.BillingView.present_currency(@subscription.currency_code) %><%= @subscription.next_bill_amount %> -
- <.styled_link :if={@subscription.update_url} href={@subscription.update_url}> - Update billing info - - <% else %> -
---
- <% end %> -
-
-

Next bill date

- - <%= if @subscription && @subscription.next_bill_date && @subscription.status in [Plausible.Billing.Subscription.Status.active(), Plausible.Billing.Subscription.Status.past_due()] do %> -
- <%= Timex.format!(@subscription.next_bill_date, "{Mshort} {D}, {YYYY}") %> -
- - (<%= subscription_interval(@subscription) %> billing) - - <% else %> -
---
- <% end %> -
-
- - -
-

Sites & team members usage

- - + - - -
- - <%= cond do %> - <% Plausible.Billing.Subscriptions.resumable?(@subscription) && @subscription.cancel_url -> %> -
- <%= link("Cancel my subscription", - to: @subscription.cancel_url, - class: - "inline-block mt-4 px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-red-700 bg-white dark:bg-gray-800 hover:text-red-500 focus:outline-none focus:border-blue-300 focus:ring active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150" - ) %> -
- <% true -> %> -
- +
+

Next bill amount

+ <%= if @subscription && @subscription.status in [Plausible.Billing.Subscription.Status.active(), Plausible.Billing.Subscription.Status.past_due()] do %> +
+ <%= PlausibleWeb.BillingView.present_currency(@subscription.currency_code) %><%= @subscription.next_bill_amount %> +
+ <.styled_link :if={@subscription.update_url} href={@subscription.update_url}> + Update billing info + + <% else %> +
---
+ <% end %>
- <% end %> -
+
+

Next bill date

- <%= case @invoices do %> - <% {:error, :no_invoices} -> %> - <% {:error, :request_failed} -> %> -
-

Invoices

-
-

- Something went wrong -

-
- <% {:ok, invoice_list} when is_list(invoice_list) -> %> -
-

Invoices

-
- - - - - - - - - <%= for invoice <- format_invoices(invoice_list) do %> - - - - - - - + <%= if @subscription && @subscription.next_bill_date && @subscription.status in [Plausible.Billing.Subscription.Status.active(), Plausible.Billing.Subscription.Status.past_due()] do %> +
+ <%= Timex.format!(@subscription.next_bill_date, "{Mshort} {D}, {YYYY}") %> +
+ + (<%= subscription_interval(@subscription) %> billing) + + <% else %> +
---
<% end %> -
- Date - - Amount - - Invoice -
- <%= invoice.date %> - - <%= invoice.currency <> invoice.amount %> - - <%= link("Link", to: invoice.url, target: "_blank") %> -
+
- <% end %> -<% end %> -
-

Dashboard Appearance

+ -
+
+

Sites & team members usage

+ + + + +
- <%= form_for @settings_changeset, "/settings", [class: "max-w-sm"], fn f -> %> -
- <%= label(f, :theme, "Theme Selection", - class: "block text-sm font-medium leading-5 text-gray-700 dark:text-gray-300" - ) %> - <%= select(f, :theme, Plausible.Themes.options(), - class: - "dark:bg-gray-900 mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 dark:border-gray-500 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md dark:text-gray-100 cursor-pointer" - ) %> + <%= cond do %> + <% Plausible.Billing.Subscriptions.resumable?(@subscription) && @subscription.cancel_url -> %> +
+ <%= link("Cancel my subscription", + to: @subscription.cancel_url, + class: + "inline-block mt-4 px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-red-700 bg-white dark:bg-gray-800 hover:text-red-500 focus:outline-none focus:border-blue-300 focus:ring active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150" + ) %> +
+ <% true -> %> +
+ +
+ <% end %>
- - Save - + <%= case @invoices do %> + <% {:error, :no_invoices} -> %> + <% {:error, :request_failed} -> %> +
+

Invoices

+
+

+ Something went wrong +

+
+ <% {:ok, invoice_list} when is_list(invoice_list) -> %> +
+

Invoices

+
+ + + + + + + + + <%= for invoice <- format_invoices(invoice_list) do %> + + + + + + + + <% end %> +
+ Date + + Amount + + Invoice +
+ <%= invoice.date %> + + <%= invoice.currency <> invoice.amount %> + + <%= link("Link", to: invoice.url, target: "_blank") %> +
+
+ <% end %> <% end %> -
-
-

- Change account name -

+
+

Dashboard Appearance

-
+
- <%= form_for @settings_changeset, "/settings#change-account-name", [class: "max-w-sm"], fn f -> %> -
- <%= label(f, :name, class: "block text-sm font-medium text-gray-700 dark:text-gray-300") %> -
- <%= text_input(f, :name, + <%= form_for @settings_changeset, "/settings", [class: "max-w-sm"], fn f -> %> +
+ <%= label(f, :theme, "Theme Selection", + class: "block text-sm font-medium leading-5 text-gray-700 dark:text-gray-300" + ) %> + <%= select(f, :theme, Plausible.Themes.options(), class: - "shadow-sm dark:bg-gray-900 dark:text-gray-300 focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:bg-gray-800" + "dark:bg-gray-900 mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 dark:border-gray-500 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md dark:text-gray-100 cursor-pointer" ) %> - <%= error_tag(f, :name) %>
-
- - Save - - <% end %> -
-
-

- Change email address -

+ + Save + + <% end %> +
-
+
+

+ Two-Factor Authentication (2FA) +

- <%= form_for @email_changeset, "/settings/email#change-email-address", [class: "max-w-sm"], fn f -> %> -
- <%= label(f, :password, "Account password", - class: "block text-sm font-medium text-gray-700 dark:text-gray-300" - ) %> -
- <%= password_input(f, :password, - class: - "shadow-sm dark:bg-gray-900 dark:text-gray-300 focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:bg-gray-800" +

+ Two-Factor Authentication protects your account by adding an extra security step when you log in. +

+ + <%= if @totp_enabled? do %> + + +

+ Lost your recovery codes? + + Generate new + +

+ <% else %> + <%= form_for @conn.params, Routes.auth_path(@conn, :initiate_2fa_setup), fn _ -> %> + + Enable 2FA + + <% end %> + <% end %> +
+ +
+

+ Change account name +

+ +
+ + <%= form_for @settings_changeset, "/settings#change-account-name", [class: "max-w-sm"], fn f -> %> +
+ <%= label(f, :name, class: "block text-sm font-medium text-gray-700 dark:text-gray-300") %> +
+ <%= text_input(f, :name, + class: + "shadow-sm dark:bg-gray-900 dark:text-gray-300 focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:bg-gray-800" + ) %> + <%= error_tag(f, :name) %> +
+
+ + Save + + <% end %> +
+ +
+

+ Change email address +

+ +
+ + <%= form_for @email_changeset, "/settings/email#change-email-address", [class: "max-w-sm"], fn f -> %> +
+ <%= label(f, :password, "Account password", + class: "block text-sm font-medium text-gray-700 dark:text-gray-300" ) %> - <%= error_tag(f, :password) %> +
+ <%= password_input(f, :password, + class: + "shadow-sm dark:bg-gray-900 dark:text-gray-300 focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:bg-gray-800" + ) %> + <%= error_tag(f, :password) %> +
-
-
- <%= label(f, :current_email, "Current email", - class: "block text-sm font-medium text-gray-700 dark:text-gray-300" - ) %> -
- <%= email_input(f, :current_email, - readonly: true, - value: f.data.email, - class: - "shadow-sm dark:bg-gray-900 dark:text-gray-300 focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:bg-gray-800 bg-gray-100" +
+ <%= label(f, :current_email, "Current email", + class: "block text-sm font-medium text-gray-700 dark:text-gray-300" ) %> +
+ <%= email_input(f, :current_email, + readonly: true, + value: f.data.email, + class: + "shadow-sm dark:bg-gray-900 dark:text-gray-300 focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:bg-gray-800 bg-gray-100" + ) %> +
-
-
- <%= label(f, :email, "New email", - class: "block text-sm font-medium text-gray-700 dark:text-gray-300" - ) %> -
- <%= email_input(f, :email, - value: f.params["email"], - class: - "shadow-sm dark:bg-gray-900 dark:text-gray-300 focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md" +
+ <%= label(f, :email, "New email", + class: "block text-sm font-medium text-gray-700 dark:text-gray-300" ) %> - <%= error_tag(f, :email) %> +
+ <%= email_input(f, :email, + value: f.params["email"], + class: + "shadow-sm dark:bg-gray-900 dark:text-gray-300 focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md" + ) %> + <%= error_tag(f, :email) %> +
-
- <%= submit("Change my email", - class: - "inline-block mt-4 px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-red-700 bg-white dark:bg-gray-800 hover:text-red-500 dark:hover:text-red-400 focus:outline-none focus:border-blue-300 focus:ring active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150" - ) %> - <% end %> -
+ <%= submit("Change my email", + class: + "inline-block mt-4 px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-red-700 bg-white dark:bg-gray-800 hover:text-red-500 dark:hover:text-red-400 focus:outline-none focus:border-blue-300 focus:ring active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150" + ) %> + <% end %> +
-
-

API Keys

-
- - - -
-
-
- <%= if Enum.any?(@user.api_keys) do %> -
- - - - - - - - - - <%= for api_key <- @user.api_keys do %> - - - - +
+

API Keys

+
+ + + +
+
+
+ <%= if Enum.any?(@user.api_keys) do %> +
+
- Name - - Key - - Revoke -
- <%= api_key.name %> - - <%= api_key.key_prefix %><%= String.duplicate("*", 32 - 6) %> - - <%= button("Revoke", - to: "/settings/api-keys/#{api_key.id}", - class: "text-red-600 hover:text-red-900", - method: :delete, - "data-confirm": - "Are you sure you want to revoke this key? This action cannot be reversed." - ) %> -
+ + + + + - <% end %> - -
+ Name + + Key + + Revoke +
-
- <% end %> + + + <%= for api_key <- @user.api_keys do %> + + + <%= api_key.name %> + + + <%= api_key.key_prefix %><%= String.duplicate("*", 32 - 6) %> + + + <%= button("Revoke", + to: "/settings/api-keys/#{api_key.id}", + class: "text-red-600 hover:text-red-900", + method: :delete, + "data-confirm": + "Are you sure you want to revoke this key? This action cannot be reversed." + ) %> + + + <% end %> + + +
+ <% end %> - - + New API Key - + + + New API Key + +
-
-
-
-

Delete account

+
+
+

Delete account

+
+ +
+ +

+ Deleting your account removes all sites and stats you've collected +

+ + <%= if @subscription && @subscription.status == Plausible.Billing.Subscription.Status.active() do %> + + Delete my account + +

+ Your account cannot be deleted because you have an active subscription. If you want to delete your account, please cancel your subscription first. +

+ <% else %> + <%= link("Delete my account", + to: "/me", + class: + "inline-block mt-4 px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-red-700 bg-white dark:bg-gray-800 hover:text-red-500 dark:hover:text-red-400 focus:outline-none focus:border-blue-300 focus:ring active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150", + method: "delete", + data: [ + confirm: + "Deleting your account will also delete all the sites and data that you own. This action cannot be reversed. Are you sure?" + ] + ) %> + <% end %>
-
+ + <:icon> + + + <:buttons> + <.button type="submit" class="w-full sm:w-auto"> + Disable 2FA + + -

- Deleting your account removes all sites and stats you've collected -

+
+ Once disabled, verification codes from the authenticator application and current recovery codes will become invalid. 2FA will have to be setup from the start. +
- <%= if @subscription && @subscription.status == Plausible.Billing.Subscription.Status.active() do %> - - Delete my account - -

- Your account cannot be deleted because you have an active subscription. If you want to delete your account, please cancel your subscription first. -

- <% else %> - <%= link("Delete my account", - to: "/me", - class: - "inline-block mt-4 px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-red-700 bg-white dark:bg-gray-800 hover:text-red-500 dark:hover:text-red-400 focus:outline-none focus:border-blue-300 focus:ring active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150", - method: "delete", - data: [ - confirm: - "Deleting your account will also delete all the sites and data that you own. This action cannot be reversed. Are you sure?" - ] - ) %> - <% end %> +
+ Enter your password to disable 2FA. +
+ +
+ <%= password_input(f, :password, + id: "disable_2fa_password", + placeholder: "Enter password", + "x-ref": "disable2FAPassword", + class: + "shadow-sm dark:bg-gray-900 dark:text-gray-300 focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:bg-gray-800" + ) %> +
+
+ + + <:icon> + + + + <:buttons> + <.button + id="generate-2fa-recovery-button" + type="submit" + class="w-full sm:w-auto [&>span.label-enabled]:block [&>span.label-disabled]:hidden [&[disabled]>span.label-enabled]:hidden [&[disabled]>span.label-disabled]:block" + > + + Generate New Codes + + + + <.spinner class="inline-block h-5 w-5 mr-2 text-white dark:text-gray-400" /> + Generating Codes + + + + +
+ If you generate new codes, the old ones will become invalid. +
+ +
+ Enter your password to continue. +
+ +
+ <%= password_input(f, :password, + id: "regenerate_2fa_password", + placeholder: "Enter password", + "x-ref": "regenerate2FAPassword", + class: + "w-full shadow-sm dark:bg-gray-900 dark:text-gray-300 focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:bg-gray-800" + ) %> +
+
diff --git a/lib/plausible_web/templates/auth/verify_2fa.html.heex b/lib/plausible_web/templates/auth/verify_2fa.html.heex new file mode 100644 index 000000000..f16601890 --- /dev/null +++ b/lib/plausible_web/templates/auth/verify_2fa.html.heex @@ -0,0 +1,43 @@ +
+ <%= form_for @conn.params, Routes.auth_path(@conn, :verify_2fa), [ + class: "w-full max-w-lg mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 py-6 mb-4 mt-8", + onsubmit: "document.getElementById('verify-button').disabled = true" + ], fn f -> %> +

+ Enter Your 2FA Code +

+ +
+ Enter the code from your authenticator application before it expires or wait for a new one. + + +
+ <%= checkbox(f, :remember_2fa, + class: + "block h-5 w-5 rounded dark:bg-gray-700 border-gray-300 text-indigo-600 focus:ring-indigo-600" + ) %> + +
+ +
+

+ Can't access your authenticator application? + + Use recovery code + + <%= if full_build?() do %> +
Lost your recovery codes? + + Contact us + + <% end %> +

+
+
+ <% end %> +
diff --git a/lib/plausible_web/templates/auth/verify_2fa_recovery_code.html.heex b/lib/plausible_web/templates/auth/verify_2fa_recovery_code.html.heex new file mode 100644 index 000000000..8c893acf5 --- /dev/null +++ b/lib/plausible_web/templates/auth/verify_2fa_recovery_code.html.heex @@ -0,0 +1,59 @@ +
+ <%= form_for @conn.params, + Routes.auth_path(@conn, :verify_2fa_recovery_code), + [ + class: "w-full max-w-lg mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 py-6 mb-4 mt-8", + onsubmit: "document.getElementById('use-code-button').disabled = true" + ], fn f -> %> +

+ Enter Recovery Code +

+ +
+ Can't access your authenticator application? Enter a recovery code instead. +
+
+ <%= text_input(f, :recovery_code, + value: "", + autocomplete: "off", + class: + "font-medium shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full px-2 border-gray-300 dark:border-gray-500 dark:text-gray-200 dark:bg-gray-900 rounded-md", + maxlength: "10", + oninvalid: "document.getElementById('use-code-button').disabled = false", + placeholder: "Enter recovery code", + required: "required" + ) %> +
+ <.button + id="use-code-button" + type="submit" + class="w-full mt-2 [&>span.label-enabled]:block [&>span.label-disabled]:hidden [&[disabled]>span.label-enabled]:hidden [&[disabled]>span.label-disabled]:block" + > + + Use Code + + + + + Verifying... + + +
+ +
+

+ Authenticator application working again? + + Enter verification code + + <%= if full_build?() do %> +
Lost your recovery codes? + + Contact us + + <% end %> +

+
+
+ <% end %> +
diff --git a/lib/plausible_web/templates/auth/verify_2fa_setup.html.heex b/lib/plausible_web/templates/auth/verify_2fa_setup.html.heex new file mode 100644 index 000000000..d091e48cd --- /dev/null +++ b/lib/plausible_web/templates/auth/verify_2fa_setup.html.heex @@ -0,0 +1,38 @@ +
+
+

+ Setup Two-Factor Authentication +

+ +
+ <%= form_for @conn.params, Routes.auth_path(@conn, :verify_2fa_setup), [ + id: "verify-2fa-form", + onsubmit: "document.getElementById('verify-button').disabled = true" + ], fn f -> %> + Enter the code from your authenticator application before it expires or wait for a new one. + + <% end %> + +
+

+ Changed your mind? + "#setup-2fa"} + class="underline text-indigo-600" + > + Go back to Settings + +

+ +

+ <%= form_for @conn.params, Routes.auth_path(@conn, :initiate_2fa_setup), [id: "start-over-form"], fn _f -> %> + Having trouble? + + <% end %> +

+
+
+
+
diff --git a/lib/plausible_web/templates/email/two_factor_disabled_email.html.heex b/lib/plausible_web/templates/email/two_factor_disabled_email.html.heex index 640302db6..8f09d9717 100644 --- a/lib/plausible_web/templates/email/two_factor_disabled_email.html.heex +++ b/lib/plausible_web/templates/email/two_factor_disabled_email.html.heex @@ -1 +1 @@ -Two-factor authentication is now disabled on your account. +Two-Factor Authentication is now disabled on your account. diff --git a/lib/plausible_web/templates/email/two_factor_enabled_email.html.heex b/lib/plausible_web/templates/email/two_factor_enabled_email.html.heex index b1abdabc9..62351bce8 100644 --- a/lib/plausible_web/templates/email/two_factor_enabled_email.html.heex +++ b/lib/plausible_web/templates/email/two_factor_enabled_email.html.heex @@ -1 +1 @@ -Two-factor authentication is now enabled on your account. +Two-Factor Authentication is now enabled on your account. diff --git a/lib/plausible_web/two_factor/session.ex b/lib/plausible_web/two_factor/session.ex new file mode 100644 index 000000000..ecd97ee34 --- /dev/null +++ b/lib/plausible_web/two_factor/session.ex @@ -0,0 +1,96 @@ +defmodule PlausibleWeb.TwoFactor.Session do + @moduledoc """ + Functions for managing session data related to Two-Factor + Authentication. + """ + + import Plug.Conn + + alias Plausible.Auth + + @remember_2fa_cookie "remember_2fa" + @remember_2fa_days 30 + @remember_2fa_seconds @remember_2fa_days * 24 * 60 * 60 + + @session_2fa_cookie "session_2fa" + @session_2fa_seconds 5 * 60 + + @spec set_2fa_user(Plug.Conn.t(), Auth.User.t()) :: Plug.Conn.t() + def set_2fa_user(conn, %Auth.User{} = user) do + put_resp_cookie(conn, @session_2fa_cookie, %{current_2fa_user_id: user.id}, + domain: domain(), + secure: secure_cookie?(), + encrypt: true, + max_age: @session_2fa_seconds, + same_site: "Lax" + ) + end + + @spec get_2fa_user(Plug.Conn.t()) :: {:ok, Auth.User.t()} | {:error, :not_found} + def get_2fa_user(conn) do + conn = fetch_cookies(conn, encrypted: [@session_2fa_cookie]) + session_2fa = conn.cookies[@session_2fa_cookie] + + with id when is_integer(id) <- session_2fa[:current_2fa_user_id], + %Auth.User{} = user <- Plausible.Users.with_subscription(id) do + {:ok, user} + else + _ -> {:error, :not_found} + end + end + + @spec clear_2fa_user(Plug.Conn.t()) :: Plug.Conn.t() + def clear_2fa_user(conn) do + delete_resp_cookie(conn, @session_2fa_cookie, + domain: domain(), + secure: secure_cookie?(), + encrypt: true, + max_age: @session_2fa_seconds, + same_site: "Lax" + ) + end + + @spec remember_2fa_days() :: non_neg_integer() + def remember_2fa_days(), do: @remember_2fa_days + + @spec remember_2fa?(Plug.Conn.t(), Auth.User.t()) :: boolean() + def remember_2fa?(conn, user) do + conn = fetch_cookies(conn, encrypted: [@remember_2fa_cookie]) + + not is_nil(user.totp_token) and conn.cookies[@remember_2fa_cookie] == user.totp_token + end + + @spec maybe_set_remember_2fa(Plug.Conn.t(), Auth.User.t(), String.t() | nil) :: Plug.Conn.t() + def maybe_set_remember_2fa(conn, user, "true") do + put_resp_cookie(conn, @remember_2fa_cookie, user.totp_token, + domain: domain(), + secure: secure_cookie?(), + encrypt: true, + max_age: @remember_2fa_seconds, + same_site: "Lax" + ) + end + + def maybe_set_remember_2fa(conn, _, _) do + clear_remember_2fa(conn) + end + + @spec clear_remember_2fa(Plug.Conn.t()) :: Plug.Conn.t() + def clear_remember_2fa(conn) do + delete_resp_cookie(conn, @remember_2fa_cookie, + domain: domain(), + secure: secure_cookie?(), + encrypt: true, + max_age: @remember_2fa_seconds, + same_site: "Lax" + ) + end + + defp domain(), do: PlausibleWeb.Endpoint.host() + + defp secure_cookie?() do + :plausible + |> Application.fetch_env!(PlausibleWeb.Endpoint) + |> Keyword.fetch!(:secure_cookie) + end +end diff --git a/mix.exs b/mix.exs index f7f1b9532..05a134800 100644 --- a/mix.exs +++ b/mix.exs @@ -80,6 +80,7 @@ defmodule Plausible.MixProject do {:ecto, "~> 3.10.0"}, {:ecto_sql, "~> 3.10.0"}, {:envy, "~> 1.1.1"}, + {:eqrcode, "~> 0.1.10"}, {:ex_machina, "~> 2.3", only: [:dev, :test, :small_dev, :small_test]}, {:excoveralls, "~> 0.10", only: :test}, {:finch, "~> 0.16.0"}, diff --git a/mix.lock b/mix.lock index d2621ceca..bea604e54 100644 --- a/mix.lock +++ b/mix.lock @@ -37,6 +37,7 @@ "ecto_sql": {:hex, :ecto_sql, "3.10.1", "6ea6b3036a0b0ca94c2a02613fd9f742614b5cfe494c41af2e6571bb034dd94c", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.10.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 0.17.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f6a25bdbbd695f12c8171eaff0851fa4c8e72eec1e98c7364402dda9ce11c56b"}, "elixir_make": {:hex, :elixir_make, "0.7.6", "67716309dc5d43e16b5abbd00c01b8df6a0c2ab54a8f595468035a50189f9169", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "5a0569756b0f7873a77687800c164cca6dfc03a09418e6fcf853d78991f49940"}, "envy": {:hex, :envy, "1.1.1", "0bc9bd654dec24fcdf203f7c5aa1b8f30620f12cfb28c589d5e9c38fe1b07475", [:mix], [], "hexpm", "7061eb1a47415fd757145d8dec10dc0b1e48344960265cb108f194c4252c3a89"}, + "eqrcode": {:hex, :eqrcode, "0.1.10", "6294fece9d68ad64eef1c3c92cf111cfd6469f4fbf230a2d4cc905a682178f3f", [:mix], [], "hexpm", "da30e373c36a0fd37ab6f58664b16029919896d6c45a68a95cc4d713e81076f1"}, "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, "esbuild": {:hex, :esbuild, "0.7.1", "fa0947e8c3c3c2f86c9bf7e791a0a385007ccd42b86885e8e893bdb6631f5169", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "66661cdf70b1378ee4dc16573fcee67750b59761b2605a0207c267ab9d19f13c"}, "eternal": {:hex, :eternal, "1.2.2", "d1641c86368de99375b98d183042dd6c2b234262b8d08dfd72b9eeaafc2a1abd", [:mix], [], "hexpm", "2c9fe32b9c3726703ba5e1d43a1d255a4f3f2d8f8f9bc19f094c7cb1a7a9e782"}, diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 575bce404..b6fecb7c0 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -10,6 +10,8 @@ # We recommend using the bang functions (`insert!`, `update!` # and so on) as they will fail if something goes wrong. +FunWithFlags.enable(:two_factor) + user = Plausible.Factory.insert(:user, email: "user@plausible.test", password: "plausible") native_stats_range = diff --git a/test/plausible/auth/totp_test.exs b/test/plausible/auth/totp_test.exs index 00ea22d10..9efe5db24 100644 --- a/test/plausible/auth/totp_test.exs +++ b/test/plausible/auth/totp_test.exs @@ -37,6 +37,7 @@ defmodule Plausible.Auth.TOTPTest do assert updated_user.id == user.id refute updated_user.totp_enabled + assert is_nil(updated_user.totp_token) assert byte_size(updated_user.totp_secret) > 0 assert Regex.match?(~r/[0-9A-Z]+/, params.secret) @@ -53,6 +54,7 @@ defmodule Plausible.Auth.TOTPTest do assert new_params.secret != params.secret refute updated_user.totp_enabled + assert is_nil(updated_user.totp_token) assert byte_size(updated_user.totp_secret) > 0 assert updated_user.totp_secret != user.totp_secret end @@ -83,10 +85,11 @@ defmodule Plausible.Auth.TOTPTest do assert_email_delivered_with( to: [{user.name, user.email}], - subject: "Plausible two-factor authentication enabled" + subject: "Plausible Two-Factor Authentication enabled" ) assert user.totp_enabled + assert byte_size(user.totp_token) > 0 assert byte_size(user.totp_secret) > 0 persisted_recovery_codes = Repo.all(RecoveryCode) @@ -117,6 +120,8 @@ defmodule Plausible.Auth.TOTPTest do assert updated_user.id == user.id assert updated_user.totp_enabled + assert byte_size(updated_user.totp_token) > 0 + assert updated_user.totp_token != user.totp_token assert updated_user.totp_secret == user.totp_secret assert Enum.uniq(recovery_codes ++ new_recovery_codes) == @@ -155,18 +160,19 @@ defmodule Plausible.Auth.TOTPTest do assert_email_delivered_with( to: [{user.name, user.email}], - subject: "Plausible two-factor authentication enabled" + subject: "Plausible Two-Factor Authentication enabled" ) assert {:ok, updated_user} = TOTP.disable(user, "VeryStrongVerySecret") assert_email_delivered_with( to: [{user.name, user.email}], - subject: "Plausible two-factor authentication disabled" + subject: "Plausible Two-Factor Authentication disabled" ) assert updated_user.id == user.id refute updated_user.totp_enabled + assert is_nil(updated_user.totp_token) assert is_nil(updated_user.totp_secret) assert Repo.all(RecoveryCode) == [] @@ -179,6 +185,7 @@ defmodule Plausible.Auth.TOTPTest do assert updated_user.id == user.id refute updated_user.totp_enabled + assert is_nil(updated_user.totp_token) assert is_nil(updated_user.totp_secret) end @@ -190,7 +197,7 @@ defmodule Plausible.Auth.TOTPTest do assert_email_delivered_with( to: [{user.name, user.email}], - subject: "Plausible two-factor authentication enabled" + subject: "Plausible Two-Factor Authentication enabled" ) assert {:error, :invalid_password} = TOTP.disable(user, "invalid") @@ -199,6 +206,39 @@ defmodule Plausible.Auth.TOTPTest do end end + describe "reset_token/1" do + test "generates new token when TOTP enabled" do + user = insert(:user, password: "VeryStrongVerySecret") + {:ok, user, _} = TOTP.initiate(user) + code = NimbleTOTP.verification_code(user.totp_secret) + {:ok, user, _} = TOTP.enable(user, code) + + assert %{totp_token: new_token} = TOTP.reset_token(user) + + assert byte_size(new_token) > 0 + assert new_token != user.totp_token + end + + test "sets to nil when TOTP disabled" do + user = insert(:user) + + assert %{totp_token: nil} = TOTP.reset_token(user) + + user2 = insert(:user, password: "VeryStrongVerySecret") + {:ok, user2, _} = TOTP.initiate(user2) + code = NimbleTOTP.verification_code(user2.totp_secret) + {:ok, user2, _} = TOTP.enable(user2, code) + {:ok, user2} = TOTP.disable(user2, "VeryStrongVerySecret") + + assert %{totp_token: nil} = TOTP.reset_token(user2) + + user3 = insert(:user) + {:ok, user3, _} = TOTP.initiate(user3) + + assert %{totp_token: nil} = TOTP.reset_token(user3) + end + end + describe "generate_recovery_codes/1" do test "generates recovery codes for user with enabled TOTP" do user = insert(:user, password: "VeryStrongVerySecret") diff --git a/test/plausible_web/controllers/auth_controller_test.exs b/test/plausible_web/controllers/auth_controller_test.exs index f4c331589..03dc95192 100644 --- a/test/plausible_web/controllers/auth_controller_test.exs +++ b/test/plausible_web/controllers/auth_controller_test.exs @@ -335,6 +335,73 @@ defmodule PlausibleWeb.AuthControllerTest do assert redirected_to(conn) == "/sites" end + test "valid email and password with login_dest set - redirects properly", %{conn: conn} do + user = insert(:user, password: "password") + + conn = + conn + |> init_session() + |> put_session(:login_dest, "/settings") + + conn = post(conn, "/login", email: user.email, password: "password") + + assert redirected_to(conn, 302) == "/settings" + end + + test "valid email and password with 2FA enabled - sets 2FA session and redirects", %{ + conn: conn + } do + user = insert(:user, password: "password") + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, _, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = post(conn, "/login", email: user.email, password: "password") + + assert redirected_to(conn, 302) == Routes.auth_path(conn, :verify_2fa_form) + + assert fetch_cookies(conn).cookies["session_2fa"].current_2fa_user_id == user.id + refute get_session(conn)["current_user_id"] + end + + test "valid email and password with 2FA enabled and remember 2FA cookie set - logs the user in", + %{conn: conn} do + user = insert(:user, password: "password") + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, user, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = set_remember_2fa_cookie(conn, user) + + conn = post(conn, "/login", email: user.email, password: "password") + + assert redirected_to(conn, 302) == Routes.site_path(conn, :index) + + assert conn.resp_cookies["session_2fa"].max_age == 0 + assert get_session(conn, :current_user_id) == user.id + end + + test "valid email and password with 2FA enabled and rogue remember 2FA cookie set - logs the user in", + %{conn: conn} do + user = insert(:user, password: "password") + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, user, _} = Auth.TOTP.enable(user, :skip_verify) + + another_user = insert(:user) + conn = set_remember_2fa_cookie(conn, another_user) + + conn = post(conn, "/login", email: user.email, password: "password") + + assert redirected_to(conn, 302) == Routes.auth_path(conn, :verify_2fa_form) + + assert fetch_cookies(conn).cookies["session_2fa"].current_2fa_user_id == user.id + refute get_session(conn, :current_user_id) + end + test "email does not exist - renders login form again", %{conn: conn} do conn = post(conn, "/login", email: "user@example.com", password: "password") @@ -354,28 +421,28 @@ defmodule PlausibleWeb.AuthControllerTest do user = insert(:user, password: "password") build_conn() - |> put_req_header("x-forwarded-for", "1.1.1.1") + |> put_req_header("x-forwarded-for", "1.2.3.5") |> post("/login", email: user.email, password: "wrong") build_conn() - |> put_req_header("x-forwarded-for", "1.1.1.1") + |> put_req_header("x-forwarded-for", "1.2.3.5") |> post("/login", email: user.email, password: "wrong") build_conn() - |> put_req_header("x-forwarded-for", "1.1.1.1") + |> put_req_header("x-forwarded-for", "1.2.3.5") |> post("/login", email: user.email, password: "wrong") build_conn() - |> put_req_header("x-forwarded-for", "1.1.1.1") + |> put_req_header("x-forwarded-for", "1.2.3.5") |> post("/login", email: user.email, password: "wrong") build_conn() - |> put_req_header("x-forwarded-for", "1.1.1.1") + |> put_req_header("x-forwarded-for", "1.2.3.5") |> post("/login", email: user.email, password: "wrong") conn = build_conn() - |> put_req_header("x-forwarded-for", "1.1.1.1") + |> put_req_header("x-forwarded-for", "1.2.3.5") |> post("/login", email: user.email, password: "wrong") assert get_session(conn, :current_user_id) == nil @@ -1029,6 +1096,21 @@ defmodule PlausibleWeb.AuthControllerTest do assert team_member_usage_row_text =~ "Team members 0 / 3" end + + test "redners 2FA section in disabled state", %{conn: conn} do + conn = get(conn, "/settings") + + assert html_response(conn, 200) =~ "Enable 2FA" + end + + test "renders 2FA in enabled state", %{conn: conn, user: user} do + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, _, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = get(conn, "/settings") + + assert html_response(conn, 200) =~ "Disable 2FA" + end end describe "PUT /settings" do @@ -1375,6 +1457,627 @@ defmodule PlausibleWeb.AuthControllerTest do end end + describe "POST /2fa/setup/initiate" do + setup [:create_user, :log_in] + + test "initiates setup rendering QR and human friendly versions of secret", %{ + conn: conn, + user: user + } do + conn = post(conn, Routes.auth_path(conn, :initiate_2fa_setup)) + + secret = Base.encode32(Repo.reload!(user).totp_secret) + + assert html = html_response(conn, 200) + + assert element_exists?(html, "svg") + assert html =~ secret + end + + test "redirects back to settings if 2FA is already setup", %{conn: conn, user: user} do + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, _, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = post(conn, Routes.auth_path(conn, :initiate_2fa_setup)) + + assert redirected_to(conn, 302) == Routes.auth_path(conn, :user_settings) <> "#setup-2fa" + + assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ + "Two-Factor Authentication is already setup" + end + end + + describe "GET /2fa/setup/verify" do + setup [:create_user, :log_in] + + test "renders form when 2FA setup is initiated", %{conn: conn, user: user} do + {:ok, _, _} = Auth.TOTP.initiate(user) + + conn = get(conn, Routes.auth_path(conn, :verify_2fa_setup)) + + assert html = html_response(conn, 200) + + assert text_of_attr(html, "form#verify-2fa-form", "action") == + Routes.auth_path(conn, :verify_2fa_setup) + + assert element_exists?(html, "input[name=code]") + + assert text_of_attr(html, "form#start-over-form", "action") == + Routes.auth_path(conn, :initiate_2fa_setup) + end + + test "redirects back to settings if 2FA not initiated", %{conn: conn} do + conn = get(conn, Routes.auth_path(conn, :verify_2fa_setup)) + + assert redirected_to(conn, 302) == Routes.auth_path(conn, :user_settings) <> "#setup-2fa" + end + end + + describe "POST /2fa/setup/verify" do + setup [:create_user, :log_in] + + test "enables 2FA and renders recovery codes when valid code provided", %{ + conn: conn, + user: user + } do + {:ok, user, _} = Auth.TOTP.initiate(user) + code = NimbleTOTP.verification_code(user.totp_secret) + + conn = post(conn, Routes.auth_path(conn, :verify_2fa_setup), %{code: code}) + + assert html = html_response(conn, 200) + + assert list = [_ | _] = find(html, "#recovery-codes-list > *") + assert length(list) == 10 + + assert user |> Repo.reload!() |> Auth.TOTP.enabled?() + end + + test "renders error on invalid code provided", %{conn: conn, user: user} do + {:ok, _, _} = Auth.TOTP.initiate(user) + + conn = post(conn, Routes.auth_path(conn, :verify_2fa_setup), %{code: "invalid"}) + + assert html_response(conn, 200) + + assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ + "The provided code is invalid." + end + + test "redirects to settings when 2FA is not initiated", %{conn: conn} do + conn = post(conn, Routes.auth_path(conn, :verify_2fa_setup), %{code: "123123"}) + + assert redirected_to(conn, 302) == Routes.auth_path(conn, :user_settings) <> "#setup-2fa" + + assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ + "Please enable Two-Factor Authentication" + end + end + + describe "POST /2fa/disable" do + setup [:create_user, :log_in] + + test "disables 2FA when valid password provided", %{conn: conn, user: user} do + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, _, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = post(conn, Routes.auth_path(conn, :disable_2fa), %{password: "password"}) + + assert redirected_to(conn, 302) == Routes.auth_path(conn, :user_settings) <> "#setup-2fa" + + assert Phoenix.Flash.get(conn.assigns.flash, :success) =~ + "Two-Factor Authentication is disabled" + + refute user |> Repo.reload!() |> Auth.TOTP.enabled?() + end + + test "renders error when invalid password provided", %{conn: conn, user: user} do + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, _, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = post(conn, Routes.auth_path(conn, :disable_2fa), %{password: "invalid"}) + + assert redirected_to(conn, 302) == Routes.auth_path(conn, :user_settings) <> "#setup-2fa" + + assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "Incorrect password provided" + end + end + + describe "POST /2fa/recovery_codes" do + setup [:create_user, :log_in] + + test "generates new recovery codes when valid password provided", %{conn: conn, user: user} do + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, _, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = + post(conn, Routes.auth_path(conn, :generate_2fa_recovery_codes), %{password: "password"}) + + assert html = html_response(conn, 200) + + assert list = [_ | _] = find(html, "#recovery-codes-list > *") + assert length(list) == 10 + end + + test "renders error when invalid password provided", %{conn: conn, user: user} do + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, _, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = + post(conn, Routes.auth_path(conn, :generate_2fa_recovery_codes), %{password: "invalid"}) + + assert redirected_to(conn, 302) == Routes.auth_path(conn, :user_settings) <> "#setup-2fa" + + assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "Incorrect password provided" + end + + test "renders error when 2FA is not enabled", %{conn: conn} do + conn = + post(conn, Routes.auth_path(conn, :generate_2fa_recovery_codes), %{password: "password"}) + + assert redirected_to(conn, 302) == Routes.auth_path(conn, :user_settings) <> "#setup-2fa" + + assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ + "Please enable Two-Factor Authentication" + end + end + + describe "GET /2fa/verify" do + test "renders verification form when 2FA session present", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, _, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = login_with_cookie(conn, user.email, "password") + + conn = get(conn, Routes.auth_path(conn, :verify_2fa_form)) + + assert html = html_response(conn, 200) + + assert text_of_attr(html, "form", "action") == Routes.auth_path(conn, :verify_2fa) + + assert element_exists?(html, "input[name=code]") + + assert element_exists?(html, "input[name=remember_2fa]") + + assert element_exists?( + html, + "a[href='#{Routes.auth_path(conn, :verify_2fa_recovery_code_form)}']" + ) + end + + test "redirects to login when cookie not found", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, _, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = get(conn, Routes.auth_path(conn, :verify_2fa_form)) + + assert redirected_to(conn, 302) == Routes.auth_path(conn, :login_form) + end + + test "redirects to login when 2FA not enabled", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, _, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = login_with_cookie(conn, user.email, "password") + + {:ok, _} = Auth.TOTP.disable(user, "password") + + conn = get(conn, Routes.auth_path(conn, :verify_2fa_form)) + + assert redirected_to(conn, 302) == Routes.auth_path(conn, :login_form) + end + end + + describe "POST /2fa/verify" do + test "redirects to sites when code verification succeeds", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, user, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = login_with_cookie(conn, user.email, "password") + + code = NimbleTOTP.verification_code(user.totp_secret) + + conn = post(conn, Routes.auth_path(conn, :verify_2fa), %{code: code}) + + assert redirected_to(conn, 302) == Routes.site_path(conn, :index) + + assert get_session(conn)["current_user_id"] == user.id + # 2FA session terminated + assert conn.resp_cookies["session_2fa"].max_age == 0 + # Remember cookie unset + assert conn.resp_cookies["remember_2fa"].max_age == 0 + end + + test "redirects to login_dest when set", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, user, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = + conn + |> init_session() + |> put_session(:login_dest, "/settings") + + conn = login_with_cookie(conn, user.email, "password") + + code = NimbleTOTP.verification_code(user.totp_secret) + + conn = post(conn, Routes.auth_path(conn, :verify_2fa), %{code: code}) + + assert redirected_to(conn, 302) == "/settings" + end + + test "sets remember cookie when device trusted", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, user, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = login_with_cookie(conn, user.email, "password") + + code = NimbleTOTP.verification_code(user.totp_secret) + + conn = post(conn, Routes.auth_path(conn, :verify_2fa), %{code: code, remember_2fa: "true"}) + + assert redirected_to(conn, 302) == Routes.site_path(conn, :index) + + assert get_session(conn)["current_user_id"] == user.id + # 2FA session terminated + assert conn.resp_cookies["session_2fa"].max_age == 0 + # Remember cookie set + assert conn.resp_cookies["remember_2fa"].max_age > 0 + end + + test "overwrites rogue remember cookie when device trusted", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, user, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = login_with_cookie(conn, user.email, "password") + + another_user = insert(:user, totp_token: "different_token") + conn = set_remember_2fa_cookie(conn, another_user) + + code = NimbleTOTP.verification_code(user.totp_secret) + + conn = post(conn, Routes.auth_path(conn, :verify_2fa), %{code: code, remember_2fa: "true"}) + + assert redirected_to(conn, 302) == Routes.site_path(conn, :index) + + assert get_session(conn)["current_user_id"] == user.id + # 2FA session terminated + assert conn.resp_cookies["session_2fa"].max_age == 0 + # Remember cookie set + assert conn.resp_cookies["remember_2fa"].max_age > 0 + assert fetch_cookies(conn).cookies["remember_2fa"] == user.totp_token + end + + test "clears rogue remember cookie when device _not_ trusted", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, user, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = login_with_cookie(conn, user.email, "password") + + another_user = insert(:user, totp_token: "different_token") + conn = set_remember_2fa_cookie(conn, another_user) + + code = NimbleTOTP.verification_code(user.totp_secret) + + conn = post(conn, Routes.auth_path(conn, :verify_2fa), %{code: code}) + + assert redirected_to(conn, 302) == Routes.site_path(conn, :index) + + assert get_session(conn)["current_user_id"] == user.id + # 2FA session terminated + assert conn.resp_cookies["session_2fa"].max_age == 0 + # Remember cookie cleared + assert conn.resp_cookies["remember_2fa"].max_age == 0 + end + + test "returns error on invalid code", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, user, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = login_with_cookie(conn, user.email, "password") + + conn = post(conn, Routes.auth_path(conn, :verify_2fa), %{code: "invalid"}) + + assert html_response(conn, 200) + + assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ + "The provided code is invalid" + end + + test "redirects to login when cookie not found", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, _, _} = Auth.TOTP.enable(user, :skip_verify) + + code = NimbleTOTP.verification_code(user.totp_secret) + + conn = post(conn, Routes.auth_path(conn, :verify_2fa, %{code: code})) + + assert redirected_to(conn, 302) == Routes.auth_path(conn, :login_form) + end + + test "passes through when 2FA is disabled", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, user, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = login_with_cookie(conn, user.email, "password") + + code = NimbleTOTP.verification_code(user.totp_secret) + + {:ok, _} = Auth.TOTP.disable(user, "password") + + conn = post(conn, Routes.auth_path(conn, :verify_2fa), %{code: code}) + + assert redirected_to(conn, 302) == Routes.site_path(conn, :index) + + assert get_session(conn)["current_user_id"] == user.id + # 2FA session terminated + assert conn.resp_cookies["session_2fa"].max_age == 0 + end + + test "limits verification attempts to 5 per minute", %{conn: conn} do + user = insert(:user, email: "ratio#{Ecto.UUID.generate()}@example.com") + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, user, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = login_with_cookie(conn, user.email, "password") + + conn + |> put_req_header("x-forwarded-for", "1.1.1.1") + |> post(Routes.auth_path(conn, :verify_2fa), %{code: "invalid"}) + + conn + |> put_req_header("x-forwarded-for", "1.1.1.1") + |> post(Routes.auth_path(conn, :verify_2fa), %{code: "invalid"}) + + conn + |> put_req_header("x-forwarded-for", "1.1.1.1") + |> post(Routes.auth_path(conn, :verify_2fa), %{code: "invalid"}) + + conn + |> put_req_header("x-forwarded-for", "1.1.1.1") + |> post(Routes.auth_path(conn, :verify_2fa), %{code: "invalid"}) + + conn + |> put_req_header("x-forwarded-for", "1.1.1.1") + |> post(Routes.auth_path(conn, :verify_2fa), %{code: "invalid"}) + + conn = + conn + |> put_req_header("x-forwarded-for", "1.1.1.1") + |> post(Routes.auth_path(conn, :verify_2fa), %{code: "invalid"}) + + assert get_session(conn, :current_user_id) == nil + # 2FA session terminated + assert conn.resp_cookies["session_2fa"].max_age == 0 + assert html_response(conn, 429) =~ "Too many login attempts" + end + end + + describe "GET /2fa/use_recovery_code" do + test "renders recovery verification form when 2FA session present", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, _, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = login_with_cookie(conn, user.email, "password") + + conn = get(conn, Routes.auth_path(conn, :verify_2fa_recovery_code_form)) + + assert html = html_response(conn, 200) + + assert text_of_attr(html, "form", "action") == + Routes.auth_path(conn, :verify_2fa_recovery_code) + + assert element_exists?(html, "input[name=recovery_code]") + + assert element_exists?(html, "a[href='#{Routes.auth_path(conn, :verify_2fa_form)}']") + end + + test "redirects to login when cookie not found", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, _, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = get(conn, Routes.auth_path(conn, :verify_2fa_recovery_code_form)) + + assert redirected_to(conn, 302) == Routes.auth_path(conn, :login_form) + end + + test "redirects to login when 2FA not enabled", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, _, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = login_with_cookie(conn, user.email, "password") + + {:ok, _} = Auth.TOTP.disable(user, "password") + + conn = get(conn, Routes.auth_path(conn, :verify_2fa_recovery_code_form)) + + assert redirected_to(conn, 302) == Routes.auth_path(conn, :login_form) + end + end + + describe "POST /2fa/use_recovery_code" do + test "redirects to sites when recovery code verification succeeds", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, user, %{recovery_codes: [recovery_code | _]}} = Auth.TOTP.enable(user, :skip_verify) + + conn = login_with_cookie(conn, user.email, "password") + + conn = + post(conn, Routes.auth_path(conn, :verify_2fa_recovery_code), %{ + recovery_code: recovery_code + }) + + assert redirected_to(conn, 302) == Routes.site_path(conn, :index) + + assert get_session(conn)["current_user_id"] == user.id + # 2FA session terminated + assert conn.resp_cookies["session_2fa"].max_age == 0 + end + + test "returns error on invalid recovery code", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, user, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = login_with_cookie(conn, user.email, "password") + + conn = + post(conn, Routes.auth_path(conn, :verify_2fa_recovery_code), %{recovery_code: "invalid"}) + + assert html_response(conn, 200) + + assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ + "The provided recovery code is invalid" + end + + test "redirects to login when cookie not found", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, _, %{recovery_codes: [recovery_code | _]}} = Auth.TOTP.enable(user, :skip_verify) + + conn = + post( + conn, + Routes.auth_path(conn, :verify_2fa_recovery_code, %{recovery_code: recovery_code}) + ) + + assert redirected_to(conn, 302) == Routes.auth_path(conn, :login_form) + end + + test "passes through when 2FA is disabled", %{conn: conn} do + user = insert(:user) + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, user, %{recovery_codes: [recovery_code | _]}} = Auth.TOTP.enable(user, :skip_verify) + + conn = login_with_cookie(conn, user.email, "password") + + {:ok, _} = Auth.TOTP.disable(user, "password") + + conn = + post(conn, Routes.auth_path(conn, :verify_2fa_recovery_code), %{ + recovery_code: recovery_code + }) + + assert redirected_to(conn, 302) == Routes.site_path(conn, :index) + + assert get_session(conn)["current_user_id"] == user.id + # 2FA session terminated + assert conn.resp_cookies["session_2fa"].max_age == 0 + end + + test "limits verification attempts to 5 per minute", %{conn: conn} do + user = insert(:user, email: "ratio#{Ecto.UUID.generate()}@example.com") + + # enable 2FA + {:ok, user, _} = Auth.TOTP.initiate(user) + {:ok, user, _} = Auth.TOTP.enable(user, :skip_verify) + + conn = login_with_cookie(conn, user.email, "password") + + conn + |> put_req_header("x-forwarded-for", "1.2.3.4") + |> post(Routes.auth_path(conn, :verify_2fa_recovery_code), %{recovery_code: "invalid"}) + + conn + |> put_req_header("x-forwarded-for", "1.2.3.4") + |> post(Routes.auth_path(conn, :verify_2fa_recovery_code), %{recovery_code: "invalid"}) + + conn + |> put_req_header("x-forwarded-for", "1.2.3.4") + |> post(Routes.auth_path(conn, :verify_2fa_recovery_code), %{recovery_code: "invalid"}) + + conn + |> put_req_header("x-forwarded-for", "1.2.3.4") + |> post(Routes.auth_path(conn, :verify_2fa_recovery_code), %{recovery_code: "invalid"}) + + conn + |> put_req_header("x-forwarded-for", "1.2.3.4") + |> post(Routes.auth_path(conn, :verify_2fa_recovery_code), %{recovery_code: "invalid"}) + + conn = + conn + |> put_req_header("x-forwarded-for", "1.2.3.4") + |> post(Routes.auth_path(conn, :verify_2fa_recovery_code), %{recovery_code: "invalid"}) + + assert get_session(conn, :current_user_id) == nil + # 2FA session terminated + assert conn.resp_cookies["session_2fa"].max_age == 0 + assert html_response(conn, 429) =~ "Too many login attempts" + end + end + + defp login_with_cookie(conn, email, password) do + conn + |> post(Routes.auth_path(conn, :login), %{ + email: email, + password: password + }) + |> recycle() + |> Map.put(:secret_key_base, secret_key_base()) + |> Plug.Conn.put_req_header("x-forwarded-for", Plausible.TestUtils.random_ip()) + end + + defp set_remember_2fa_cookie(conn, user) do + conn + |> PlausibleWeb.TwoFactor.Session.maybe_set_remember_2fa(user, "true") + |> recycle() + |> Map.put(:secret_key_base, secret_key_base()) + |> Plug.Conn.put_req_header("x-forwarded-for", Plausible.TestUtils.random_ip()) + end + defp mock_captcha_success() do mock_captcha(true) end @@ -1406,4 +2109,10 @@ defmodule PlausibleWeb.AuthControllerTest do billing_interval: :yearly ) end + + defp secret_key_base() do + :plausible + |> Application.fetch_env!(PlausibleWeb.Endpoint) + |> Keyword.fetch!(:secret_key_base) + end end diff --git a/test/plausible_web/live/reset_password_form_test.exs b/test/plausible_web/live/reset_password_form_test.exs index e2502aacd..3acaedfc3 100644 --- a/test/plausible_web/live/reset_password_form_test.exs +++ b/test/plausible_web/live/reset_password_form_test.exs @@ -6,6 +6,7 @@ defmodule PlausibleWeb.Live.ResetPasswordFormTest do alias Plausible.Auth.User alias Plausible.Auth.Token + alias Plausible.Auth.TOTP alias Plausible.Repo describe "/password/reset" do @@ -25,6 +26,24 @@ defmodule PlausibleWeb.Live.ResetPasswordFormTest do assert new_hash != user.password_hash end + test "reset's user's TOTP token when present", %{conn: conn} do + user = insert(:user) + {:ok, user, _} = TOTP.initiate(user) + {:ok, user, _} = TOTP.enable(user, :skip_verify) + + token = Token.sign_password_reset(user.email) + + lv = get_liveview(conn, "/password/reset?token=#{token}") + + type_into_passowrd(lv, "very-secret-and-very-long-123") + lv |> element("form") |> render_submit() + + updated_user = Repo.reload!(user) + + assert byte_size(updated_user.totp_token) > 0 + assert updated_user.totp_token != user.totp_token + end + test "renders error when new password fails validation", %{conn: conn} do user = insert(:user) token = Token.sign_password_reset(user.email) diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index a09926d59..1837715d1 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -41,8 +41,15 @@ defmodule PlausibleWeb.ConnCase do # rate limiting during tests conn = Phoenix.ConnTest.build_conn() + |> Map.put(:secret_key_base, secret_key_base()) |> Plug.Conn.put_req_header("x-forwarded-for", Plausible.TestUtils.random_ip()) {:ok, conn: conn} end + + defp secret_key_base() do + :plausible + |> Application.fetch_env!(PlausibleWeb.Endpoint) + |> Keyword.fetch!(:secret_key_base) + end end diff --git a/test/test_helper.exs b/test/test_helper.exs index ed8c80428..ea8a4d096 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -3,6 +3,7 @@ Mox.defmock(Plausible.HTTPClient.Mock, for: Plausible.HTTPClient.Interface) Application.ensure_all_started(:double) FunWithFlags.enable(:business_tier) FunWithFlags.enable(:window_time_on_page) +FunWithFlags.enable(:two_factor) Ecto.Adapters.SQL.Sandbox.mode(Plausible.Repo, :manual) if Mix.env() == :small_test do