mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
Set up SES mail delivery and harden the mailing list (#120)
* setup basic SES email sending * basic honeypot form form submission
This commit is contained in:
@@ -13,6 +13,7 @@ defmodule ChesstrainerWeb.LandingComponents do
|
||||
alias Chesstrainer.FEN
|
||||
alias Chesstrainer.Themes
|
||||
alias ChesstrainerWeb.Board.PositionText
|
||||
alias ChesstrainerWeb.MailingListFormToken
|
||||
|
||||
@button_variants %{
|
||||
nav: "nav-cta",
|
||||
@@ -93,6 +94,19 @@ defmodule ChesstrainerWeb.LandingComponents do
|
||||
|
||||
<form action="/mailing-list" method="post" id="mailing-list-form" class="mailing-list-form">
|
||||
<input type="hidden" name="_csrf_token" value={get_csrf_token()} />
|
||||
<input type="hidden" name="subscriber[form_token]" value={MailingListFormToken.sign()} />
|
||||
|
||||
<div class="mailing-list-honeypot" aria-hidden="true">
|
||||
<label for="mailing-list-company">Company</label>
|
||||
<input
|
||||
id="mailing-list-company"
|
||||
type="text"
|
||||
name="subscriber[company]"
|
||||
tabindex="-1"
|
||||
autocomplete="off"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<label for="mailing-list-email">Email address</label>
|
||||
<div class="mailing-list-row">
|
||||
<input
|
||||
|
||||
@@ -3,45 +3,31 @@ defmodule ChesstrainerWeb.MailingListController do
|
||||
|
||||
alias Chesstrainer.MailingList
|
||||
alias Chesstrainer.MailingList.Notifier
|
||||
alias ChesstrainerWeb.MailingListFormToken
|
||||
|
||||
@return_to "/#mailing-list"
|
||||
@token_salt "mailing_list_confirm"
|
||||
@token_max_age 7 * 24 * 60 * 60
|
||||
|
||||
def create(conn, %{"subscriber" => %{"email" => email}}) when is_binary(email) do
|
||||
email = String.trim(email)
|
||||
def create(conn, %{"subscriber" => params}) when is_map(params) do
|
||||
email = params["email"]
|
||||
|
||||
case MailingList.subscribe(email) do
|
||||
{:ok, :already_subscribed} ->
|
||||
conn
|
||||
|> put_flash(:info, "You're already on the mailing list.")
|
||||
|> redirect(to: @return_to)
|
||||
cond do
|
||||
honeypot_filled?(params) ->
|
||||
fake_success(conn)
|
||||
|
||||
{:ok, :confirmation_required} ->
|
||||
case send_confirmation(email) do
|
||||
:ok ->
|
||||
conn
|
||||
|> put_flash(:info, "Check your inbox to confirm your subscription.")
|
||||
|> redirect(to: @return_to)
|
||||
not is_binary(email) ->
|
||||
invalid_email(conn)
|
||||
|
||||
:error ->
|
||||
conn
|
||||
|> put_flash(:error, "We couldn't send the confirmation email. Please try again.")
|
||||
|> redirect(to: @return_to)
|
||||
end
|
||||
MailingListFormToken.verify(params["form_token"]) != :ok ->
|
||||
form_error(conn)
|
||||
|
||||
{:error, %Ecto.Changeset{}} ->
|
||||
conn
|
||||
|> put_flash(:error, "Enter a valid email address.")
|
||||
|> redirect(to: @return_to)
|
||||
true ->
|
||||
subscribe(conn, email)
|
||||
end
|
||||
end
|
||||
|
||||
def create(conn, _params) do
|
||||
conn
|
||||
|> put_flash(:error, "Enter a valid email address.")
|
||||
|> redirect(to: @return_to)
|
||||
end
|
||||
def create(conn, _params), do: invalid_email(conn)
|
||||
|
||||
def confirm(conn, %{"token" => token}) do
|
||||
case Phoenix.Token.verify(
|
||||
@@ -77,13 +63,63 @@ defmodule ChesstrainerWeb.MailingListController do
|
||||
end
|
||||
end
|
||||
|
||||
defp send_confirmation(email) do
|
||||
defp subscribe(conn, email) do
|
||||
email = String.trim(email)
|
||||
|
||||
case MailingList.subscribe(email) do
|
||||
{:ok, :already_subscribed} ->
|
||||
conn
|
||||
|> put_flash(:info, "You're already on the mailing list.")
|
||||
|> redirect(to: @return_to)
|
||||
|
||||
{:ok, :confirmation_required} ->
|
||||
if MailingList.confirmation_allowed?(email) do
|
||||
send_confirmation(conn, email)
|
||||
else
|
||||
fake_success(conn)
|
||||
end
|
||||
|
||||
{:error, %Ecto.Changeset{}} ->
|
||||
invalid_email(conn)
|
||||
end
|
||||
end
|
||||
|
||||
defp send_confirmation(conn, email) do
|
||||
token = Phoenix.Token.sign(ChesstrainerWeb.Endpoint, @token_salt, email)
|
||||
url = url(~p"/mailing-list/confirm/#{token}")
|
||||
|
||||
case Notifier.deliver_confirmation_instructions(email, url) do
|
||||
{:ok, _email} -> :ok
|
||||
{:error, _reason} -> :error
|
||||
{:ok, _email} ->
|
||||
_ = MailingList.mark_confirmation_sent(email)
|
||||
|
||||
conn
|
||||
|> put_flash(:info, "Check your inbox to confirm your subscription.")
|
||||
|> redirect(to: @return_to)
|
||||
|
||||
{:error, _reason} ->
|
||||
conn
|
||||
|> put_flash(:error, "We couldn't send the confirmation email. Please try again.")
|
||||
|> redirect(to: @return_to)
|
||||
end
|
||||
end
|
||||
|
||||
defp honeypot_filled?(params), do: params["company"] not in [nil, ""]
|
||||
|
||||
defp fake_success(conn) do
|
||||
conn
|
||||
|> put_flash(:info, "Check your inbox to confirm your subscription.")
|
||||
|> redirect(to: @return_to)
|
||||
end
|
||||
|
||||
defp form_error(conn) do
|
||||
conn
|
||||
|> put_flash(:error, "Please refresh the page and try again.")
|
||||
|> redirect(to: @return_to)
|
||||
end
|
||||
|
||||
defp invalid_email(conn) do
|
||||
conn
|
||||
|> put_flash(:error, "Enter a valid email address.")
|
||||
|> redirect(to: @return_to)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,6 +4,7 @@ defmodule ChesstrainerWeb.UserLive.Profile do
|
||||
alias Chesstrainer.Accounts
|
||||
alias Chesstrainer.Accounts.Scope
|
||||
alias Chesstrainer.Attempts
|
||||
alias Chesstrainer.MailingList
|
||||
|
||||
@recent_attempts 25
|
||||
|
||||
@@ -159,6 +160,27 @@ defmodule ChesstrainerWeb.UserLive.Profile do
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<section id="mailing-list-settings" class="space-y-3 rounded-lg border border-base-300 p-4">
|
||||
<h2 class="settings-header">Mailing list</h2>
|
||||
|
||||
<p :if={@mailing_list_subscribed?} class="settings-subtitle">
|
||||
You're subscribed to occasional endgame tips and site news.
|
||||
</p>
|
||||
|
||||
<p :if={!@mailing_list_subscribed?} class="settings-subtitle">
|
||||
You're not subscribed to the mailing list.
|
||||
</p>
|
||||
|
||||
<.button
|
||||
:if={@mailing_list_subscribed?}
|
||||
id="unsubscribe-mailing-list"
|
||||
variant="secondary"
|
||||
phx-click="unsubscribe_mailing_list"
|
||||
>
|
||||
Unsubscribe
|
||||
</.button>
|
||||
</section>
|
||||
|
||||
<p id="settings-empty-state" class="text-sm text-base-content/60">
|
||||
More site settings will appear here as they become available.
|
||||
</p>
|
||||
@@ -332,6 +354,24 @@ defmodule ChesstrainerWeb.UserLive.Profile do
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("unsubscribe_mailing_list", _params, socket) do
|
||||
user = socket.assigns.current_scope.user
|
||||
|
||||
case MailingList.unsubscribe_user(user) do
|
||||
{:ok, _subscriber} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:mailing_list_subscribed?, false)
|
||||
|> put_flash(:info, "You've been removed from the mailing list.")}
|
||||
|
||||
{:error, :not_found} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:mailing_list_subscribed?, false)
|
||||
|> put_flash(:info, "You're not on the mailing list.")}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("delete_account", _params, socket) do
|
||||
user = socket.assigns.current_scope.user
|
||||
true = Accounts.sudo_mode?(user)
|
||||
@@ -348,10 +388,16 @@ defmodule ChesstrainerWeb.UserLive.Profile do
|
||||
case socket.assigns.live_action do
|
||||
:account -> assign_account(socket)
|
||||
:data -> assign_data(socket)
|
||||
:settings -> assign_settings(socket)
|
||||
_ -> socket
|
||||
end
|
||||
end
|
||||
|
||||
defp assign_settings(socket) do
|
||||
user = socket.assigns.current_scope.user
|
||||
assign(socket, :mailing_list_subscribed?, MailingList.user_subscribed?(user))
|
||||
end
|
||||
|
||||
defp assign_account(socket) do
|
||||
user = socket.assigns.current_scope.user
|
||||
email_changeset = Accounts.change_user_email(user, %{}, validate_unique: false)
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
defmodule ChesstrainerWeb.MailingListFormToken do
|
||||
@moduledoc """
|
||||
Signed timestamp hidden in the mailing-list signup form.
|
||||
|
||||
Used as a timing check: submissions that arrive faster than a human could
|
||||
plausibly fill the form are rejected. The timestamp is signed so a bot
|
||||
cannot simply make one up.
|
||||
"""
|
||||
|
||||
@salt "mailing_list_form"
|
||||
@max_age 2 * 60 * 60
|
||||
@min_seconds 2
|
||||
|
||||
@type verify_result :: :ok | :too_fast | :expired | :invalid
|
||||
|
||||
@doc "Signs the current time for the hidden form field."
|
||||
def sign, do: sign(System.system_time(:second))
|
||||
|
||||
@doc false
|
||||
def sign(signed_at) when is_integer(signed_at) do
|
||||
Phoenix.Token.sign(ChesstrainerWeb.Endpoint, @salt, signed_at, signed_at: signed_at)
|
||||
end
|
||||
|
||||
@doc "Checks a submitted form token."
|
||||
@spec verify(term()) :: verify_result()
|
||||
def verify(token) when is_binary(token) do
|
||||
now = System.system_time(:second)
|
||||
|
||||
case Phoenix.Token.verify(ChesstrainerWeb.Endpoint, @salt, token, max_age: @max_age) do
|
||||
{:ok, signed_at} when is_integer(signed_at) and now - signed_at >= @min_seconds -> :ok
|
||||
{:ok, _signed_at} -> :too_fast
|
||||
{:error, :expired} -> :expired
|
||||
{:error, _reason} -> :invalid
|
||||
end
|
||||
end
|
||||
|
||||
def verify(_token), do: :invalid
|
||||
end
|
||||
Reference in New Issue
Block a user