Skip to content

Instantly share code, notes, and snippets.

@docunext
Created March 26, 2013 05:54
Show Gist options
  • Save docunext/5243442 to your computer and use it in GitHub Desktop.
Save docunext/5243442 to your computer and use it in GitHub Desktop.
# 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