2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-02-04 17:15:16 -05:00
|
|
|
module ActionView
|
2010-06-16 14:17:49 -04:00
|
|
|
# = Action View CSRF Helper
|
2017-08-26 20:12:19 -04:00
|
|
|
module Helpers #:nodoc:
|
2010-02-04 17:15:16 -05:00
|
|
|
module CsrfHelper
|
2010-09-11 05:04:19 -04:00
|
|
|
# Returns meta tags "csrf-param" and "csrf-token" with the name of the cross-site
|
|
|
|
# request forgery protection parameter and token, respectively.
|
|
|
|
#
|
|
|
|
# <head>
|
|
|
|
# <%= csrf_meta_tags %>
|
|
|
|
# </head>
|
|
|
|
#
|
|
|
|
# These are used to generate the dynamic forms that implement non-remote links with
|
|
|
|
# <tt>:method</tt>.
|
|
|
|
#
|
2013-07-26 11:47:18 -04:00
|
|
|
# You don't need to use these tags for regular forms as they generate their own hidden fields.
|
|
|
|
#
|
2016-08-06 13:55:02 -04:00
|
|
|
# For AJAX requests other than GETs, extract the "csrf-token" from the meta-tag and send as the
|
2017-07-19 00:36:42 -04:00
|
|
|
# "X-CSRF-Token" HTTP header. If you are using rails-ujs this happens automatically.
|
2013-07-26 11:47:18 -04:00
|
|
|
#
|
2010-09-11 05:04:19 -04:00
|
|
|
def csrf_meta_tags
|
2011-04-07 20:18:33 -04:00
|
|
|
if protect_against_forgery?
|
|
|
|
[
|
2016-08-06 13:36:34 -04:00
|
|
|
tag("meta", name: "csrf-param", content: request_forgery_protection_token),
|
|
|
|
tag("meta", name: "csrf-token", content: form_authenticity_token)
|
2011-04-07 20:18:33 -04:00
|
|
|
].join("\n").html_safe
|
|
|
|
end
|
2010-02-04 17:15:16 -05:00
|
|
|
end
|
2010-09-11 05:04:19 -04:00
|
|
|
|
|
|
|
# For backwards compatibility.
|
|
|
|
alias csrf_meta_tag csrf_meta_tags
|
2010-02-04 17:15:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|