Skip to content

Commit

Permalink
Merge pull request sferik#167 from NateBarnes/master
Browse files Browse the repository at this point in the history
Custom OAuth Fix
  • Loading branch information
sferik committed Jun 1, 2011
2 parents e15548a + 88f28bd commit e26567e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
24 changes: 24 additions & 0 deletions lib/faraday/request/oauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'faraday'

module Faraday
class Request::OAuth < Faraday::Middleware
dependency 'simple_oauth'

def call(env)
params = env[:body] || {}
signature_params = params

params.map{ |k,v| signature_params = {} if v.respond_to?(:content_type) }

header = SimpleOAuth::Header.new(env[:method], env[:url], signature_params, @options)

env[:request_headers]['Authorization'] = header.to_s

@app.call(env)
end

def initialize(app, options)
@app, @options = app, options
end
end
end
24 changes: 24 additions & 0 deletions lib/faraday/request/twitter_oauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'faraday'

module Faraday
class Request::TwitterOAuth < Faraday::Middleware
dependency 'simple_oauth'

def call(env)
params = env[:body] || {}
signature_params = params

params.map{ |k,v| signature_params = {} if v.respond_to?(:content_type) }

header = SimpleOAuth::Header.new(env[:method], env[:url], signature_params, @options)

env[:request_headers]['Authorization'] = header.to_s

@app.call(env)
end

def initialize(app, options)
@app, @options = app, options
end
end
end
3 changes: 2 additions & 1 deletion lib/twitter/connection.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'faraday_middleware'
require 'faraday/request/multipart_with_file'
require 'faraday/request/gateway'
require 'faraday/request/twitter_oauth'
require 'faraday/response/raise_http_4xx'
require 'faraday/response/raise_http_5xx'

Expand All @@ -19,7 +20,7 @@ def connection(raw=false)

Faraday.new(options) do |builder|
builder.use Faraday::Request::MultipartWithFile
builder.use Faraday::Request::OAuth, authentication if authenticated?
builder.use Faraday::Request::TwitterOAuth, authentication if authenticated?
builder.use Faraday::Request::Multipart
builder.use Faraday::Request::UrlEncoded
builder.use Faraday::Request::Gateway, gateway if gateway
Expand Down
29 changes: 29 additions & 0 deletions spec/faraday/request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'helper'

describe Faraday::Request do
before(:each) do
@oauth = Faraday::Request::TwitterOAuth.new lambda { |env| return env }, Hash.new
@req = { :method => "post", :url => "http://test.com/test.JSON", :request_headers => {}, :body => { :status => "Yar" } }
end

describe "OAuth v1" do
it "should encode the entire body when no uploaded media is present" do
res = SimpleOAuth::Header.parse(@oauth.call( @req )[:request_headers]["Authorization"])
sig = res[:signature]
res.delete(:signature)

newhead = SimpleOAuth::Header.new(:post, "http://test.com/test.JSON", { :status => "Yar" }, res)
sig.should == SimpleOAuth::Header.parse(newhead.to_s)[:signature]
end

it "should encode none of the body when uploaded media is present" do
@req[:body] = { :file => Faraday::UploadIO.new("Rakefile", "Test"), :status => "Yar" }
res = SimpleOAuth::Header.parse(@oauth.call( @req )[:request_headers]["Authorization"])
sig = res[:signature]
res.delete(:signature)

newhead = SimpleOAuth::Header.new(:post, "http://test.com/test.JSON", { }, res)
sig.should == SimpleOAuth::Header.parse(newhead.to_s)[:signature]
end
end
end

0 comments on commit e26567e

Please sign in to comment.