rack-simple_auth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 391baa3938d8f51b5501b0064c25100dfaf8941c
4
+ data.tar.gz: f785d92ba75ad9b0b968202460b384da9c36f489
5
+ SHA512:
6
+ metadata.gz: ec4dbc9f92f9625b28c355d0745f4d6648c7d06f79b98a2078133124a6648de7e7b926f21c75604385b2e5f57e2e99151d628f16aafb7d5fba715e4bb6df6a5b
7
+ data.tar.gz: 217410019be2f9157251c14f7eb35ca7af23e652b5394f09cbcd9d189f5ee5f594df56eea976a32dc36bba5ce842cb35626c4151a33610ba6bac63e6f77099eb
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
18
+ *.swp
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ LineLength:
2
+ Max: 160
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ rvm:
5
+ - 2.0.0
6
+ - 2.1.0
7
+ - 2.1.1
8
+ - ruby-head
9
+ - jruby
10
+ - rbx
11
+
12
+ matrix:
13
+ allow_failures:
14
+ - rvm: jruby
15
+ - rvm: ruby-head
16
+
17
+ script: "env COVERAGE=true bundle exec rake"
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --private
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-simple_auth.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Benny1992
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,49 @@
1
+ # Rack::SimpleAuth
2
+
3
+ Rack Middleware for HMAC Authentication
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rack-simple_auth'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack-simple_auth
18
+
19
+ ## Gem Status
20
+
21
+ [![Build Status](https://travis-ci.org/Benny1992/rack-simple_auth.png?branch=master)](https://travis-ci.org/Benny1992/rack-simple_auth)
22
+ [![Coverage Status](https://coveralls.io/repos/Benny1992/rack-simple_auth/badge.png?branch=master)](https://coveralls.io/r/Benny1992/rack-simple_auth?branch=master)
23
+ [![GitHub version](https://badge.fury.io/gh/benny1992%2Frack-simple_auth.png)](http://badge.fury.io/gh/benny1992%2Frack-simple_auth)
24
+
25
+ ## Usage
26
+
27
+ Uses Authorization HTTP Header, example:
28
+ ```Authorization: ContentHash:Signature```
29
+
30
+ Signature is the "Public Key"
31
+
32
+ ContentHash is the HMAC encrypted Message
33
+
34
+ ```ruby
35
+ map '/' do
36
+ use Rack::SimpleAuth::HMAC, 'signature', 'private_key'
37
+ run MyApplication
38
+ end
39
+ ```
40
+
41
+ Private Key and Signature should be served by a file which is not checked into git version control.
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( http://github.com/<my-github-username>/rack-simple_auth/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+ # require 'cucumber/rake/task'
4
+
5
+ Dir.glob('tasks/*.rake').each { |r| import r }
6
+
7
+
@@ -0,0 +1,18 @@
1
+ require 'rack/simple_auth/version'
2
+ require 'rack/simple_auth/hmac'
3
+
4
+ require 'json'
5
+
6
+ # Rack Module
7
+ module Rack
8
+ # Module which Contains different Authorization / Authentication Classes (HMAC, ..)
9
+ module SimpleAuth
10
+ class << self
11
+ # Method to return Gem Root Dir
12
+ # @return [String] Gem Root Folder
13
+ def root
14
+ ::File.dirname(::File.dirname(::File.expand_path('..', __FILE__)))
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,62 @@
1
+ module Rack
2
+ # Module which Contains different Authorization / Authentication Classes (HMAC, ..)
3
+ module SimpleAuth
4
+ # HMAC Middleware uses HMAC Authorization for Securing an REST API
5
+ class HMAC
6
+ # Constructor for Rack Middleware (passing the rack stack)
7
+ # @param [Rack Application] app [next middleware or rack app which gets called]
8
+ # @param [String] signature [Public Signature]
9
+ # @param [String] secret [Secret used for Message Encryption]
10
+ def initialize(app, signature, secret)
11
+ @app = app
12
+ @signature = signature
13
+ @secret = secret
14
+ end
15
+
16
+ # call Method for Rack Middleware/Application
17
+ # @param [Hash] env [Rack Env Hash which contains headers etc..]
18
+ def call(env)
19
+ request = Rack::Request.new(env)
20
+ if valid?(request)
21
+ @app.call(env)
22
+ else
23
+ response = Rack::Response.new('Unauthorized', 401, 'Content-Type' => 'text/html')
24
+ response.finish
25
+ end
26
+ end
27
+
28
+ # checks for valid HMAC Request
29
+ # @param [Rack::Request] request [current Request]
30
+ # @return [boolean] ValidationStatus [If authorized returns true, else false]
31
+ def valid?(request)
32
+ return false if request.env['HTTP_AUTHORIZATION'].nil?
33
+
34
+ auth_array = request.env['HTTP_AUTHORIZATION'].split(':')
35
+ content_hash = auth_array[0]
36
+ signature = auth_array[1]
37
+
38
+ case request.request_method
39
+ when 'GET'
40
+ content = { 'method' => request.request_method, 'data' => request.path }.to_json
41
+ when 'POST'
42
+ content = { 'method' => request.request_method, 'data' => request.POST }.to_json
43
+ when 'DELETE'
44
+ content = { 'method' => request.request_method, 'data' => request.path }.to_json
45
+ when 'PUT'
46
+ content = { 'method' => request.request_method, 'data' => request.POST }.to_json
47
+ end
48
+
49
+ hash = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha256'), @secret, content)
50
+ # puts content
51
+ # puts "Hash to Check: #{hash}"
52
+ # puts "Content Hash: #{content_hash}"
53
+
54
+ if signature == @signature && hash == content_hash
55
+ true
56
+ else
57
+ false
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,7 @@
1
+ module Rack
2
+ # Module which Contains different Authorization / Authentication Classes (HMAC, ..)
3
+ module SimpleAuth
4
+ # Current Gem Version
5
+ VERSION = '0.0.1'
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/simple_auth/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-simple_auth"
8
+ spec.version = Rack::SimpleAuth::VERSION
9
+ spec.authors = ["Benny1992"]
10
+ spec.email = ["klotz.benjamin@yahoo.de"]
11
+ spec.summary = %q{SimpleAuth HMAC authentication}
12
+ spec.description = %q{SimpleAuth HMAC authentication}
13
+ spec.homepage = "http://www.bennyklotz.at"
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_runtime_dependency "rack"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "coveralls"
26
+ spec.add_development_dependency "rack-test"
27
+ end
@@ -0,0 +1,5 @@
1
+ task :default do
2
+ Rake::Task['test:unit'].invoke
3
+ # Rake::Task['test:spec'].invoke
4
+ # Rake::Task['test:feature'].invoke
5
+ end
@@ -0,0 +1,33 @@
1
+ test_runs = if ENV['TESTS']
2
+ Integer(ENV['TESTS'])
3
+ else
4
+ 30
5
+ end
6
+
7
+ namespace :floodtest do
8
+ task :unit do
9
+ desc 'Run Unit floodtest (default 30 tests, configurable via ENV["TESTS"])'
10
+ 1.upto(test_runs) do |i|
11
+ puts "Running test #{i} of #{test_runs}"
12
+ exit(-1) if !system('bundle exec rake test:unit')
13
+ end
14
+ end
15
+
16
+ # task :spec do
17
+ # desc 'Run Spec floodtest (default 30 tests, configurable via ENV["TESTS"])'
18
+ # 1.upto(test_runs) do |i|
19
+ # puts "Running test #{i} of #{test_runs}"
20
+ # exit(-1) if !system('bundle exec rake test:spec ')
21
+ # end
22
+ # end
23
+
24
+ # task :feature do
25
+ # desc 'Run Feature floodtest (default 30 tests, configurable via ENV["TESTS"])'
26
+ # 1.upto(test_runs) do |i|
27
+ # puts "Running test #{i} of #{test_runs}"
28
+ # exit(-1) if !system('bundle exec rake test:feature')
29
+ # end
30
+ # end
31
+ end
32
+
33
+
data/tasks/test.rake ADDED
@@ -0,0 +1,19 @@
1
+ namespace :test do
2
+ Rake::TestTask.new(:unit) do |t|
3
+ t.libs << "test" << "bin" << "ext" << "controllers" << "helpers" << "models"
4
+ t.test_files = FileList['test/**/*_test.rb']
5
+ t.verbose = true
6
+ end
7
+
8
+ # Rake::TestTask.new(:spec) do |t|
9
+ # t.libs << "spec" << "bin" << "ext" << "controllers" << "helpers" << "models"
10
+ # t.test_files = FileList['spec/**/*_spec.rb']
11
+ # t.verbose = true
12
+ # end
13
+
14
+ # Cucumber::Rake::Task.new(:feature) do |t|
15
+ # t.cucumber_opts = "features --format pretty"
16
+ # end
17
+ end
18
+
19
+
data/tasks/travis.rake ADDED
@@ -0,0 +1,5 @@
1
+ task :travis do
2
+ Rake::Task['floodtest:unit'].invoke
3
+ # Rake::Task['floodtest:feature'].invoke
4
+ # Rake::Task['floodtest:spec'].invoke
5
+ end
data/test/config.ru ADDED
@@ -0,0 +1,5 @@
1
+ require 'rack/lobster'
2
+ require 'rack/simple_auth'
3
+
4
+ use Rack::SimpleAuth::HMAC, 'test_signature', 'test_secret'
5
+ run Rack::Lobster.new
@@ -0,0 +1,83 @@
1
+ require 'test_helper.rb'
2
+
3
+ # Test HMAC Authorization Method
4
+ class HMACTest < MiniTest::Unit::TestCase
5
+ include Rack::Test::Methods
6
+
7
+ def setup
8
+ @secret = 'test_secret'
9
+ @signature = 'test_signature'
10
+ end
11
+
12
+ def app
13
+ Rack::SimpleAuth.testapp
14
+ end
15
+
16
+ def test_get_without_auth_header
17
+ get '/'
18
+ assert_equal(401, last_response.status, 'Unauthorized reqeust should receive 401')
19
+ end
20
+
21
+ def test_get_with_wrong_auth_header
22
+ get '/', {}, 'HTTP_AUTHORIZATION' => 'wrong_header'
23
+ assert_equal(401, last_response.status, 'Wrong HTTP_AUTHORIZATION Header should receive 401')
24
+ end
25
+
26
+ def test_get_with_right_auth_header
27
+ uri = '/'
28
+ content = { 'method' => 'GET', 'data' => uri }.to_json
29
+ hash = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha256'), @secret, content)
30
+
31
+ get uri, {}, 'HTTP_AUTHORIZATION' => "#{hash}:#{@signature}"
32
+
33
+ assert_equal(200, last_response.status, 'Authorized Request should receive 200')
34
+ end
35
+
36
+ def test_post_with_wrong_auth_header
37
+ post '/', { 'name' => 'Bensn' }, 'HTTP_AUTHORIZATION' => 'wrong_header'
38
+ assert_equal(401, last_response.status, 'Wrong HTTP_AUTHORIZATION Header should receive 401')
39
+ end
40
+
41
+ def test_post_with_right_auth_header
42
+ params = { 'name' => 'Bensn' }
43
+ content = { 'method' => 'POST', 'data' => params }.to_json
44
+ hash = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha256'), @secret, content)
45
+
46
+ post '/', params, 'HTTP_AUTHORIZATION' => "#{hash}:#{@signature}"
47
+
48
+ assert_equal(200, last_response.status, 'Authorized Request should receive 200')
49
+ end
50
+
51
+ def test_delete_with_wrong_auth_header
52
+ delete '/', {}, 'HTTP_AUTHORIZATION' => 'wrong_header'
53
+ assert_equal(401, last_response.status, 'Wrong HTTP_AUTHORIZATION Header should receive 401')
54
+ end
55
+
56
+ def test_delete_with_right_auth_header
57
+ uri = '/'
58
+ content = { 'method' => 'DELETE', 'data' => uri }.to_json
59
+ hash = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha256'), @secret, content)
60
+
61
+ delete uri, {}, 'HTTP_AUTHORIZATION' => "#{hash}:#{@signature}"
62
+
63
+ assert_equal(200, last_response.status, 'Authorized Request should receive 200')
64
+ end
65
+
66
+ def test_put_with_wrong_auth_header
67
+ put '/', { 'name' => 'Bensn' }, 'HTTP_AUTHORIZATION' => 'wrong_header'
68
+ assert_equal(401, last_response.status, 'Wrong HTTP_AUTHORIZATION Header should receive 401')
69
+ end
70
+
71
+ def test_post_with_right_auth_header
72
+ params = { 'name' => 'Bensn' }
73
+ content = { 'method' => 'PUT', 'data' => params }.to_json
74
+ hash = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha256'), @secret, content)
75
+
76
+ put '/', params, 'HTTP_AUTHORIZATION' => "#{hash}:#{@signature}"
77
+
78
+ assert_equal(200, last_response.status, 'Authorized Request should receive 200')
79
+ end
80
+
81
+ def teardown
82
+ end
83
+ end
@@ -0,0 +1,40 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+
9
+ SimpleCov.start do
10
+ project_name 'rack-simple_auth'
11
+ add_filter '/test/'
12
+ add_filter '/pkg/'
13
+ add_filter '/spec/'
14
+ add_filter '/features/'
15
+ add_filter '/doc/'
16
+ end if ENV['COVERAGE']
17
+
18
+ # Minitest
19
+ require 'minitest/autorun'
20
+ require 'minitest/mock'
21
+ require 'minitest/pride' # for colored output
22
+
23
+ # Rack Test Methods
24
+ require 'rack/test'
25
+
26
+ require 'json'
27
+
28
+ # Load gem
29
+ require 'rack/simple_auth'
30
+
31
+ module Rack
32
+ # Module which Contains different Authorization / Authentication Classes (HMAC, ..)
33
+ module SimpleAuth
34
+ class << self
35
+ attr_accessor :testapp
36
+ end
37
+ end
38
+ end
39
+
40
+ Rack::SimpleAuth.testapp = Rack::Builder.parse_file("#{Rack::SimpleAuth.root}/test/config.ru").first
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-simple_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Benny1992
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2014-03-09 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - &id003
18
+ - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: "0"
21
+ name: rack
22
+ prerelease: false
23
+ type: :runtime
24
+ version_requirements: *id001
25
+ - !ruby/object:Gem::Dependency
26
+ requirement: &id002 !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: "1.5"
31
+ name: bundler
32
+ prerelease: false
33
+ type: :development
34
+ version_requirements: *id002
35
+ - !ruby/object:Gem::Dependency
36
+ requirement: &id004 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - *id003
39
+ name: rake
40
+ prerelease: false
41
+ type: :development
42
+ version_requirements: *id004
43
+ - !ruby/object:Gem::Dependency
44
+ requirement: &id005 !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - *id003
47
+ name: coveralls
48
+ prerelease: false
49
+ type: :development
50
+ version_requirements: *id005
51
+ - !ruby/object:Gem::Dependency
52
+ requirement: &id006 !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - *id003
55
+ name: rack-test
56
+ prerelease: false
57
+ type: :development
58
+ version_requirements: *id006
59
+ description: SimpleAuth HMAC authentication
60
+ email:
61
+ - klotz.benjamin@yahoo.de
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files: []
67
+
68
+ files:
69
+ - .gitignore
70
+ - .rubocop.yml
71
+ - .travis.yml
72
+ - .yardopts
73
+ - Gemfile
74
+ - LICENSE.txt
75
+ - README.md
76
+ - Rakefile
77
+ - lib/rack/simple_auth.rb
78
+ - lib/rack/simple_auth/hmac.rb
79
+ - lib/rack/simple_auth/version.rb
80
+ - rack-simple_auth.gemspec
81
+ - tasks/default.rake
82
+ - tasks/floodtest.rake
83
+ - tasks/test.rake
84
+ - tasks/travis.rake
85
+ - test/config.ru
86
+ - test/rack/simple_auth/hmac_test.rb
87
+ - test/test_helper.rb
88
+ homepage: http://www.bennyklotz.at
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+
93
+ post_install_message:
94
+ rdoc_options: []
95
+
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - *id003
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - *id003
104
+ requirements: []
105
+
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.2
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: SimpleAuth HMAC authentication
111
+ test_files:
112
+ - test/config.ru
113
+ - test/rack/simple_auth/hmac_test.rb
114
+ - test/test_helper.rb