philomena/lib/philomena_web/plugs/captcha_plug.ex

30 lines
801 B
Elixir
Raw Permalink Normal View History

2019-11-24 19:36:21 +01:00
defmodule PhilomenaWeb.CaptchaPlug do
2020-09-12 19:43:16 +02:00
alias PhilomenaWeb.ContentSecurityPolicyPlug
2019-11-24 19:36:21 +01:00
2020-09-12 19:43:16 +02:00
@hcaptcha_url ["https://hcaptcha.com", "https://*.hcaptcha.com"]
2019-11-24 19:36:21 +01:00
2020-09-12 19:43:16 +02:00
def init(_opts) do
[]
2019-11-24 19:36:21 +01:00
end
2020-09-12 19:43:16 +02:00
# Set CSP headers for serving captchas.
# Only holepunch CSP if the user is not signed in.
@spec call(Plug.Conn.t(), any()) :: Plug.Conn.t()
def call(conn, _opts) do
user = conn.assigns.current_user
2020-01-11 05:20:19 +01:00
2020-09-12 19:43:16 +02:00
maybe_assign_csp_headers(conn, user)
2019-11-24 19:36:21 +01:00
end
2020-01-11 05:20:19 +01:00
2020-09-12 19:43:16 +02:00
defp maybe_assign_csp_headers(conn, nil) do
conn
2020-09-12 19:43:16 +02:00
|> ContentSecurityPolicyPlug.permit_source(:script_src, @hcaptcha_url)
|> ContentSecurityPolicyPlug.permit_source(:frame_src, @hcaptcha_url)
|> ContentSecurityPolicyPlug.permit_source(:style_src, @hcaptcha_url)
end
2020-09-12 19:43:16 +02:00
defp maybe_assign_csp_headers(conn, _user) do
conn
end
2019-11-24 19:36:21 +01:00
end