Skip to content

Commit c8dafcd

Browse files
committed
Simplify Captcha configuration, add CAPTCHA_SCRIPT_URL setting
1 parent 55dfd39 commit c8dafcd

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

config/runtime.exs

+9-10
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ if config_env() == :prod do
135135
captcha_secret_key =
136136
System.get_env("CAPTCHA_SECRET_KEY") || System.get_env("HCAPTCHA_SECRET_KEY")
137137

138-
captcha_url = System.get_env("CAPTCHA_URL") || System.get_env("HCAPTCHA_URL")
138+
captcha_verify_url =
139+
System.get_env("CAPTCHA_VERIFY_URL") || System.get_env("CAPTCHA_URL") ||
140+
System.get_env("HCAPTCHA_URL")
141+
142+
captcha_script_url = System.get_env("CAPTCHA_SCRIPT_URL")
139143

140144
if captcha_site_key not in [nil, ""] and captcha_secret_key not in [nil, ""] do
141145
captcha_provider =
@@ -148,20 +152,14 @@ if config_env() == :prod do
148152

149153
Logger.info("Using the #{captcha_provider} captcha provider")
150154

151-
default_captcha_url =
152-
case captcha_provider do
153-
:hcaptcha -> "https://hcaptcha.com/siteverify"
154-
:friendly_captcha -> "https://api.friendlycaptcha.com/api/v1/siteverify"
155-
end
156-
157155
config =
158156
[
159157
secret_key: captcha_secret_key,
160158
site_key: captcha_site_key,
161-
url: default_captcha_url,
162159
provider: captcha_provider
163160
]
164-
|> put_if_not_empty.(:url, captcha_url)
161+
|> put_if_not_empty.(:verify_url, captcha_verify_url)
162+
|> put_if_not_empty.(:script_url, captcha_script_url)
165163

166164
config :keila, KeilaWeb.Captcha, config
167165
else
@@ -173,7 +171,8 @@ if config_env() == :prod do
173171
174172
- CAPTCHA_SITE_KEY
175173
- CAPTCHA_SECRET_KEY
176-
- CAPTCHA_URL (defaults to https://hcaptcha.com/siteverify or https://api.friendlycaptcha.com/api/v1/siteverify)
174+
- CAPTCHA_VERIFY_URL (defaults to https://hcaptcha.com/siteverify or https://api.friendlycaptcha.com/api/v1/siteverify)
175+
- CAPTCHA_SCRIPT_URL (defaults to https://hcaptcha.com/1/api.js for hCaptcha or https://unpkg.com/friendly-challenge@0.9.11/widget.module.min.js for Friendly Captcha)
177176
- CAPTCHA_PROVIDER (defaults to hCaptcha, unless set to 'friendly_captcha')
178177
""")
179178
end

lib/keila_web/helpers/captcha/captcha.ex

+25-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@ defmodule KeilaWeb.Captcha do
88

99
use Phoenix.HTML
1010

11-
@script_url_hcaptcha "https://hcaptcha.com/1/api.js"
12-
@script_url_friendlycaptcha "https://unpkg.com/friendly-challenge@0.9.11/widget.module.min.js"
11+
@default_urls [
12+
hcaptcha: [
13+
verify: "https://hcaptcha.com/siteverify",
14+
script: "https://hcaptcha.com/1/api.js"
15+
],
16+
friendly_captcha: [
17+
verify: "https://api.friendlycaptcha.com/api/v1/siteverify",
18+
script: "https://unpkg.com/friendly-challenge@0.9.11/widget.module.min.js"
19+
]
20+
]
1321

1422
def captcha_tag() do
1523
[
@@ -33,10 +41,9 @@ defmodule KeilaWeb.Captcha do
3341
def captcha_valid?(response) when response in [nil, ""], do: false
3442

3543
def captcha_valid?(response) do
36-
config = config()
3744
body = request_body(response)
3845

39-
with {:ok, response} <- HTTPoison.post(config[:url], body, [], recv_timeout: 5_000),
46+
with {:ok, response} <- HTTPoison.post(verify_url(), body, [], recv_timeout: 5_000),
4047
{:ok, response_body} <- Jason.decode(response.body),
4148
%{"success" => true} <- response_body do
4249
true
@@ -57,10 +64,21 @@ defmodule KeilaWeb.Captcha do
5764
end
5865
end
5966

67+
defp verify_url() do
68+
config = config()
69+
70+
case config[:verify_url] do
71+
nil -> @default_urls[config[:provider]][:verify]
72+
verify_url -> verify_url
73+
end
74+
end
75+
6076
defp script_url() do
61-
case config()[:provider] do
62-
:hcaptcha -> @script_url_hcaptcha
63-
:friendly_captcha -> @script_url_friendlycaptcha
77+
config = config()
78+
79+
case config[:script_url] do
80+
nil -> @default_urls[config[:provider]][:script]
81+
verify_url -> verify_url
6482
end
6583
end
6684

0 commit comments

Comments
 (0)