Created
March 26, 2013 05:54
-
-
Save docunext/5243442 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Thanks http://devblog.mixlr.com/2012/09/01/nginx-lua/ | |
class CsrfTokenEndpoint | |
def self.call(env) | |
if env["PATH_INFO"] =~ /^\/csrf_token_endpoint/ | |
session = env["rack.session"] || {} | |
token = session[:_csrf_token] | |
if token.nil? | |
token = SecureRandom.base64(32) | |
session[:_csrf_token] = token | |
end | |
[ 200, { "Content-Type" => "text/plain" }, [ token ] ] | |
else | |
[404, {"Content-Type" => "text/html"}, ["Not Found"]] | |
end | |
end | |
end | |
# This would go into config/routes.rb | |
#get 'csrf_token_endpoint' => CsrfTokenEndpoint |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment