From 6b67f13aaf84560f0311109a49432b4a92898dd7 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 29 Jul 2021 10:53:54 -0300 Subject: [PATCH] Origin y Referer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Algunos navegadores no envían Origin, la forma de obtenerlo es mirar en el Referer. --- app/controllers/api/v1/base_controller.rb | 24 ++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index 2d58187c..649c6078 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -23,8 +23,30 @@ module Api end end + # Referer + # + # @see {https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer} + # @return [String,Nil] + def referer + request.referer + end + alias referrer referer + + # Origin + # + # @see {https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin} + # @return [String,Nil] def origin - request.headers['Origin'] + request.origin + end + + # Genera un header Origin a partir del Referer si existe. + # + # @return [String,Nil] + def origin_from_referer + return if referer.blank? + + referer.split('/', 4).tap { |u| u.pop if u.size > 3 }.join('/') end # Los navegadores antiguos no envían Origin