49 lines
1.6 KiB
Elixir
49 lines
1.6 KiB
Elixir
defmodule ToweropsWeb.Plugs.DetectEUUser do
|
|
@moduledoc """
|
|
Detects if a user is from an EU/EEA country that requires GDPR cookie consent.
|
|
|
|
This plug checks the CloudFlare CF-IPCountry header (if available) or other
|
|
geolocation headers to determine if the user is from an EU/EEA country.
|
|
|
|
The result is stored in conn.assigns.requires_cookie_consent
|
|
"""
|
|
import Plug.Conn
|
|
|
|
# EU/EEA country codes that require GDPR cookie consent
|
|
# EU member states + EEA countries (Norway, Iceland, Liechtenstein)
|
|
@eu_eea_countries ~w(
|
|
AT BE BG HR CY CZ DK EE FI FR DE GR HU IE IT LV LT LU MT NL PL PT RO SK SI ES SE
|
|
IS NO LI
|
|
)
|
|
|
|
@doc false
|
|
def init(opts), do: opts
|
|
|
|
@doc false
|
|
def call(conn, _opts) do
|
|
requires_consent = detect_eu_user(conn)
|
|
assign(conn, :requires_cookie_consent, requires_consent)
|
|
end
|
|
|
|
defp detect_eu_user(conn) do
|
|
# Try CloudFlare's CF-IPCountry header first (most reliable if behind CF)
|
|
case get_req_header(conn, "cf-ipcountry") do
|
|
[country_code] when country_code != "" ->
|
|
country_code = String.upcase(country_code)
|
|
country_code in @eu_eea_countries
|
|
|
|
_ ->
|
|
# Try X-Country header (some proxies set this)
|
|
case get_req_header(conn, "x-country") do
|
|
[country_code] when country_code != "" ->
|
|
country_code = String.upcase(country_code)
|
|
country_code in @eu_eea_countries
|
|
|
|
_ ->
|
|
# No reliable country detection available
|
|
# Default to showing consent banner (conservative approach for GDPR compliance)
|
|
true
|
|
end
|
|
end
|
|
end
|
|
end
|