mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 20:18:00 +01:00
add captcha verification
This commit is contained in:
parent
b18e17a179
commit
58a045ed29
5 changed files with 55 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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}]"
|
||||
|
|
43
lib/pow_captcha/phoenix/controllers/controller_callbacks.ex
Normal file
43
lib/pow_captcha/phoenix/controllers/controller_callbacks.ex
Normal 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
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue