rails-request-signing 0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ec42a4e43d44e4ba59ddc7e9757476e64be78d02
4
+ data.tar.gz: 8a9cd4b084cbe675e937d5fbde25a25f9674e5e0
5
+ SHA512:
6
+ metadata.gz: e483b0cc8bb46c53a3a9b0320c08fd00b409a0e916ef6e8a45a4deadd9649e5bfbb2ac75b1274fd20934fd43fb13fdaaf9908e5d322cc79a1e45515a6467dfdb
7
+ data.tar.gz: fffc082d74fa9854e9625486ed4c10a1cd24eb1d31ffdb027f472b7b1e11e6ab3ea293b06d0aac570ff082313e7ccb7c4f279b3c536b14424c7a88647ccbe2b4
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails-request-signing.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,51 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :bundler do
5
+ watch('Gemfile')
6
+ watch(/^.+\.gemspec/)
7
+ end
8
+
9
+ guard 'pow' do
10
+ watch('.powrc')
11
+ watch('.powenv')
12
+ watch('.rvmrc')
13
+ watch('.ruby-version')
14
+ watch('Gemfile')
15
+ watch('Gemfile.lock')
16
+ watch('config/application.rb')
17
+ watch('config/environment.rb')
18
+ watch(%r{^config/environments/.*\.rb$})
19
+ watch(%r{^config/initializers/.*\.rb$})
20
+ end
21
+
22
+ # Note: The cmd option is now required due to the increasing number of ways
23
+ # rspec may be run, below are examples of the most common uses.
24
+ # * bundler: 'bundle exec rspec'
25
+ # * bundler binstubs: 'bin/rspec'
26
+ # * spring: 'bin/rsspec' (This will use spring if running and you have
27
+ # installed the spring binstubs per the docs)
28
+ # * zeus: 'zeus rspec' (requires the server to be started separetly)
29
+ # * 'just' rspec: 'rspec'
30
+ guard :rspec, cmd: 'bundle exec rspec' do
31
+ watch(%r{^spec/.+_spec\.rb$})
32
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
33
+ watch('spec/spec_helper.rb') { "spec" }
34
+
35
+ # Rails example
36
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
37
+ watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
38
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
39
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
40
+ watch('config/routes.rb') { "spec/routing" }
41
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
42
+ watch('spec/rails_helper.rb') { "spec" }
43
+
44
+ # Capybara features specs
45
+ watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
46
+
47
+ # Turnip features and steps
48
+ watch(%r{^spec/acceptance/(.+)\.feature$})
49
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
50
+ end
51
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Vlad Verestiuc
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Rails::Request::Signing
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rails-request-signing'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rails-request-signing
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/rails-request-signing/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,76 @@
1
+ module Rails::Request::Signing
2
+
3
+ class SigningError < StandardError
4
+ end
5
+
6
+ class MissingSignatureKey < SigningError
7
+ end
8
+
9
+ class MissingSignatureChecksum < SigningError
10
+ end
11
+
12
+ class BadSignatureError < SigningError
13
+ end
14
+
15
+
16
+ class Validator
17
+
18
+ delegate :env, :headers, to: :@request
19
+
20
+ def initialize(credentials, request)
21
+ @credentials = credentials
22
+ @request = request
23
+ end
24
+
25
+
26
+ def validate!
27
+
28
+ error = if missing_access_key?
29
+ Rails::Request::Signing::MissingSignatureKey.new("missing X-Signature-ID header")
30
+ elsif missing_signature?
31
+ Rails::Request::Signing::MissingSignatureChecksum.new("missing X-Signature header")
32
+ elsif invalid_signature?
33
+ Rails::Request::Signing::BadSignatureError.new("Bad signature #{provided_signature} expected #{expected_signature}")
34
+ end
35
+
36
+ raise error if error
37
+
38
+ end
39
+
40
+ def signer_identityfier
41
+ headers["HTTP_X_SIGNATURE_ID"]
42
+ end
43
+
44
+ protected
45
+ def missing_access_key?
46
+ ! headers.key?("HTTP_X_SIGNATURE_ID")
47
+ end
48
+
49
+
50
+ def missing_signature?
51
+ ! headers.key?("HTTP_X_SIGNATURE")
52
+ end
53
+
54
+
55
+ def invalid_signature?
56
+ provided_signature.blank? or secret_key.blank? or expected_signature != provided_signature
57
+ end
58
+
59
+ def expected_signature
60
+ @expected_signature ||= begin
61
+ buffer = "#{secret_key}#{env["QUERY_STRING"]}#{env["RAW_POST_DATA"]}"
62
+ checksum = Digest::SHA2.hexdigest(buffer)
63
+ end if secret_key
64
+ end
65
+
66
+ def secret_key
67
+ @credentials[:secret_key]
68
+ end
69
+
70
+ def provided_signature
71
+ @provided_signature ||= headers["HTTP_X_SIGNATURE"]
72
+ end
73
+
74
+
75
+ end
76
+ end
@@ -0,0 +1,7 @@
1
+ module Rails
2
+ module Request
3
+ module Signing
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,45 @@
1
+ require "rails/request/signing/version"
2
+ module Rails
3
+ module Request
4
+ module Signing
5
+
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ def requires_signed_request_by!(signer, options={})
10
+ process_signed_request! signer, true, options
11
+ end
12
+
13
+ def maybe_signed_request_by!(signer, options={})
14
+ process_signed_request! signer, false, options
15
+ end
16
+
17
+ def process_signed_request!(signer, required = true, options={})
18
+
19
+ prepend_before_filter(options) do
20
+ break unless required
21
+ helper = method(signer)
22
+
23
+ signing_entity = helper.arity.zero? ? helper.call : helper.call(request.headers["HTTP_X_SIGNATURE_ID"])
24
+
25
+ credentials = {}
26
+ credentials = signing_entity.to_credentials if signing_entity.respond_to? :to_credentials
27
+
28
+ validator = Validator.new(credentials, request)
29
+ validator.validate!
30
+
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end
39
+ end
40
+
41
+
42
+
43
+ require 'rails/request/signing/validator'
44
+
45
+
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails/request/signing/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails-request-signing"
8
+ spec.version = Rails::Request::Signing::VERSION
9
+ spec.authors = ["Vlad Verestiuc"]
10
+ spec.email = ["verestiuc.vlad@gmail.com"]
11
+ spec.summary = %q{Request signing for rails}
12
+ spec.description = %q{Request signing for rails}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rails"
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency 'guard'
25
+ spec.add_development_dependency 'guard-bundler'
26
+ spec.add_development_dependency 'guard-pow'
27
+ spec.add_development_dependency 'guard-rspec'
28
+ spec.add_development_dependency 'rspec-rails'
29
+ spec.add_development_dependency "timecop"
30
+ spec.add_development_dependency "sqlite3"
31
+
32
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rails::Request::Signing::Validator do
4
+
5
+ let(:request) {
6
+ double({
7
+ headers: {
8
+ "HTTP_X_SIGNATURE_ID" => "1234",
9
+ "HTTP_X_SIGNATURE" => "153de60c0f481f16a4e229806afa87b27c8b8e8dd62406569d6172f5a7c86cce"
10
+ },
11
+ env: {
12
+ "QUERY_STRING" => "example_param=example_value",
13
+ "RAW_POST_DATA" => "request body"
14
+ }
15
+ })
16
+ }
17
+ subject{ Rails::Request::Signing::Validator.new({secret_key: "1234", access_key: "1234" }, request) }
18
+
19
+ it "should be valid" do
20
+ expect{subject.validate!}.not_to raise_error
21
+ end
22
+
23
+ it "should raise MissingSignatureKey" do
24
+ request.headers.delete "HTTP_X_SIGNATURE_ID"
25
+ expect{subject.validate!}.to raise_error Rails::Request::Signing::MissingSignatureKey
26
+ end
27
+
28
+ it "should raise MissingSignature" do
29
+ request.headers.delete "HTTP_X_SIGNATURE"
30
+ expect{subject.validate!}.to raise_error Rails::Request::Signing::MissingSignatureChecksum
31
+ end
32
+
33
+
34
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+
4
+
5
+ describe Rails::Request::Signing do
6
+
7
+ include RSpec::Rails::ControllerExampleGroup
8
+
9
+ controller(ActionController::Base) do
10
+ include Rails::Request::Signing
11
+ include Rails.application.routes.url_helpers
12
+
13
+ requires_signed_request_by! :api_client
14
+
15
+ def index
16
+ head :ok
17
+ end
18
+
19
+ rescue_from Rails::Request::Signing::SigningError do |e|
20
+ head :unauthorized
21
+ end
22
+
23
+ private
24
+ def api_client(access_key)
25
+ keys = {
26
+ '12345' => OpenStruct.new({
27
+ to_credentials: {
28
+ secret_key: '1234'
29
+ }
30
+ })
31
+ }[access_key]
32
+ end
33
+
34
+ end
35
+
36
+ before do
37
+ routes.draw do
38
+ get '/index', to: "anonymous#index"
39
+ end
40
+ end
41
+
42
+
43
+ it "should be success" do
44
+ request.headers["HTTP_X_SIGNATURE_ID"] = "12345"
45
+ request.headers["HTTP_X_SIGNATURE"] = "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4"
46
+ expect(get :index).to be_success
47
+ end
48
+
49
+ it "should not be success" do
50
+ expect(get :index).not_to be_success
51
+ end
52
+
53
+
54
+ end
@@ -0,0 +1,41 @@
1
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
2
+ $: << File.expand_path("../../lib", __FILE__)
3
+ ENV["RAILS_ENV"] ||= 'test'
4
+
5
+ require 'rails/all'
6
+ require 'rspec/rails'
7
+
8
+ require 'rails/request/signing'
9
+
10
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
11
+
12
+ module Rails::Request::Signing
13
+ class Application < Rails::Application
14
+
15
+ config.action_controller.action_on_unpermitted_parameters = :raise
16
+ config.secret_key_base = 'a-secret-key'
17
+
18
+ end
19
+ end
20
+
21
+ RSpec.configure do |config|
22
+
23
+
24
+
25
+ config.mock_with :rspec
26
+ config.infer_base_class_for_anonymous_controllers = false
27
+ config.order = "random"
28
+
29
+ config.filter_run focus: true
30
+ config.run_all_when_everything_filtered = true
31
+
32
+ end
33
+
34
+ ActionView::TestCase::TestController.instance_eval do
35
+ helper Rails.application.routes.url_helpers#, (append other helpers you need)
36
+ end
37
+ ActionView::TestCase::TestController.class_eval do
38
+ def _routes
39
+ Rails.application.routes
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,200 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-request-signing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vlad Verestiuc
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-pow
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: timecop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: sqlite3
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Request signing for rails
154
+ email:
155
+ - verestiuc.vlad@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - Gemfile
162
+ - Guardfile
163
+ - LICENSE.txt
164
+ - README.md
165
+ - Rakefile
166
+ - lib/rails/request/signing.rb
167
+ - lib/rails/request/signing/validator.rb
168
+ - lib/rails/request/signing/version.rb
169
+ - rails-request-signing.gemspec
170
+ - spec/lib/rails/request/signing/validator_spec.rb
171
+ - spec/lib/rails/request/signing_spec.rb
172
+ - spec/spec_helper.rb
173
+ homepage: ''
174
+ licenses:
175
+ - MIT
176
+ metadata: {}
177
+ post_install_message:
178
+ rdoc_options: []
179
+ require_paths:
180
+ - lib
181
+ required_ruby_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ required_rubygems_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - ">="
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ requirements: []
192
+ rubyforge_project:
193
+ rubygems_version: 2.2.2
194
+ signing_key:
195
+ specification_version: 4
196
+ summary: Request signing for rails
197
+ test_files:
198
+ - spec/lib/rails/request/signing/validator_spec.rb
199
+ - spec/lib/rails/request/signing_spec.rb
200
+ - spec/spec_helper.rb