lh-faraday-auth-hmac 1.0.1.20130116

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,31 @@
1
+ # Faraday AuthHMAC
2
+ ## HMAC Signing for Faraday Requests
3
+
4
+ Enables signing your requests (from Faraday) with AuthHMAC.
5
+
6
+ **Supports faraday ~> 0.8.1**
7
+
8
+ ## Usage
9
+
10
+ ``` ruby
11
+ require 'faraday'
12
+ require 'faraday/auth-hmac'
13
+
14
+ c = Faraday.new do |b|
15
+ b.request :auth_hmac # enables request signing
16
+ b.adapter :net_http
17
+ end
18
+
19
+ c.get('http://localhost/') do |r|
20
+ # signs the request with the access_id and the secret
21
+ r.sign! 'access_id', 'secret'
22
+ end
23
+ ```
24
+
25
+ ## Contributing
26
+
27
+ * Fork
28
+ * Work on a topic branch
29
+ * Write tests
30
+ * Add/fix/etc
31
+ * 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 = 'lh-faraday-auth-hmac'
16
+ s.version = '1.0.1.20130116'
17
+ s.date = '2013-01-16'
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", "he9lin"]
29
+ s.email = 'he9lin@gmail.com'
30
+ s.homepage = 'http://github.com/he9lin/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.8.1"])
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,101 @@
1
+ require 'faraday'
2
+ require 'faraday/request'
3
+
4
+ require 'auth-hmac'
5
+
6
+ module Faraday
7
+ class Request
8
+
9
+ register_middleware :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
+
46
+ def request_method(request)
47
+ request[:method].to_s.upcase
48
+ end
49
+
50
+ def request_body(request)
51
+ request[:body].to_s
52
+ end
53
+
54
+ def request_path(request)
55
+ request[:url].path
56
+ end
57
+
58
+ def request_path(request, authenticate_referrer)
59
+ return super if authenticate_referrer
60
+ request[:url].path
61
+ end
62
+
63
+ def headers(request)
64
+ request[:request_headers]
65
+ end
66
+
67
+ end
68
+
69
+ class << self
70
+ attr_accessor :keys, :options
71
+ end
72
+ self.keys = {}
73
+ self.options = { :signature => CanonicalString }
74
+
75
+ def self.auth
76
+ ::AuthHMAC.new(keys, options)
77
+ end
78
+
79
+ def auth
80
+ self.class.auth
81
+ end
82
+
83
+ def sign!(env, sign_with)
84
+ self.auth.sign!(env, sign_with)
85
+
86
+ # AuthHMAC doesn't set the Authorization header in the
87
+ # `request_headers` hash.
88
+ env[:request_headers][AUTH_HEADER] = env.delete(AUTH_HEADER)
89
+ end
90
+
91
+ def call(env)
92
+ if sign_with = env.delete(:sign_with)
93
+ sign!(env, sign_with)
94
+ end
95
+
96
+ @app.call(env)
97
+ end
98
+
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,61 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class AuthHMACMiddlewareTest < Faraday::TestCase
4
+ def setup
5
+ Faraday::Request::AuthHMAC.keys.clear
6
+ @access_id, @secret = "access_id", "secret"
7
+ @conn = Faraday.new do |req|
8
+ req.request :auth_hmac
9
+ req.adapter :test do |stub|
10
+ stub.post('/echo') do |env|
11
+ posted_as = env[:request_headers]['Content-Type']
12
+ [200, {'Content-Type' => posted_as}, env[:body]]
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ def test_auth_hmac_skips_when_sign_is_not_called
19
+ response = @conn.post('/echo', { :some => 'data' }, 'content-type' => 'application/x-foo')
20
+ assert_nil response.env[:request_headers]['Authorization']
21
+ end
22
+
23
+ def test_request_instructed_to_sign_a_request_will_result_in_a_correctly_signed_request
24
+ response = @conn.post('/echo', { :some => 'data' }, 'content-type' => 'application/x-foo') do |r|
25
+ r.sign! @access_id, @secret
26
+ end
27
+
28
+ assert signed?(response.env, @access_id, @secret), "should be signed"
29
+ end
30
+
31
+ def test_a_signed_request_includes_appropriate_headers
32
+ response = @conn.post('/echo', { :some => 'data' }, 'content-type' => 'application/x-foo') do |r|
33
+ r.sign! @access_id, @secret
34
+ end
35
+
36
+ %w(Authorization Content-MD5 Date).each do |header|
37
+ assert_not_nil response.env[:request_headers][header], "should have #{header} header"
38
+ end
39
+ end
40
+
41
+ protected
42
+
43
+ def klass
44
+ Faraday::Request::AuthHMAC
45
+ end
46
+
47
+ # Based on the `authenticated?` method in auth-hmac.
48
+ # https://github.com/dnclabs/auth-hmac/blob/master/lib/auth-hmac.rb#L252
49
+ def signed?(env, access_id, secret)
50
+ auth = klass.auth
51
+ rx = Regexp.new("#{klass.options[:service_id]} ([^:]+):(.+)$")
52
+ if md = rx.match(env[:request_headers][klass::AUTH_HEADER])
53
+ access_key_id = md[1]
54
+ hmac = md[2]
55
+ !secret.nil? && hmac == auth.signature(env, secret)
56
+ else
57
+ false
58
+ end
59
+ end
60
+
61
+ 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,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lh-faraday-auth-hmac
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.1.20130116
6
+ platform: ruby
7
+ authors:
8
+ - Matt Todd
9
+ - he9lin
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2013-01-16 00:00:00 Z
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.8.1
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: he9lin@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
+ homepage: http://github.com/he9lin/faraday-auth-hmac
79
+ licenses: []
80
+
81
+ post_install_message:
82
+ rdoc_options:
83
+ - --charset=UTF-8
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: "0"
98
+ requirements: []
99
+
100
+ rubyforge_project: faraday-auth-hmac
101
+ rubygems_version: 1.8.24
102
+ signing_key:
103
+ specification_version: 2
104
+ summary: Faraday AuthHMAC Request Signing Middelware
105
+ test_files: []
106
+