Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2464380

Browse files
committedSep 13, 2012
first commit
0 parents  commit 2464380

4 files changed

+70
-0
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.gem

‎README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Cacheable CSRF Token for Rails
2+
3+
### Cache HTML containing CSRF protection tokens without worrying
4+
5+
CacheableCSRFToken allows you to easily cache Ruby on Rails pages or partials containing a CSRF protection token. The user-specific token will inserted in the HTML before the response is sent to the user.
6+
7+
#### Usage
8+
9+
1. Add `cacheable-csrf-rails` to your Gemfile
10+
2. Add this line in ApplicationController:
11+
`include CacheableCSRFTokenRails`

‎cacheable-csrf-token-rails.gemspec

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Gem::Specification.new do |s|
2+
s.name = %q{cacheable-csrf-token-rails}
3+
s.version = "0.1.0"
4+
s.date = %q{2012-09-13}
5+
s.summary = %q{Cache HTML containing CSRF protection tokens without worrying}
6+
s.description = %q{CacheableCSRFToken allows you to easily cache Ruby on Rails pages or partials containing a CSRF protection token. The user-specific token will inserted in the HTML before the response is sent to the user.}
7+
s.authors = ["Carl Mercier"]
8+
s.email = ["carl@carlmercier.com"]
9+
s.homepage = "http://github.com/cmer/cacheable-csrf-token-rails"
10+
s.files = ["README.md", "lib/cacheable-csrf-token-rails.rb"]
11+
12+
s.add_dependency('rails', '>= 3.2.5')
13+
end

‎lib/cacheable-csrf-token-rails.rb

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Inspired from http://www.jarrodspillers.com/2010/02/06/trying-to-use-rails-csrf-protection-on-cached-actions-rack-middleware-to-the-rescue/ and https://gist.github.com/1124982/632f1fcbe0981424128b3088ddb27a322c369cc1
2+
3+
module CacheableCSRFTokenRails
4+
def self.included(base)
5+
6+
ApplicationController.const_set "TOKEN_PLACEHOLDER", "__CROSS_SITE_REQUEST_FORGERY_PROTECTION_TOKEN__"
7+
base.class_eval do
8+
after_filter :inject_csrf_token
9+
10+
private
11+
def inject_csrf_token
12+
if protect_against_forgery? && token = session['_csrf_token']
13+
if body_with_token = response.body.gsub!(ApplicationController::TOKEN_PLACEHOLDER, token)
14+
response.body = body_with_token
15+
end
16+
end
17+
end
18+
end
19+
20+
ActionView::Helpers::FormTagHelper.class_eval do
21+
alias_method :token_tag_rails, :token_tag
22+
23+
def token_tag(token=nil)
24+
if token != false && protect_against_forgery?
25+
token ||= form_authenticity_token
26+
tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => ApplicationController::TOKEN_PLACEHOLDER)
27+
else
28+
''
29+
end
30+
end
31+
end
32+
33+
ActionView::Helpers::CsrfHelper.class_eval do
34+
def csrf_meta_tags
35+
if protect_against_forgery?
36+
[
37+
tag('meta', :name => 'csrf-param', :content => request_forgery_protection_token),
38+
tag('meta', :name => 'csrf-token', :content => ApplicationController::TOKEN_PLACEHOLDER)
39+
].join("\n").html_safe
40+
end
41+
end
42+
end
43+
44+
end # included
45+
end

0 commit comments

Comments
 (0)
Please sign in to comment.