add captcha verification

This commit is contained in:
byte[] 2019-11-15 11:14:23 -05:00
parent b18e17a179
commit 58a045ed29
5 changed files with 55 additions and 4 deletions

View file

@ -21,9 +21,10 @@ config :philomena, :pow,
repo: Philomena.Repo,
web_module: PhilomenaWeb,
users_context: Philomena.Users,
extensions: [PowResetPassword, PowLockout, PowPersistentSession],
extensions: [PowResetPassword, PowLockout, PowCaptcha, PowPersistentSession],
controller_callbacks: Pow.Extension.Phoenix.ControllerCallbacks,
mailer_backend: PhilomenaWeb.PowMailer
mailer_backend: PhilomenaWeb.PowMailer,
captcha_verifier: Philomena.Captcha
config :bcrypt_elixir,
log_rounds: 12

View file

@ -127,4 +127,11 @@ defmodule Philomena.Captcha do
def valid_solution?(_solution_id, _solution),
do: false
def valid_solution?(%{"captcha" => %{"id" => id, "sln" => solution}}) do
valid_solution?(id, solution)
end
def valid_solution?(_params),
do: false
end

View file

@ -17,4 +17,4 @@ div
label> for="captcha_sln[#{i}]"
| Name of pony with cutie mark #
= i
= select :captcha, "sln[#{i}]", options, class: "input"
= select :captcha, "sln[#{i}]", options, class: "input", name: "captcha[sln][#{i}]"

View file

@ -0,0 +1,43 @@
defmodule PowCaptcha.Phoenix.ControllerCallbacks do
@moduledoc """
Controller callback logic for captcha verification.
"""
use Pow.Extension.Phoenix.ControllerCallbacks.Base
alias Pow.Config
alias Plug.Conn
alias Phoenix.Controller
alias Pow.Phoenix.RegistrationController
alias PowResetPassword.Phoenix.ResetPasswordController
@doc false
@impl true
def before_process(RegistrationController, :create, conn, config) do
verifier = Config.get(config, :captcha_verifier)
return_path = routes(conn).registration_path(conn, :new)
verifier.valid_solution?(conn.params)
|> maybe_halt(conn, return_path)
end
def before_process(ResetPasswordController, :create, conn, config) do
verifier = Config.get(config, :captcha_verifier)
return_path = routes(conn).path_for(conn, ResetPasswordController, :new)
verifier.valid_solution?(conn.params)
|> maybe_halt(conn, return_path)
end
defp maybe_halt(false, conn, return_path) do
conn
|> Controller.put_flash(:error, "There was an error verifying you're not a robot. Please try again.")
|> Controller.redirect(to: return_path)
|> Conn.halt()
end
defp maybe_halt(true, conn, _return_path) do
conn
end
end

View file

@ -1,6 +1,6 @@
defmodule PowLockout.Phoenix.ControllerCallbacks do
@moduledoc """
Controller callback logic for e-mail confirmation.
Controller callback logic for account lockout.
### User is locked out