Improve and simplify email verification codes generation (#3407)
* Refactor email verification codes generation to avoid predictability * Improve `Site.Memberships.any?` slightly * Update tests * Fix seeds * Use `expired?` predicate for checking verification code validity in tests * Store verification code as string in database to avoid unnecessary int castingpull/3428/head
parent
87f7729cf9
commit
70c001099d
@ -0,0 +1,35 @@
|
||||
defmodule Plausible.Auth.EmailActivationCode do
|
||||
@moduledoc """
|
||||
Schema for email activation codes.
|
||||
"""
|
||||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Plausible.Auth.User
|
||||
|
||||
@type t() :: %__MODULE__{}
|
||||
|
||||
schema "email_activation_codes" do
|
||||
field :code, :string
|
||||
field :issued_at, :naive_datetime
|
||||
|
||||
belongs_to :user, User
|
||||
end
|
||||
|
||||
@spec new(User.t(), NaiveDateTime.t()) :: Ecto.Changeset.t()
|
||||
def new(user, now) do
|
||||
now = NaiveDateTime.truncate(now, :second)
|
||||
|
||||
%__MODULE__{}
|
||||
|> change(code: generate_code(), issued_at: now)
|
||||
|> put_assoc(:user, user)
|
||||
end
|
||||
|
||||
@spec generate_code() :: String.t()
|
||||
def generate_code do
|
||||
1000..9999
|
||||
|> Enum.random()
|
||||
|> Integer.to_string()
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,91 @@
|
||||
defmodule Plausible.Auth.EmailVerification do
|
||||
@moduledoc """
|
||||
API for verifying emails.
|
||||
"""
|
||||
|
||||
import Ecto.Query, only: [from: 2]
|
||||
|
||||
alias Plausible.Auth
|
||||
alias Plausible.Auth.EmailActivationCode
|
||||
alias Plausible.Repo
|
||||
|
||||
require Logger
|
||||
|
||||
@expiration_hours 4
|
||||
|
||||
@spec any?(Auth.User.t()) :: boolean()
|
||||
def any?(user) do
|
||||
Repo.exists?(from(v in EmailActivationCode, where: v.user_id == ^user.id))
|
||||
end
|
||||
|
||||
@spec issue_code(Auth.User.t(), NaiveDateTime.t()) ::
|
||||
{:ok, EmailActivationCode.t()} | {:error, :hard_bounce | :unknown_error}
|
||||
def issue_code(user, now \\ NaiveDateTime.utc_now()) do
|
||||
now = NaiveDateTime.truncate(now, :second)
|
||||
|
||||
verification =
|
||||
user
|
||||
|> EmailActivationCode.new(now)
|
||||
|> Repo.insert!(
|
||||
on_conflict: [
|
||||
set: [
|
||||
issued_at: now,
|
||||
code: EmailActivationCode.generate_code()
|
||||
]
|
||||
],
|
||||
conflict_target: :user_id,
|
||||
returning: true
|
||||
)
|
||||
|
||||
email_template = PlausibleWeb.Email.activation_email(user, verification.code)
|
||||
|
||||
case Plausible.Mailer.send(email_template) do
|
||||
:ok ->
|
||||
Logger.debug(
|
||||
"E-mail verification e-mail sent. In dev environment GET /sent-emails for details."
|
||||
)
|
||||
|
||||
{:ok, verification}
|
||||
|
||||
error ->
|
||||
error
|
||||
end
|
||||
end
|
||||
|
||||
@spec verify_code(Auth.User.t(), String.t() | non_neg_integer()) ::
|
||||
:ok | {:error, :incorrect | :expired}
|
||||
def verify_code(user, code) do
|
||||
with {:ok, verification} <- get_verification(user, code) do
|
||||
Repo.transaction(fn ->
|
||||
user
|
||||
|> Ecto.Changeset.change(email_verified: true)
|
||||
|> Repo.update!()
|
||||
|
||||
Repo.delete_all(from(c in EmailActivationCode, where: c.id == ^verification.id))
|
||||
end)
|
||||
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
defp get_verification(user, code) do
|
||||
verification = Repo.get_by(EmailActivationCode, user_id: user.id, code: code)
|
||||
|
||||
cond do
|
||||
is_nil(verification) ->
|
||||
{:error, :incorrect}
|
||||
|
||||
expired?(verification) ->
|
||||
{:error, :expired}
|
||||
|
||||
true ->
|
||||
{:ok, verification}
|
||||
end
|
||||
end
|
||||
|
||||
@spec expired?(EmailActivationCode.t()) :: boolean()
|
||||
def expired?(verification) do
|
||||
expiration_time = Timex.shift(NaiveDateTime.utc_now(), hours: -1 * @expiration_hours)
|
||||
Timex.before?(verification.issued_at, expiration_time)
|
||||
end
|
||||
end
|
||||
@ -1,17 +0,0 @@
|
||||
defmodule Plausible.Workers.CleanEmailVerificationCodes do
|
||||
use Plausible.Repo
|
||||
use Oban.Worker, queue: :clean_email_verification_codes
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(_job) do
|
||||
Repo.update_all(
|
||||
from(c in "email_verification_codes",
|
||||
where: not is_nil(c.user_id),
|
||||
where: c.issued_at < fragment("now() - INTERVAL '4 hours'")
|
||||
),
|
||||
set: [user_id: nil]
|
||||
)
|
||||
|
||||
:ok
|
||||
end
|
||||
end
|
||||
@ -1,34 +0,0 @@
|
||||
defmodule Plausible.Workers.CleanEmailVerificationCodesTest do
|
||||
use Plausible.DataCase
|
||||
alias Plausible.Workers.CleanEmailVerificationCodes
|
||||
|
||||
defp issue_code(user, issued_at) do
|
||||
code =
|
||||
Repo.one(
|
||||
from(c in "email_verification_codes", where: is_nil(c.user_id), select: c.code, limit: 1)
|
||||
)
|
||||
|
||||
Repo.update_all(from(c in "email_verification_codes", where: c.code == ^code),
|
||||
set: [user_id: user.id, issued_at: issued_at]
|
||||
)
|
||||
end
|
||||
|
||||
test "cleans codes that are more than 4 hours old" do
|
||||
user = insert(:user)
|
||||
issue_code(user, Timex.now() |> Timex.shift(hours: -5))
|
||||
issue_code(user, Timex.now() |> Timex.shift(days: -5))
|
||||
|
||||
CleanEmailVerificationCodes.perform(nil)
|
||||
|
||||
refute Repo.exists?(from c in "email_verification_codes", where: c.user_id == ^user.id)
|
||||
end
|
||||
|
||||
test "does not clean code from 2 hours ago" do
|
||||
user = insert(:user)
|
||||
issue_code(user, Timex.now() |> Timex.shift(hours: -2))
|
||||
|
||||
CleanEmailVerificationCodes.perform(nil)
|
||||
|
||||
assert Repo.exists?(from c in "email_verification_codes", where: c.user_id == ^user.id)
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue