faraday-auth-hmac 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'tzinfo', '~> 0.3.28'
7
+ gem 'activesupport', '~> 2.3'
8
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 Matt Todd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Faraday AuthHMAC
2
+ ## HMAC Signing for Faraday Requests
3
+
4
+ Enables signing your requests (from Faraday) with AuthHMAC.
5
+
6
+ ## Usage
7
+
8
+ ``` ruby
9
+ require 'faraday'
10
+ require 'faraday/auth-hmac'
11
+
12
+ c = Faraday.new do |b|
13
+ b.request :auth_hmac # enables request signing
14
+ b.adapter :net_http
15
+ end
16
+
17
+ c.get('http://localhost/') do |r|
18
+ # signs the request with the access_id and the secret
19
+ r.sign! 'access_id', 'secret'
20
+ end
21
+ ```
22
+
23
+ ## Contributing
24
+
25
+ * Fork
26
+ * Work on a topic branch
27
+ * Write tests
28
+ * Add/fix/etc
29
+ * Create a Pull Request
@@ -0,0 +1,142 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/faraday/auth-hmac.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/testtask'
49
+ Rake::TestTask.new(:test) do |test|
50
+ test.libs << 'lib' << 'test'
51
+ test.pattern = 'test/**/*_test.rb'
52
+ test.verbose = true
53
+ end
54
+
55
+ require 'rake/rdoctask'
56
+ Rake::RDocTask.new do |rdoc|
57
+ rdoc.rdoc_dir = 'rdoc'
58
+ rdoc.title = "#{name} #{version}"
59
+ rdoc.rdoc_files.include('README*')
60
+ rdoc.rdoc_files.include('lib/**/*.rb')
61
+ end
62
+
63
+ desc "Open an irb session preloaded with this library"
64
+ task :console do
65
+ sh "ruby -rubygems -r irb -r ./test/helper -r faraday -r ./lib/faraday/auth-hmac.rb " <<
66
+ %(-e 'Test::Unit.run = true' ) << # so tests wont run
67
+ %(-e '$c = Faraday.new("http://httpbin.org"){ |b| b.request :auth_hmac; b.response :logger; b.adapter :net_http }' ) <<
68
+ %(-e '$r = $c.get("/get"){ |r| r.sign! "access_id", "secret" }' ) <<
69
+ %(-e 'puts $r.body' ) <<
70
+ %(-e 'IRB.start')
71
+ end
72
+
73
+ #############################################################################
74
+ #
75
+ # Custom tasks (add your own tasks here)
76
+ #
77
+ #############################################################################
78
+
79
+
80
+
81
+ #############################################################################
82
+ #
83
+ # Packaging tasks
84
+ #
85
+ #############################################################################
86
+
87
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
88
+ task :release => :build do
89
+ unless `git branch` =~ /^\* master$/
90
+ puts "You must be on the master branch to release!"
91
+ exit!
92
+ end
93
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
94
+ sh "git tag v#{version}"
95
+ sh "git push origin master"
96
+ sh "git push origin v#{version}"
97
+ sh "gem push pkg/#{name}-#{version}.gem"
98
+ end
99
+
100
+ desc "Build #{gem_file} into the pkg directory"
101
+ task :build => :gemspec do
102
+ sh "mkdir -p pkg"
103
+ sh "gem build #{gemspec_file}"
104
+ sh "mv #{gem_file} pkg"
105
+ end
106
+
107
+ desc "Generate #{gemspec_file}"
108
+ task :gemspec => :validate do
109
+ # read spec file and split out manifest section
110
+ spec = File.read(gemspec_file)
111
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
112
+
113
+ # replace name version and date
114
+ replace_header(head, :name)
115
+ replace_header(head, :version)
116
+ replace_header(head, :date)
117
+ #comment this out if your rubyforge_project has a different name
118
+ replace_header(head, :rubyforge_project)
119
+
120
+ # determine file list from git ls-files
121
+ files = `git ls-files`.
122
+ split("\n").
123
+ sort.
124
+ reject { |file| file =~ /^\./ }.
125
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
126
+ map { |file| " #{file}" }.
127
+ join("\n")
128
+
129
+ # piece file back together and write
130
+ manifest = " s.files = %w[\n#{files}\n ]\n"
131
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
132
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
133
+ puts "Updated #{gemspec_file}"
134
+ end
135
+
136
+ desc "Validate #{gemspec_file}"
137
+ task :validate do
138
+ unless Dir['VERSION*'].empty?
139
+ puts "A `VERSION` file at root level violates Gem best practices."
140
+ exit!
141
+ end
142
+ end
@@ -0,0 +1,70 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'faraday-auth-hmac'
16
+ s.version = '1.0.1'
17
+ s.date = '2011-06-20'
18
+ s.rubyforge_project = 'faraday-auth-hmac'
19
+
20
+ ## Make sure your summary is short. The description may be as long
21
+ ## as you like.
22
+ s.summary = "Faraday AuthHMAC Request Signing Middelware"
23
+ s.description = "Signs Faraday requests with AuthHMAC"
24
+
25
+ ## List the primary authors. If there are a bunch of authors, it's probably
26
+ ## better to set the email to an email list or something. If you don't have
27
+ ## a custom homepage, consider using your GitHub URL or the like.
28
+ s.authors = ["Matt Todd"]
29
+ s.email = 'chiology@gmail.com'
30
+ s.homepage = 'http://github.com/mtodd/faraday-auth-hmac'
31
+
32
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
33
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
34
+ s.require_paths = %w[lib]
35
+
36
+ ## Specify any RDoc options here. You'll want to add your README and
37
+ ## LICENSE files to the extra_rdoc_files list.
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.extra_rdoc_files = %w[README.md LICENSE.md]
40
+
41
+ ## List your runtime dependencies here. Runtime dependencies are those
42
+ ## that are needed for an end user to actually USE your code.
43
+ s.add_dependency('faraday', ["~> 0.7.2"])
44
+ s.add_dependency('dnclabs-auth-hmac', ["~> 1.1.1"])
45
+
46
+ ## List your development dependencies here. Development dependencies are
47
+ ## those that are only needed during development
48
+ s.add_development_dependency('test-unit', ["~> 2.3"])
49
+ s.add_development_dependency('leftright', ["~> 0.9"])
50
+
51
+ ## Leave this section as-is. It will be automatically generated from the
52
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
53
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
54
+ # = MANIFEST =
55
+ s.files = %w[
56
+ Gemfile
57
+ LICENSE.md
58
+ README.md
59
+ Rakefile
60
+ faraday-auth-hmac.gemspec
61
+ lib/faraday/auth-hmac.rb
62
+ test/auth-hmac_test.rb
63
+ test/helper.rb
64
+ ]
65
+ # = MANIFEST =
66
+
67
+ ## Test files will be grabbed from the file list. Make sure the path glob
68
+ ## matches what you actually use.
69
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
70
+ end
@@ -0,0 +1,94 @@
1
+ require 'faraday'
2
+ require 'faraday/request'
3
+
4
+ require 'auth-hmac'
5
+
6
+ module Faraday
7
+ class Request
8
+
9
+ register_lookup_modules :auth_hmac => :AuthHMAC
10
+
11
+ attr_accessor :sign_with
12
+
13
+ # Sign the request with the specified `access_id` and `secret`.
14
+ def sign!(access_id, secret)
15
+ AuthHMAC.keys[access_id] = secret
16
+ self.sign_with = access_id
17
+ end
18
+
19
+ # Include the `sign_with` property to ensure the request is signed with
20
+ # the specified `access_id`.
21
+ alias_method :original_to_env, :to_env
22
+ def to_env(connection)
23
+ original_to_env(connection).merge(:sign_with => self.sign_with)
24
+ end
25
+
26
+ # Sign your request using AuthHMAC.
27
+ #
28
+ # @connection.get('http://localhost/') do |req|
29
+ # req.sign! 'access_id', 'secret'
30
+ # req.body = 'abc'
31
+ # end
32
+ #
33
+ # This adds the Authorization, Content-MD5, and Date headers.
34
+ #
35
+ # This middleware can be added and nothing will happen unless the `sign!`
36
+ # method is called (as in the example above).
37
+ #
38
+ class AuthHMAC < Faraday::Middleware
39
+ VERSION = '1.0.1'
40
+ AUTH_HEADER = "Authorization".freeze
41
+
42
+ # Modified CanonicalString to know how to pull from the Faraday-specific
43
+ # env hash.
44
+ class CanonicalString < ::AuthHMAC::CanonicalString
45
+ def request_method(request)
46
+ request[:method].to_s.upcase
47
+ end
48
+ def request_body(request)
49
+ request[:body]
50
+ end
51
+ def request_path(request)
52
+ URI.parse(request[:url]).path
53
+ end
54
+ def request_path(request, authenticate_referrer)
55
+ return super if authenticate_referrer
56
+ URI.parse(request[:url]).path
57
+ end
58
+ def headers(request)
59
+ request[:request_headers]
60
+ end
61
+ end
62
+
63
+ class << self
64
+ attr_accessor :keys, :options
65
+ end
66
+ self.keys = {}
67
+ self.options = {:service_id => "FaradayHMAC", :signature => CanonicalString}
68
+
69
+ def self.auth
70
+ ::AuthHMAC.new(keys, options)
71
+ end
72
+ def auth
73
+ self.class.auth
74
+ end
75
+
76
+ def sign!(env, sign_with)
77
+ self.auth.sign!(env, sign_with)
78
+
79
+ # AuthHMAC doesn't set the Authorization header in the
80
+ # `request_headers` hash.
81
+ env[:request_headers][AUTH_HEADER] = env.delete(AUTH_HEADER)
82
+ end
83
+
84
+ def call(env)
85
+ if sign_with = env.delete(:sign_with)
86
+ sign!(env, sign_with)
87
+ end
88
+
89
+ @app.call(env)
90
+ end
91
+
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,75 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+ # require 'rack/utils'
3
+
4
+ class AuthHMACMiddlewareTest < Faraday::TestCase
5
+ def setup
6
+ Faraday::Request::AuthHMAC.keys.clear
7
+ @access_id, @secret = "id", "secret"
8
+ @connection = Faraday.new :url => 'http://sushi.com/api'
9
+ @request = Faraday::Request.create(:get) do |req|
10
+ req.url 'foo.json'
11
+ req.body = "test"
12
+ end
13
+ generate_env!
14
+ end
15
+
16
+ def test_auth_hmac_skips_when_sign_is_not_called
17
+ call(@env)
18
+ assert_nil @env[:request_headers]['Authorization']
19
+ end
20
+
21
+ def test_request_will_instruct_middleware_to_sign_if_told_to
22
+ assert_nil @env[:sign_with]
23
+
24
+ @request.sign! @access_id, @secret
25
+ generate_env!
26
+ assert_equal @access_id, @env[:sign_with]
27
+ end
28
+
29
+ def test_request_instructed_to_sign_a_request_will_result_in_a_correctly_signed_request
30
+ @env[:sign_with] = @access_id
31
+ klass.keys = {@access_id => @secret}
32
+
33
+ call(@env)
34
+ assert signed?(@env, @access_id, @secret), "should be signed"
35
+ end
36
+
37
+ def test_a_signed_request_includes_appropriate_headers
38
+ @request.sign! @access_id, @secret
39
+ generate_env!
40
+ call(@env)
41
+
42
+ %w(Authorization Content-MD5 Date).each do |header|
43
+ assert_not_nil @env[:request_headers][header], "should have #{header} header"
44
+ end
45
+ end
46
+
47
+ protected
48
+
49
+ def klass
50
+ Faraday::Request::AuthHMAC
51
+ end
52
+
53
+ def call(env)
54
+ klass.new(lambda{|_|}).call(env)
55
+ end
56
+
57
+ def generate_env!
58
+ @env = @request.to_env(@connection)
59
+ end
60
+
61
+ # Based on the `authenticated?` method in auth-hmac.
62
+ # https://github.com/dnclabs/auth-hmac/blob/master/lib/auth-hmac.rb#L252
63
+ def signed?(env, access_id, secret)
64
+ auth = klass.auth
65
+ rx = Regexp.new("#{klass.options[:service_id]} ([^:]+):(.+)$")
66
+ if md = rx.match(env[:request_headers][klass::AUTH_HEADER])
67
+ access_key_id = md[1]
68
+ hmac = md[2]
69
+ !secret.nil? && hmac == auth.signature(env, secret)
70
+ else
71
+ false
72
+ end
73
+ end
74
+
75
+ end
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+
3
+ require 'bundler'
4
+ Bundler.setup(:default, :development)
5
+
6
+ require 'test/unit'
7
+
8
+ require 'time'
9
+
10
+ require 'active_support/core_ext/object/blank'
11
+ require 'active_support/time_with_zone'
12
+
13
+ if ENV['LEFTRIGHT']
14
+ begin
15
+ require 'leftright'
16
+ rescue LoadError
17
+ puts "Run `gem install leftright` to install leftright."
18
+ end
19
+ end
20
+
21
+ unless $LOAD_PATH.include? 'lib'
22
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
23
+ $LOAD_PATH.unshift(File.join($LOAD_PATH.first, '..', 'lib'))
24
+ end
25
+
26
+ require 'faraday'
27
+ require 'faraday/auth-hmac'
28
+
29
+ begin
30
+ require 'ruby-debug'
31
+ rescue LoadError
32
+ # ignore
33
+ else
34
+ Debugger.start
35
+ end
36
+
37
+ module Faraday
38
+ class TestCase < Test::Unit::TestCase
39
+ def test_default
40
+ assert true
41
+ end unless defined? ::MiniTest
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday-auth-hmac
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Matt Todd
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-20 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: faraday
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: 0.7.2
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: dnclabs-auth-hmac
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 1.1.1
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: test-unit
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: "2.3"
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: leftright
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: "0.9"
58
+ type: :development
59
+ version_requirements: *id004
60
+ description: Signs Faraday requests with AuthHMAC
61
+ email: chiology@gmail.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - README.md
68
+ - LICENSE.md
69
+ files:
70
+ - Gemfile
71
+ - LICENSE.md
72
+ - README.md
73
+ - Rakefile
74
+ - faraday-auth-hmac.gemspec
75
+ - lib/faraday/auth-hmac.rb
76
+ - test/auth-hmac_test.rb
77
+ - test/helper.rb
78
+ has_rdoc: true
79
+ homepage: http://github.com/mtodd/faraday-auth-hmac
80
+ licenses: []
81
+
82
+ post_install_message:
83
+ rdoc_options:
84
+ - --charset=UTF-8
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project: faraday-auth-hmac
102
+ rubygems_version: 1.6.0
103
+ signing_key:
104
+ specification_version: 2
105
+ summary: Faraday AuthHMAC Request Signing Middelware
106
+ test_files: []
107
+