2020-02-11 15:06:36 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
2020-05-30 19:43:25 +00:00
|
|
|
# Obtiene una cookie válida por el tiempo especificado por el
|
|
|
|
# sitio.
|
|
|
|
#
|
|
|
|
# Aunque visitemos el sitio varias veces enviando la cookie
|
|
|
|
# anterior, la cookie se renueva.
|
2020-02-11 15:06:36 +00:00
|
|
|
class InvitadesController < BaseController
|
2020-05-30 19:43:25 +00:00
|
|
|
# Cookie para el formulario de contacto
|
2020-06-16 22:10:54 +00:00
|
|
|
#
|
|
|
|
# Usamos where porque no nos importa encontrar el resultado, todes
|
|
|
|
# les visitantes reciben lo mismo, pero algunes no reciben cookie.
|
2020-05-30 19:43:25 +00:00
|
|
|
def contact_cookie
|
2020-06-16 22:10:54 +00:00
|
|
|
contact = Site.where(name: site_id, contact: true)
|
|
|
|
.pluck(:contact)
|
|
|
|
.first
|
2020-05-30 19:43:25 +00:00
|
|
|
|
|
|
|
set_cookie if contact
|
|
|
|
|
2020-06-16 23:36:10 +00:00
|
|
|
render file: Rails.root.join('public', '1x1.png'),
|
2020-05-30 19:43:25 +00:00
|
|
|
content_type: 'image/png',
|
|
|
|
layout: false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Cookie para colaboraciones anónimas
|
2020-02-11 15:06:36 +00:00
|
|
|
def cookie
|
|
|
|
# XXX: Prestar atención a que estas acciones sean lo más rápidas
|
|
|
|
# y utilicen la menor cantidad posible de recursos, porque son
|
|
|
|
# un vector de DDOS.
|
2020-06-16 22:10:54 +00:00
|
|
|
anon = Site.where(name: site_id, colaboracion_anonima: true)
|
|
|
|
.pluck(:colaboracion_anonima)
|
|
|
|
.first
|
2020-02-11 15:06:36 +00:00
|
|
|
|
2020-05-30 19:43:25 +00:00
|
|
|
set_cookie if anon
|
|
|
|
|
2020-06-16 23:36:10 +00:00
|
|
|
render file: Rails.root.join('public', '1x1.png'),
|
2020-06-16 22:10:54 +00:00
|
|
|
content_type: 'image/png',
|
|
|
|
layout: false
|
2020-05-30 19:43:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-06-16 22:10:54 +00:00
|
|
|
# Genera el Origin correcto a partir de la URL del sitio.
|
|
|
|
#
|
|
|
|
# En desarrollo devuelve el Origin enviado.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def return_origin
|
|
|
|
Rails.env.production? ? Site.find_by(name: site_id).url : origin
|
|
|
|
end
|
|
|
|
|
2020-05-30 19:43:25 +00:00
|
|
|
# La cookie no es accesible a través de JS y todo su contenido
|
|
|
|
# está cifrado para que no lo modifiquen les visitantes
|
|
|
|
#
|
|
|
|
# Enviamos un token de protección CSRF
|
|
|
|
def set_cookie
|
2020-08-18 13:13:57 +00:00
|
|
|
# TODO: Volver configurable por sitio
|
|
|
|
expires = ENV.fetch('COOKIE_DURATION', '30').to_i.minutes
|
|
|
|
|
2020-06-16 22:10:54 +00:00
|
|
|
headers['Access-Control-Allow-Origin'] = return_origin
|
|
|
|
headers['Access-Control-Allow-Credentials'] = true
|
|
|
|
headers['Vary'] = 'Origin'
|
2020-08-18 13:13:57 +00:00
|
|
|
headers['Cache-Control'] = "private, max-age=#{expires}, stale-while-revalidate=#{expires}"
|
2020-06-16 22:10:54 +00:00
|
|
|
|
|
|
|
cookies.encrypted[site_id] = {
|
2020-05-30 19:43:25 +00:00
|
|
|
httponly: true,
|
|
|
|
secure: !Rails.env.test?,
|
|
|
|
expires: expires,
|
2020-06-16 22:10:54 +00:00
|
|
|
same_site: :strict,
|
2020-05-30 19:43:25 +00:00
|
|
|
value: {
|
|
|
|
csrf: form_authenticity_token,
|
|
|
|
expires: (Time.now + expires).to_i
|
|
|
|
}
|
|
|
|
}
|
2020-02-11 15:06:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|