0.0
No commit activity in last 3 years
No release in over 3 years
Transparently handles Rails (and maybe not only Rails) CSRF protection, in case you need to send requests to an app that doesn't provide an API
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

> 1.9
~> 10.4.2
~> 3.3
~> 2.9
~> 1.21

Runtime

 Project Readme

Build Status Code Climate

Faraday CSRF middleware

Transparently handles Rails (and maybe not only Rails) CSRF protection, in case you need to send requests to an app that doesn't provide an API. It tries to extract a CSRF token from each request and later inserts it into (POST, PUT, DELETE, etc.) requests that probably require it.

Installation

Add this line to your application's Gemfile:

gem 'faraday_csrf'

You need to add it to your stack, you would also need a cookie_jar (gem 'faraday-cookie_jar').

Usage

Create a Faraday connection like this:

conn = Faraday.new url: 'https://a-rails-app.example.com/' do |conn|
  conn.use :csrf
  conn.request :url_encoded
  conn.use :cookie_jar
  conn.adapter Faraday.default_adapter
end

When you would make a get request, the CSRF thingy would try to parse the page and extract the token from it. When you make a POST request after that, it would add the token to it.

You have to use faraday-cookie_jar gem for handling cookies, and use :url_encoded middleware or something of that nature to allow this middleware to insert tokens.

You can get the token it extracted by accessing response_env[:csrf_token].

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/unmanbearpig/faraday_csrf.