Skip to content

Commit

Permalink
Pass method and headers properly in Faraday middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
fxposter committed Oct 8, 2012
1 parent 1b6a830 commit 855eb42
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
17 changes: 9 additions & 8 deletions lib/faraday/request/hmac.rb
Expand Up @@ -3,7 +3,7 @@

module Faraday
class Request::Hmac < Faraday::Middleware

# create a new Hmac middleware instance
#
# @param [Object] app The url of the request
Expand All @@ -13,7 +13,7 @@ class Request::Hmac < Faraday::Middleware
# @option options [String] :nonce ('') The nonce to use in the signature
# @option options [String, #strftime] :date (Time.now) The date to use in the signature
# @option options [Hash] :headers ({}) A list of optional headers to include in the signature
#
#
# @option options [String] :auth_scheme ('HMAC') The name of the authorization scheme used in the Authorization header and to construct various header-names
# @option options [String] :auth_param ('auth') The name of the authentication param to use for query based authentication
# @option options [Hash] :extra_auth_params ({}) Additional parameters to inject in the auth parameter. This parameter is ignored unless :query_based evaluates to true.
Expand All @@ -27,21 +27,22 @@ class Request::Hmac < Faraday::Middleware
def initialize(app, secret, options = {})
@app, @secret, @options, @query_values = app, secret, options
end

def call(env)
sign(env)
@app.call(env)
end

def sign(env)
signer = HMAC::Signer.new
url = env[:url]
headers, url = *signer.sign_request(url, @secret, @options)

method = env[:method]
headers, url = *signer.sign_request(url, @secret, @options.merge(:method => env[:method]))

env[:request_headers] = (env[:request_headers] || {}).merge(headers)
env[:url] = URI.parse(url)
env
end

end
end
end
45 changes: 31 additions & 14 deletions test/faraday/request/hmac_test.rb
@@ -1,5 +1,6 @@
require 'faraday'
require 'faraday/request/hmac'
require 'riot/rr'

class DummyApp
attr_accessor :env
Expand All @@ -15,75 +16,91 @@ def reset


context "the faraday middleware" do

setup do
Timecop.freeze Time.gm(2011, 7, 1, 20, 28, 55)
end

teardown do
Timecop.return
end

context "> using header-based auth" do
setup do
m = Faraday::Request::Hmac.new(DummyApp.new, "testsecret")
m.call({ :request_headers => {}, :url => 'http://www.example.com' })
end

asserts("authorization header") {topic[:request_headers]["Authorization"]}.equals("HMAC 539263f4f83878a4917d2f9c1521320c28b926a9")
asserts("date header") {topic[:request_headers]["Date"]}.equals("Fri, 1 Jul 2011 20:28:55 GMT")
asserts("query values") {topic[:url].query}.nil

context "> using a different auth header format" do
setup do
m = Faraday::Request::Hmac.new(DummyApp.new, "testsecret", {:auth_key => 'TESTKEYID', :auth_header_format => '%{auth_scheme} %{auth_key} %{signature}'})
m.call({ :request_headers => {}, :url => 'http://www.example.com' })
end

asserts("authorization header") {topic[:request_headers]["Authorization"]}.equals("HMAC TESTKEYID 539263f4f83878a4917d2f9c1521320c28b926a9")
asserts("date header") {topic[:request_headers]["Date"]}.equals("Fri, 1 Jul 2011 20:28:55 GMT")
asserts("query values") {topic[:url].query}.nil
end

end

context "> using query-based auth" do
setup do
m = Faraday::Request::Hmac.new(DummyApp.new, "testsecret", {:query_based => true, :extra_auth_params => {"auth_key" => "TESTKEYID"}})
m.call({ :request_headers => {}, :url => 'http://www.example.com' })
end

asserts("authorization header") {topic[:request_headers]["Authorization"]}.nil
asserts("date header") {topic[:request_headers]["Date"]}.nil

context "> query values" do

setup do
Rack::Utils.parse_nested_query(topic[:url].query)
end

asserts("auth date") {topic["auth"]["date"]}.equals("Fri, 1 Jul 2011 20:28:55 GMT")
asserts("auth_key") {topic["auth"]["auth_key"]}.equals("TESTKEYID")
asserts("auth signature") {topic["auth"]["signature"]}.equals("539263f4f83878a4917d2f9c1521320c28b926a9")
end

end


context "> integration test" do
setup do
con = Faraday.new(:url => "http://example.com/") do |builder|
builder.headers['X-Public-Key'] = 'TESTPUBLIC'
builder.headers['Content-MD5'] = '539263f4f83878a4917d2f9c1521320c28b926a9'

builder.use Faraday::Request::Hmac, 'TESTKEYID'
builder.adapter :test do |stub|
stub.get('/') do |env|
[200, {}, ""]
end

stub.post('/') do |env|
[200, {}, ""]
end
end
end
end

asserts("Not Raise Exeption") {topic.get('/')}
asserts("does not raise exeption") { topic.get('/') }
asserts("passes method properly") {
representation = nil
any_instance_of(HMAC::Signer) do |signer|
proxy(signer).canonical_representation do |r|
representation = r
end
end
topic.post('/')
representation[0, 4] == 'POST'
}
end

end
end

0 comments on commit 855eb42

Please sign in to comment.